pulumi/pkg/backend/filestate/backend_test.go
Lee Briggs bad67d3242
switch os/user with luser
We make several calls to `os/user`, which uses CGO and means
cross-compilation is not possible. This replaces `os/user` with the
`luser` package, which is a drop-in replacement which does not use `CGO`
2020-07-27 14:44:08 -07:00

66 lines
2 KiB
Go

package filestate
import (
user "github.com/tweekmonster/luser"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMassageBlobPath(t *testing.T) {
testMassagePath := func(t *testing.T, s string, want string) {
massaged, err := massageBlobPath(s)
assert.NoError(t, err)
assert.Equal(t, want, massaged,
"massageBlobPath(%s) didn't return expected result.\nWant: %q\nGot: %q", s, want, massaged)
}
// URLs not prefixed with "file://" are kept as-is. Also why we add FilePathPrefix as a prefix for other tests.
t.Run("NonFilePrefixed", func(t *testing.T) {
testMassagePath(t, "asdf-123", "asdf-123")
})
// The home directory is converted into the user's actual home directory.
// Which requires even more tweaks to work on Windows.
t.Run("PrefixedWithTilde", func(t *testing.T) {
usr, err := user.Current()
if err != nil {
t.Fatalf("Unable to get current user: %v", err)
}
homeDir := usr.HomeDir
// When running on Windows, the "home directory" takes on a different meaning.
if runtime.GOOS == "windows" {
t.Logf("Running on %v", runtime.GOOS)
t.Run("NormalizeDirSeparator", func(t *testing.T) {
testMassagePath(t, FilePathPrefix+`C:\Users\steve\`, FilePathPrefix+"/C:/Users/steve")
})
newHomeDir := "/" + filepath.ToSlash(homeDir)
t.Logf("Changed homeDir to expect from %q to %q", homeDir, newHomeDir)
homeDir = newHomeDir
}
testMassagePath(t, FilePathPrefix+"~", FilePathPrefix+homeDir)
testMassagePath(t, FilePathPrefix+"~/alpha/beta", FilePathPrefix+homeDir+"/alpha/beta")
})
t.Run("MakeAbsolute", func(t *testing.T) {
// Run the expected result through filepath.Abs, since on Windows we expect "C:\1\2".
expected := "/1/2"
abs, err := filepath.Abs(expected)
assert.NoError(t, err)
expected = filepath.ToSlash(abs)
if expected[0] != '/' {
expected = "/" + expected // A leading slash is added on Windows.
}
testMassagePath(t, FilePathPrefix+"/1/2/3/../4/..", FilePathPrefix+expected)
})
}