diff --git a/pkg/cache/cache_test.go b/pkg/cache/cache_test.go index e1098c76a..d84e1f5d0 100644 --- a/pkg/cache/cache_test.go +++ b/pkg/cache/cache_test.go @@ -3,7 +3,6 @@ package cache import ( "fmt" "os" - "path" "path/filepath" "testing" @@ -160,7 +159,7 @@ func TestStoreRelocationMapping(t *testing.T) { relocationMapping: relocation.ImageRelocationMap{ "asd": "asdf", }, - wantedReloPath: path.Join(home+"/.porter/cache", kahn1dot0Hash, "cnab", "relocation-mapping.json"), + wantedReloPath: filepath.Join(home+"/.porter/cache", kahn1dot0Hash, "cnab", "relocation-mapping.json"), }, "no relocation file gets no path": { tag: kahnlatest, diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index bda5383e1..70de2f7dd 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -18,7 +18,7 @@ func TestConfig_GetHomeDir(t *testing.T) { home, err := c.GetHomeDir() require.NoError(t, err) - assert.Equal(t, "/home/myuser/.porter", home) + assert.Equal(t, filepath.FromSlash("/home/myuser/.porter"), home) } func TestConfig_GetHomeDirFromSymlink(t *testing.T) { diff --git a/pkg/config/helpers.go b/pkg/config/helpers.go index 1633089ce..dafb1e664 100644 --- a/pkg/config/helpers.go +++ b/pkg/config/helpers.go @@ -38,7 +38,7 @@ func NewTestConfig(t *testing.T) *TestConfig { // SetupUnitTest initializes the unit test filesystem with the supporting files in the PORTER_HOME directory. func (c *TestConfig) SetupUnitTest() { // Set up the test porter home directory - home := "/home/myuser/.porter" + home := filepath.FromSlash("/home/myuser/.porter") c.SetHomeDir(home) // Fake out the porter home directory diff --git a/pkg/exec/builder/execute_test.go b/pkg/exec/builder/execute_test.go index 934bfe031..5113b80e1 100644 --- a/pkg/exec/builder/execute_test.go +++ b/pkg/exec/builder/execute_test.go @@ -2,7 +2,6 @@ package builder import ( "context" - "fmt" "os" "path/filepath" "runtime" @@ -159,8 +158,13 @@ func TestExecuteStep_SpecifiesCustomWorkingDirectory(t *testing.T) { SuffixArguments: []string{}, } + if runtime.GOOS == "windows" { + step.TestStep.Command = "cmd.exe" + step.Arguments = []string{"/c", "cd"} + } + _, err := ExecuteStep(ctx, c.RuntimeConfig, step) - assert.Equal(t, fmt.Sprintln(wd), c.TestContext.GetOutput()) + assert.Equal(t, wd, strings.TrimRight(c.TestContext.GetOutput(), "\r\n")) require.NoError(t, err, "Execute Step failed") } diff --git a/pkg/mixin/pkgmgmt_test.go b/pkg/mixin/pkgmgmt_test.go index df81a0179..70713c65c 100644 --- a/pkg/mixin/pkgmgmt_test.go +++ b/pkg/mixin/pkgmgmt_test.go @@ -16,12 +16,12 @@ func TestRunner_BuildCommand(t *testing.T) { runnerCommand string wantCommand string }{ - {"build", "build", "/home/myuser/.porter/mixins/exec/exec build"}, - {"install", "install", "/home/myuser/.porter/mixins/exec/exec install"}, - {"upgrade", "upgrade", "/home/myuser/.porter/mixins/exec/exec upgrade"}, - {"uninstall", "uninstall", "/home/myuser/.porter/mixins/exec/exec uninstall"}, - {"invoke", "status", "/home/myuser/.porter/mixins/exec/exec invoke --action status"}, - {"version", "version --output json", "/home/myuser/.porter/mixins/exec/exec version --output json"}, + {"build", "build", "/home/myuser/.porter/mixins/exec/exec build\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe build"}, + {"install", "install", "/home/myuser/.porter/mixins/exec/exec install\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe install"}, + {"upgrade", "upgrade", "/home/myuser/.porter/mixins/exec/exec upgrade\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe upgrade"}, + {"uninstall", "uninstall", "/home/myuser/.porter/mixins/exec/exec uninstall\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe uninstall"}, + {"invoke", "status", "/home/myuser/.porter/mixins/exec/exec invoke --action status\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe invoke --action status"}, + {"version", "version --output json", "/home/myuser/.porter/mixins/exec/exec version --output json\n\\home\\myuser\\.porter\\mixins\\exec\\exec.exe version --output json"}, } for _, tc := range testcases { diff --git a/pkg/pkgmgmt/client/install_test.go b/pkg/pkgmgmt/client/install_test.go index ab58354d0..153c22652 100644 --- a/pkg/pkgmgmt/client/install_test.go +++ b/pkg/pkgmgmt/client/install_test.go @@ -64,6 +64,9 @@ func TestFileSystem_InstallFromUrl(t *testing.T) { } else { require.NoError(t, err) clientPath := "/home/myuser/.porter/packages/mypkg/mypkg" + if runtime.GOOS == "windows" { + clientPath += ".exe" + } clientStats, err := p.FileSystem.Stat(clientPath) require.NoError(t, err) wantMode := pkg.FileModeExecutable @@ -116,6 +119,9 @@ func TestFileSystem_InstallFromFeedUrl(t *testing.T) { require.NoError(t, err) clientExists, _ := p.FileSystem.Exists("/home/myuser/.porter/packages/helm/helm") + if runtime.GOOS == "windows" { + clientExists, _ = p.FileSystem.Exists("/home/myuser/.porter/packages/helm/helm.exe") + } assert.True(t, clientExists) runtimeExists, _ := p.FileSystem.Exists("/home/myuser/.porter/packages/helm/runtimes/helm-runtime") assert.True(t, runtimeExists) diff --git a/pkg/pkgmgmt/feed/generate.go b/pkg/pkgmgmt/feed/generate.go index 0d4a870d7..6eeae392c 100644 --- a/pkg/pkgmgmt/feed/generate.go +++ b/pkg/pkgmgmt/feed/generate.go @@ -65,7 +65,7 @@ func (feed *MixinFeed) Generate(ctx context.Context, opts GenerateOptions) error } } - mixinRegex := regexp.MustCompile(`(.*/)?(.+)/([a-z0-9-]+)-(linux|windows|darwin)-(amd64|arm64)(\.exe)?`) + mixinRegex := regexp.MustCompile(`(.*[/\\])?(.+)[/\\]([a-z0-9-]+)-(linux|windows|darwin)-(amd64|arm64)(\.exe)?`) err = feed.FileSystem.Walk(opts.SearchDirectory, func(path string, info os.FileInfo, err error) error { if err != nil { diff --git a/pkg/pkgmgmt/feed/generate_test.go b/pkg/pkgmgmt/feed/generate_test.go index 1c2c93da8..f5208e202 100644 --- a/pkg/pkgmgmt/feed/generate_test.go +++ b/pkg/pkgmgmt/feed/generate_test.go @@ -124,7 +124,7 @@ func TestGenerate_RegexMatch(t *testing.T) { }{{ name: "no bins", mixinName: "", - wantError: `failed to traverse the bin directory: open /bin: file does not exist`, + wantError: `file does not exist`, }, { name: "valid mixin name", mixinName: "my-42nd-mixin", diff --git a/pkg/porter/archive.go b/pkg/porter/archive.go index 6a66ddf01..72977be72 100644 --- a/pkg/porter/archive.go +++ b/pkg/porter/archive.go @@ -59,7 +59,7 @@ func (p *Porter) Archive(ctx context.Context, opts ArchiveOptions) error { dir := filepath.Dir(opts.ArchiveFile) if _, err := p.Config.FileSystem.Stat(dir); os.IsNotExist(err) { - return log.Error(fmt.Errorf("parent directory %q does not exist", dir)) + return log.Error(fmt.Errorf("parent directory %q does not exist", filepath.ToSlash(dir))) } bundleRef, err := opts.GetBundleReference(ctx, p) diff --git a/pkg/porter/archive_test.go b/pkg/porter/archive_test.go index 0ca8b998d..37f6e7052 100644 --- a/pkg/porter/archive_test.go +++ b/pkg/porter/archive_test.go @@ -2,6 +2,7 @@ package porter import ( "context" + "path/filepath" "testing" "get.porter.sh/porter/pkg" @@ -65,7 +66,7 @@ func TestArchive_ArchiveDirectory(t *testing.T) { fs: p.FileSystem, } - dir, err := ex.createArchiveFolder("examples/test-bundle-0.2.0") + dir, err := ex.createArchiveFolder(filepath.FromSlash("examples/test-bundle-0.2.0")) require.NoError(t, err) require.Contains(t, dir, "examples-test-bundle-0.2.0") diff --git a/pkg/porter/cnab_test.go b/pkg/porter/cnab_test.go index 813ebd887..59e14e752 100644 --- a/pkg/porter/cnab_test.go +++ b/pkg/porter/cnab_test.go @@ -1,6 +1,7 @@ package porter import ( + "fmt" "path/filepath" "testing" @@ -23,7 +24,7 @@ func TestSharedOptions_defaultBundleFiles(t *testing.T) { require.NoError(t, err) assert.Equal(t, "porter.yaml", opts.File) - assert.Equal(t, ".cnab/bundle.json", opts.CNABFile) + assert.Equal(t, filepath.FromSlash(".cnab/bundle.json"), opts.CNABFile) } func TestSharedOptions_defaultBundleFiles_AltManifest(t *testing.T) { @@ -37,7 +38,7 @@ func TestSharedOptions_defaultBundleFiles_AltManifest(t *testing.T) { err := opts.defaultBundleFiles(cxt.Context) require.NoError(t, err) - assert.Equal(t, ".cnab/bundle.json", opts.CNABFile) + assert.Equal(t, filepath.FromSlash(".cnab/bundle.json"), opts.CNABFile) } func TestSharedOptions_defaultBundleFiles_CNABFile(t *testing.T) { @@ -88,7 +89,9 @@ func TestSharedOptions_validateBundleJson(t *testing.T) { if tc.wantError == "" { require.NoError(t, err) - assert.Equal(t, opts.CNABFile, tc.wantBundleJson) + wantBundleJsonAbs, err := filepath.Abs(tc.wantBundleJson) + require.NoError(t, err) + assert.Equal(t, wantBundleJsonAbs, opts.CNABFile) } else { require.Error(t, err) assert.Contains(t, err.Error(), tc.wantError) @@ -98,6 +101,10 @@ func TestSharedOptions_validateBundleJson(t *testing.T) { } func Test_bundleFileOptions(t *testing.T) { + absWantFile := absOSFilepath(t, "/"+config.Name) + absWantCNABFile := absOSFilepath(t, "/"+build.LOCAL_BUNDLE) + absPathToBundle := absOSFilepath(t, "/path/to/bundle") + testcases := []struct { name string opts BundleDefinitionOptions @@ -110,8 +117,8 @@ func Test_bundleFileOptions(t *testing.T) { name: "no opts", opts: BundleDefinitionOptions{}, setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { return nil }, - wantFile: "/" + config.Name, - wantCNABFile: "/" + build.LOCAL_BUNDLE, + wantFile: absWantFile, + wantCNABFile: absWantCNABFile, wantError: "", }, { name: "reference set", @@ -125,12 +132,12 @@ func Test_bundleFileOptions(t *testing.T) { }, { name: "invalid dir", opts: BundleDefinitionOptions{ - Dir: "path/to/bundle", + Dir: filepath.FromSlash("path/to/bundle"), }, setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { return nil }, wantFile: "", wantCNABFile: "", - wantError: `"path/to/bundle" is not a valid directory: open /path/to/bundle: file does not exist`, + wantError: fmt.Sprintf("%q is not a valid directory: open %s: file does not exist", filepath.FromSlash("path/to/bundle"), absPathToBundle), }, { name: "invalid file", opts: BundleDefinitionOptions{ @@ -139,11 +146,11 @@ func Test_bundleFileOptions(t *testing.T) { setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { return nil }, wantFile: "", wantCNABFile: "", - wantError: "unable to access --file /alternate/porter.yaml: open /alternate/porter.yaml: file does not exist", + wantError: fmt.Sprintf("unable to access --file %s: open %s: file does not exist", absOSFilepath(t, "/alternate/porter.yaml"), absOSFilepath(t, "/alternate/porter.yaml")), }, { name: "valid dir", opts: BundleDefinitionOptions{ - Dir: "path/to/bundle", + Dir: absOSFilepath(t, "/path/to/bundle"), }, setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { err := ctx.FileSystem.MkdirAll(filepath.Join(opts.Dir, config.Name), pkg.FileModeDirectory) @@ -152,8 +159,8 @@ func Test_bundleFileOptions(t *testing.T) { } return ctx.FileSystem.MkdirAll(opts.Dir, pkg.FileModeDirectory) }, - wantFile: "/path/to/bundle/porter.yaml", - wantCNABFile: "/path/to/bundle/.cnab/bundle.json", + wantFile: absOSFilepath(t, "/path/to/bundle/porter.yaml"), + wantCNABFile: absOSFilepath(t, "/path/to/bundle/.cnab/bundle.json"), wantError: "", }, { name: "valid file", @@ -163,14 +170,14 @@ func Test_bundleFileOptions(t *testing.T) { setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { return ctx.FileSystem.MkdirAll(opts.File, pkg.FileModeDirectory) }, - wantFile: "/alternate/porter.yaml", - wantCNABFile: "/" + build.LOCAL_BUNDLE, + wantFile: absOSFilepath(t, "/alternate/porter.yaml"), + wantCNABFile: absOSFilepath(t, "/"+build.LOCAL_BUNDLE), wantError: "", }, { name: "valid dir and file", opts: BundleDefinitionOptions{ - Dir: "path/to/bundle", - File: "alternate/porter.yaml", + Dir: absOSFilepath(t, "/path/to/bundle"), + File: filepath.FromSlash("alternate/porter.yaml"), }, setup: func(ctx *portercontext.Context, opts BundleDefinitionOptions) error { err := ctx.FileSystem.MkdirAll(filepath.Join(opts.Dir, opts.File), pkg.FileModeDirectory) @@ -179,8 +186,8 @@ func Test_bundleFileOptions(t *testing.T) { } return ctx.FileSystem.MkdirAll(opts.Dir, pkg.FileModeDirectory) }, - wantFile: "/path/to/bundle/alternate/porter.yaml", - wantCNABFile: "/path/to/bundle/.cnab/bundle.json", + wantFile: absOSFilepath(t, "/path/to/bundle/alternate/porter.yaml"), + wantCNABFile: absOSFilepath(t, "/path/to/bundle/.cnab/bundle.json"), wantError: "", }} @@ -209,9 +216,16 @@ func Test_bundleFileOptions(t *testing.T) { if tc.opts.Dir != "" && tc.wantError == "" { require.Equal(t, tc.opts.Dir, wd) } else { - require.Equal(t, "/", wd) + path := absOSFilepath(t, "/") + require.Equal(t, path, wd) } } }) } } + +func absOSFilepath(t *testing.T, path string) string { + result, err := filepath.Abs(filepath.FromSlash(path)) + require.NoError(t, err) + return result +} diff --git a/pkg/porter/credentials_test.go b/pkg/porter/credentials_test.go index f4c4942a5..cd28c824b 100644 --- a/pkg/porter/credentials_test.go +++ b/pkg/porter/credentials_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "testing" "time" @@ -302,6 +303,12 @@ func TestCredentialsEdit(t *testing.T) { p.Setenv("EDITOR", "vi") p.Setenv(test.ExpectedCommandEnv, "bash -c vi "+filepath.Join(os.TempDir(), "porter-kool-kreds.yaml")) + if runtime.GOOS == "windows" { + p.Setenv("SHELL", "cmd") + p.Setenv("EDITOR", "notepad") + p.Setenv(test.ExpectedCommandEnv, "cmd /C notepad "+filepath.Join(os.TempDir(), "porter-kool-kreds.yaml")) + } + opts := CredentialEditOptions{Namespace: "dev", Name: "kool-kreds"} p.TestCredentials.AddTestCredentialsDirectory("testdata/test-creds") @@ -315,7 +322,10 @@ func TestCredentialsEditEditorPathWithArgument(t *testing.T) { p.Setenv("SHELL", "something") p.Setenv("EDITOR", "C:\\Program Files\\Visual Studio Code\\code.exe --wait") - p.Setenv(test.ExpectedCommandEnv, "something -c C:\\Program Files\\Visual Studio Code\\code.exe --wait "+filepath.Join(os.TempDir(), "porter-kool-kreds.yaml")) + p.Setenv(test.ExpectedCommandEnv, fmt.Sprintf("something -c C:\\Program Files\\Visual Studio Code\\code.exe --wait %s", filepath.Join(os.TempDir(), "porter-kool-kreds.yaml"))) + if runtime.GOOS == "windows" { + p.Setenv(test.ExpectedCommandEnv, fmt.Sprintf("something /C C:\\Program Files\\Visual Studio Code\\code.exe --wait %s", filepath.Join(os.TempDir(), "porter-kool-kreds.yaml"))) + } opts := CredentialEditOptions{Namespace: "dev", Name: "kool-kreds"} p.TestCredentials.AddTestCredentialsDirectory("testdata/test-creds") diff --git a/pkg/porter/publish_test.go b/pkg/porter/publish_test.go index fe1ffcbfb..296c5951d 100644 --- a/pkg/porter/publish_test.go +++ b/pkg/porter/publish_test.go @@ -3,7 +3,9 @@ package porter import ( "context" "errors" + "runtime" "testing" + "time" "get.porter.sh/porter/pkg" "get.porter.sh/porter/pkg/cache" @@ -34,12 +36,10 @@ func TestPublish_Validate_PorterYamlDoesNotExist(t *testing.T) { opts := PublishOptions{} err := opts.Validate(p.Config) - require.Error(t, err, "validation should have failed") - assert.EqualError( + require.ErrorContains( t, err, - "could not find porter.yaml in the current directory /, make sure you are in the right directory or specify the porter manifest with --file", - "porter.yaml not present so should have failed validation", + "could not find porter.yaml in the current directory", ) } @@ -51,7 +51,7 @@ func TestPublish_Validate_ArchivePath(t *testing.T) { ArchiveFile: "mybuns.tgz", } err := opts.Validate(p.Config) - assert.EqualError(t, err, "unable to access --archive mybuns.tgz: open /mybuns.tgz: file does not exist") + assert.ErrorContains(t, err, "file does not exist") p.FileSystem.WriteFile("mybuns.tgz", []byte("mybuns"), pkg.FileModeWritable) err = opts.Validate(p.Config) @@ -225,6 +225,11 @@ func TestPublish_RefreshCachedBundle(t *testing.T) { require.NoError(t, err) origBunPathTime := file.ModTime() + if runtime.GOOS == "windows" { + // see https://github.com/getporter/porter/issues/2858 + time.Sleep(5 * time.Millisecond) + } + // Should refresh cache err = p.refreshCachedBundle(bundleRef) require.NoError(t, err, "should have successfully updated the cache") diff --git a/pkg/portercontext/context_test.go b/pkg/portercontext/context_test.go index 606cfbbff..f7b4e8516 100644 --- a/pkg/portercontext/context_test.go +++ b/pkg/portercontext/context_test.go @@ -3,6 +3,7 @@ package portercontext import ( "context" "errors" + "runtime" "testing" "github.com/stretchr/testify/assert" @@ -53,5 +54,9 @@ func TestContext_LogToFile(t *testing.T) { c.CompareGoldenFile("testdata/expected-logs.txt", string(logContents)) // Compare the human readable logs sent to stderr - c.CompareGoldenFile("testdata/expected-output.txt", c.GetError()) + if runtime.GOOS == "windows" { + c.CompareGoldenFile("testdata/expected-output-windows.txt", c.GetError()) + } else { + c.CompareGoldenFile("testdata/expected-output.txt", c.GetError()) + } } diff --git a/pkg/portercontext/testdata/expected-output-windows.txt b/pkg/portercontext/testdata/expected-output-windows.txt new file mode 100644 index 000000000..c9e634daa --- /dev/null +++ b/pkg/portercontext/testdata/expected-output-windows.txt @@ -0,0 +1,4 @@ +Writing logs to C:\.porter\logs\0.json +a thing happened +a weird thing happened +a bad thing happened diff --git a/pkg/secrets/plugins/filesystem/store_test.go b/pkg/secrets/plugins/filesystem/store_test.go index b0a612aa9..0dfea537b 100644 --- a/pkg/secrets/plugins/filesystem/store_test.go +++ b/pkg/secrets/plugins/filesystem/store_test.go @@ -42,7 +42,7 @@ func TestFileSystem_SetSecretDir(t *testing.T) { s := filesystem.NewStore(c.Config) secretDir, err := s.SetSecretDir() require.NoError(t, err) - require.Equal(t, "/home/myuser/.porter/secrets", secretDir) + require.Equal(t, filepath.FromSlash("/home/myuser/.porter/secrets"), secretDir) } func TestFileSystem_DataOperation(t *testing.T) { diff --git a/pkg/storage/migrations/testhelpers/helpers.go b/pkg/storage/migrations/testhelpers/helpers.go index 2d32505bc..14130ab60 100644 --- a/pkg/storage/migrations/testhelpers/helpers.go +++ b/pkg/storage/migrations/testhelpers/helpers.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "runtime" "testing" "get.porter.sh/porter/pkg/config" @@ -33,11 +34,15 @@ func CreateLegacyPorterHome(t *testing.T) *config.TestConfig { oldPorterPath := filepath.Join(oldPorterDir, "porter"+xplat.FileExt()) if _, err = os.Stat(oldPorterPath); os.IsNotExist(err) { os.MkdirAll(oldPorterDir, 0700) - err = downloads.Download(oldPorterDir, downloads.DownloadOptions{ - UrlTemplate: "https://github.com/getporter/porter/releases/download/{{.VERSION}}/porter-{{.GOOS}}-amd64", + opts := downloads.DownloadOptions{ + UrlTemplate: "https://github.com/getporter/porter/releases/download/{{.VERSION}}/porter-{{.GOOS}}-amd64{{.EXT}}", Name: "porter", Version: "v0.38.10", - }) + } + if runtime.GOOS == "windows" { + opts.Ext = ".exe" + } + err = downloads.Download(oldPorterDir, opts) require.NoError(t, err, "Failed to download a copy of the old version of porter") } diff --git a/pkg/storage/parameters_test.go b/pkg/storage/parameters_test.go index 2d6e0ee54..ec5cec1f7 100644 --- a/pkg/storage/parameters_test.go +++ b/pkg/storage/parameters_test.go @@ -2,6 +2,7 @@ package storage import ( "reflect" + "strings" "testing" "time" @@ -55,7 +56,7 @@ func TestTestParameterProvider_Load(t *testing.T) { t.Run("unsuccessful load", func(t *testing.T) { _, err := p.Load("paramset.json") require.Error(t, err) - assert.Contains(t, err.Error(), "no such file or directory") + assert.True(t, strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "The system cannot find the file specified")) }) t.Run("successful load, unsuccessful unmarshal", func(t *testing.T) {