From c24c77ba2c8cb9888f6b0eb0f5b4a1cff2cf5d82 Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Fri, 8 Apr 2022 15:13:07 -0500 Subject: [PATCH 1/3] Improve helper methods for porter * Default to installing porter v1.0.0-alpha.19 with m1 support. If someone wants a different version they can use porter.EnsurePorterAt and specify the version. You can also change porter.DefaultPorterVersion to change the default version. * Add porter package with methods for installing and working with porter. It now provides UseLocalBinForPorterHome so you can install porter and the mixins into bin and not affect the user's installation. * Add porter.EnsureMixins to help with installing a set of mixins. * Update mixins that use these magesfiles to use a local porter in bin/ instead of the system porter. Signed-off-by: Carolyn Van Slyck --- .gitignore | 1 + go.mod | 2 +- go.sum | 4 +- magefile.go | 5 + mixins/magefile.go | 14 +-- porter/config.go | 43 +++++++++ porter/config_test.go | 64 +++++++++++++ porter/porter.go | 91 +++++++++++++++++++ porter/porter_test.go | 35 +++++++ releases/publish.go | 22 ++--- releases/publish_test.go | 35 +++++++ releases/testdata/atom-template.xml | 25 +++++ .../mixins/v1.2.3/mymixin-darwin-amd64 | 0 .../mixins/v1.2.3/mymixin-linux-amd64 | 0 .../mixins/v1.2.3/mymixin-windows-amd64.exe | 0 tools/install.go | 13 +-- tools/install_test.go | 3 +- 17 files changed, 322 insertions(+), 35 deletions(-) create mode 100644 .gitignore create mode 100644 porter/config.go create mode 100644 porter/config_test.go create mode 100644 porter/porter.go create mode 100644 porter/porter_test.go create mode 100644 releases/testdata/atom-template.xml create mode 100644 releases/testdata/mixins/v1.2.3/mymixin-darwin-amd64 create mode 100644 releases/testdata/mixins/v1.2.3/mymixin-linux-amd64 create mode 100644 releases/testdata/mixins/v1.2.3/mymixin-windows-amd64.exe diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/go.mod b/go.mod index f31ed9e..d8f8385 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.17 require ( github.com/Masterminds/semver/v3 v3.1.1 - github.com/carolynvs/magex v0.7.2 + github.com/carolynvs/magex v0.8.0 github.com/magefile/mage v1.11.0 github.com/stretchr/testify v1.7.0 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c diff --git a/go.sum b/go.sum index c4df7bf..a637503 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4= github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= -github.com/carolynvs/magex v0.7.2 h1:6IpoxUorSDOXtZmd72ExPCzQBxkeUj09JwJfnzf+9y8= -github.com/carolynvs/magex v0.7.2/go.mod h1:vZB3BkRfkd5ZMtkxJkCGbdFyWGoZiuNPKhx6uEQARmY= +github.com/carolynvs/magex v0.8.0 h1:BCY0A6fLs37NZrIFBKuOMXda5wEIrUeTF4sHB601JVM= +github.com/carolynvs/magex v0.8.0/go.mod h1:vZB3BkRfkd5ZMtkxJkCGbdFyWGoZiuNPKhx6uEQARmY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/magefile.go b/magefile.go index 96d804a..f638c80 100644 --- a/magefile.go +++ b/magefile.go @@ -5,8 +5,10 @@ package main import ( "get.porter.sh/magefiles/ci" + "get.porter.sh/magefiles/porter" "github.com/carolynvs/magex/mgx" "github.com/carolynvs/magex/shx" + "github.com/magefile/mage/mg" ) var must = shx.CommandBuilder{StopOnError: true} @@ -20,5 +22,8 @@ func Build() { } func Test() { + // Set up the bin with porter installed, tests can use that to initialize their local test bin to save time re-downloading it + mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter) + must.RunV("go", "test", "./...") } diff --git a/mixins/magefile.go b/mixins/magefile.go index 822d4e1..a7a967a 100644 --- a/mixins/magefile.go +++ b/mixins/magefile.go @@ -5,8 +5,8 @@ import ( "os" "path/filepath" + "get.porter.sh/magefiles/porter" "get.porter.sh/magefiles/releases" - "get.porter.sh/magefiles/tools" "github.com/carolynvs/magex/mgx" "github.com/carolynvs/magex/shx" "github.com/carolynvs/magex/xplat" @@ -63,6 +63,7 @@ func (m Magefile) Publish() { // Publish binaries to a github release // Requires PORTER_RELEASE_REPOSITORY to be set to github.com/USERNAME/REPO func (m Magefile) PublishBinaries() { + mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter) releases.PrepareMixinForPublish(m.MixinName) releases.PublishMixin(m.MixinName) } @@ -70,7 +71,7 @@ func (m Magefile) PublishBinaries() { // Publish a mixin feed // Requires PORTER_PACKAGES_REMOTE to be set to git@github.com:USERNAME/REPO.git func (m Magefile) PublishMixinFeed() { - mg.Deps(tools.EnsurePorter) + mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter) releases.PublishMixinFeed(m.MixinName) } @@ -89,14 +90,7 @@ func (m Magefile) TestPublish(username string) { // Install the mixin func (m Magefile) Install() { - porterHome := os.Getenv("PORTER_HOME") - if porterHome == "" { - home, _ := os.UserHomeDir() - porterHome = filepath.Join(home, ".porter") - } - if _, err := os.Stat(porterHome); err != nil { - panic("Could not find a Porter installation. Make sure that Porter is installed and set PORTER_HOME if you are using a non-standard installation path") - } + porterHome := porter.GetPorterHome() fmt.Printf("Installing the %s mixin into %s\n", m.MixinName, porterHome) os.MkdirAll(filepath.Join(porterHome, "mixins", m.MixinName, "runtimes"), 0770) diff --git a/porter/config.go b/porter/config.go new file mode 100644 index 0000000..57fb4c6 --- /dev/null +++ b/porter/config.go @@ -0,0 +1,43 @@ +package porter + +import ( + "os" + "path/filepath" + + "github.com/carolynvs/magex/xplat" +) + +var ( + // DefaultMixinVersion is the default version of mixins installed when it's not present + DefaultMixinVersion = "canary" +) + +// GetPorterHome determines the current PORTER_HOME directory +func GetPorterHome() string { + porterHome := os.Getenv("PORTER_HOME") + if porterHome == "" { + home, _ := os.UserHomeDir() + porterHome = filepath.Join(home, ".porter") + } + if _, err := os.Stat(porterHome); err != nil { + panic("Could not find a Porter installation. Make sure that Porter is installed and set PORTER_HOME if you are using a non-standard installation path") + } + return porterHome +} + +// UseBinForPorterHome sets the bin/ directory to be PORTER_HOME +func UseBinForPorterHome() { + // use bin as PORTER_HOME + wd, _ := os.Getwd() + home := filepath.Join(wd, "bin") + os.Mkdir(home, 0770) + UsePorterHome(home) +} + +// UsePorterHome sets the specified directory to be PORTER_HOME +func UsePorterHome(home string) { + os.Setenv("PORTER_HOME", home) + + // Add PORTER_HOME to the PATH + xplat.EnsureInPath(home) +} diff --git a/porter/config_test.go b/porter/config_test.go new file mode 100644 index 0000000..1b476c1 --- /dev/null +++ b/porter/config_test.go @@ -0,0 +1,64 @@ +package porter + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/carolynvs/magex/xplat" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetPorterHome(t *testing.T) { + t.Run("PORTER_HOME set", func(t *testing.T) { + tmpPorterHome, err := ioutil.TempDir("", "magefiles") + require.NoError(t, err) + defer os.RemoveAll(tmpPorterHome) + + os.Setenv("PORTER_HOME", tmpPorterHome) + defer os.Unsetenv("PORTER_HOME") + + gotHome := GetPorterHome() + assert.Equal(t, tmpPorterHome, gotHome) + }) + + t.Run("Default to HOME/.porter", func(t *testing.T) { + tmpUserHome, err := ioutil.TempDir("", "magefiles") + require.NoError(t, err) + defer os.RemoveAll(tmpUserHome) + tmpPorterHome := filepath.Join(tmpUserHome, ".porter") + err = os.Mkdir(tmpPorterHome, 0700) + require.NoError(t, err) + + os.Setenv("HOME", tmpUserHome) + defer os.Unsetenv("HOME") + + gotHome := GetPorterHome() + assert.Equal(t, tmpPorterHome, gotHome) + }) + + t.Run("no home found", func(t *testing.T) { + os.Unsetenv("HOME") + + defer func() { + panicObj := recover() + require.Contains(t, panicObj, "Could not find a Porter installation", "Expected a panic since there's no home for porter") + }() + + GetPorterHome() + }) +} + +func TestUseBinForPorterHome(t *testing.T) { + defer os.RemoveAll("bin") + + UseBinForPorterHome() + + pwd, _ := os.Getwd() + binDir := filepath.Join(pwd, "bin") + assert.Equal(t, binDir, os.Getenv("PORTER_HOME"), "PORTER_HOME was not set correctly") + + assert.True(t, xplat.InPath(binDir), "expected the bin directory to be in the PATH") +} diff --git a/porter/porter.go b/porter/porter.go new file mode 100644 index 0000000..719d6b8 --- /dev/null +++ b/porter/porter.go @@ -0,0 +1,91 @@ +package porter + +import ( + "log" + "os" + "path/filepath" + "runtime" + + "github.com/carolynvs/magex/mgx" + "github.com/carolynvs/magex/pkg/downloads" + "github.com/carolynvs/magex/shx" + "github.com/carolynvs/magex/xplat" +) + +var ( + // DefaultPorterVersion is the default version of Porter that is installed when it's not present + DefaultPorterVersion = "v1.0.0-alpha.19" +) + +// Install the default version of porter +func EnsurePorter() { + EnsurePorterAt(DefaultPorterVersion) +} + +// Install the specified version of porter +func EnsurePorterAt(version string) { + home := GetPorterHome() + runtimesDir := filepath.Join(home, "runtimes") + os.MkdirAll(runtimesDir, 0770) + + clientPath := filepath.Join(home, "porter"+xplat.FileExt()) + if _, err := os.Stat(clientPath); os.IsNotExist(err) { + log.Println("Porter client not found at", clientPath) + log.Println("Installing porter into", home) + opts := downloads.DownloadOptions{ + UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-{{.GOOS}}-{{.GOARCH}}{{.EXT}}", + Name: "porter", + Version: version, + Ext: xplat.FileExt(), + } + if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" { + // we don't yet publish arm64 for porter + opts.UrlTemplate = "https://cdn.porter.sh/{{.VERSION}}/porter-darwin-amd64" + } + err = downloads.Download(home, opts) + mgx.Must(err) + } + + runtimePath := filepath.Join(home, "runtimes", "porter-runtime") + if _, err := os.Stat(runtimePath); os.IsNotExist(err) { + log.Println("Porter runtime not found at", runtimePath) + opts := downloads.DownloadOptions{ + UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-linux-amd64", + Name: "porter-runtime", + Version: version, + } + err = downloads.Download(runtimesDir, opts) + mgx.Must(err) + } +} + +type InstallMixinOptions struct { + Name string + URL string + Feed string + Version string +} + +// EnsureMixin installs the specified mixin. +func EnsureMixin(mixin InstallMixinOptions) error { + home := GetPorterHome() + mixinDir := filepath.Join(home, "mixins", mixin.Name) + if _, err := os.Stat(mixinDir); err == nil { + log.Println("Mixin already installed:", mixin.Name) + return nil + } + + log.Println("Installing mixin:", mixin.Name) + if mixin.Version == "" { + mixin.Version = DefaultMixinVersion + } + var source string + if mixin.Feed != "" { + source = "--feed-url=" + mixin.Feed + } else { + source = "--url=" + mixin.URL + } + + porterPath := filepath.Join(home, "porter") + return shx.Run(porterPath, "mixin", "install", mixin.Name, "--version", mixin.Version, source) +} diff --git a/porter/porter_test.go b/porter/porter_test.go new file mode 100644 index 0000000..ce88bb6 --- /dev/null +++ b/porter/porter_test.go @@ -0,0 +1,35 @@ +package porter + +import ( + "os" + "path/filepath" + "testing" + + "github.com/carolynvs/magex/pkg" + "github.com/carolynvs/magex/xplat" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestEnsurePorter(t *testing.T) { + check := func(wantVersion string) { + UseBinForPorterHome() + defer os.RemoveAll("bin") + + EnsurePorter() + require.FileExists(t, filepath.Join("bin", "porter"+xplat.FileExt()), "expected the porter client to be in bin") + assert.FileExists(t, filepath.Join("bin", "runtimes", "porter-runtime"), "expected the porter runtime to be in bin") + + ok, err := pkg.IsCommandAvailable("porter", wantVersion, "--version") + require.NoError(t, err) + assert.True(t, ok, "could not resolve the desired porter version") + } + + t.Run("EnsurePorter - DefaultVersion", func(t *testing.T) { + check(DefaultPorterVersion) + }) + + t.Run("EnsurePorterAt - CustomVersion", func(t *testing.T) { + check("v1.0.0-alpha.1") + }) +} diff --git a/releases/publish.go b/releases/publish.go index 6fb106a..ed766bb 100644 --- a/releases/publish.go +++ b/releases/publish.go @@ -160,28 +160,24 @@ func PublishPluginFeed(plugin string) { publishPackageFeed("plugin", plugin) } -func generatePackageFeed(pkgType string) { +func generatePackageFeed(pkgType string) error { pkgDir := pkgType + "s" feedFile := filepath.Join(packagesRepo, pkgDir, "atom.xml") - - // Try to use a local copy of porter first, otherwise use the - // one installed in GOAPTH/bin - porterPath := "bin/porter" - if _, err := os.Stat(porterPath); err != nil { - porterPath = "porter" - tools.EnsurePorter() + if err := os.MkdirAll(filepath.Dir(feedFile), 0770); err != nil { + return err } - must.RunV(porterPath, "mixins", "feed", "generate", "-d", filepath.Join("bin", pkgDir), "-f", feedFile, "-t", "build/atom-template.xml") + + return shx.RunE("porter", "mixins", "feed", "generate", "-d", filepath.Join("bin", pkgDir), "-f", feedFile, "-t", "build/atom-template.xml") } // Generate a mixin feed from any mixin versions in bin/mixins. -func GenerateMixinFeed() { - generatePackageFeed("mixin") +func GenerateMixinFeed() error { + return generatePackageFeed("mixin") } // Generate a plugin feed from any plugin versions in bin/plugins. -func GeneratePluginFeed() { - generatePackageFeed("plugin") +func GeneratePluginFeed() error { + return generatePackageFeed("plugin") } // AddFilesToRelease uploads the files in the specified directory to a GitHub release. diff --git a/releases/publish_test.go b/releases/publish_test.go index 8ade0b1..faced74 100644 --- a/releases/publish_test.go +++ b/releases/publish_test.go @@ -3,8 +3,13 @@ package releases import ( "crypto/rand" "encoding/hex" + "io/ioutil" + "os" + "path/filepath" "testing" + "get.porter.sh/magefiles/porter" + "github.com/carolynvs/magex/shx" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -48,3 +53,33 @@ func TestAppendDataPath(t *testing.T) { output := AppendDataPath(data, dataPath) require.Equal(t, expected, output) } + +func TestGenerateMixinFeed(t *testing.T) { + tmp, err := ioutil.TempDir("", "magefiles") + require.NoError(t, err) + defer os.RemoveAll(tmp) + + // Install porter in our test bin + tmpBin := filepath.Join(tmp, "bin") + require.NoError(t, shx.Copy("../bin", tmpBin, shx.CopyRecursive), "failed to copy the porter bin into the test directory") + porter.UsePorterHome(tmpBin) + + // Copy our atom feed template + buildDir := filepath.Join(tmp, "build") + require.NoError(t, os.Mkdir(buildDir, 0770)) + require.NoError(t, shx.Copy("testdata/atom-template.xml", buildDir)) + + // Make a fake mixin release + require.NoError(t, shx.Copy("testdata/mixins", tmpBin, shx.CopyRecursive)) + + // Change into the tmp directory since the publish logic uses relative file paths + origDir, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, os.Chdir(tmp)) + defer os.Chdir(origDir) + + err = GenerateMixinFeed() + require.NoError(t, err) + + assert.FileExists(t, filepath.Join(tmpBin, "mixins/.packages/mixins/atom.xml"), "expected a mixin feed") +} diff --git a/releases/testdata/atom-template.xml b/releases/testdata/atom-template.xml new file mode 100644 index 0000000..03bbeb9 --- /dev/null +++ b/releases/testdata/atom-template.xml @@ -0,0 +1,25 @@ + + https://porter.sh + Porter Mixins + {{Updated}} + + + Porter Authors + https://porter.sh/mixins + + {{#Mixins}} + + {{/Mixins}} + {{#Entries}} + + https://cdn.porter.sh/mixins/{{Mixin}}/{{Version}} + {{Mixin}} @ {{Version}} + {{Updated}} + + {{Version}} + {{#Files}} + + {{/Files}} + + {{/Entries}} + diff --git a/releases/testdata/mixins/v1.2.3/mymixin-darwin-amd64 b/releases/testdata/mixins/v1.2.3/mymixin-darwin-amd64 new file mode 100644 index 0000000..e69de29 diff --git a/releases/testdata/mixins/v1.2.3/mymixin-linux-amd64 b/releases/testdata/mixins/v1.2.3/mymixin-linux-amd64 new file mode 100644 index 0000000..e69de29 diff --git a/releases/testdata/mixins/v1.2.3/mymixin-windows-amd64.exe b/releases/testdata/mixins/v1.2.3/mymixin-windows-amd64.exe new file mode 100644 index 0000000..e69de29 diff --git a/tools/install.go b/tools/install.go index c730894..ce668d8 100644 --- a/tools/install.go +++ b/tools/install.go @@ -11,10 +11,13 @@ import ( "github.com/carolynvs/magex/pkg" "github.com/carolynvs/magex/pkg/archive" "github.com/carolynvs/magex/pkg/downloads" + "github.com/carolynvs/magex/shx" ) -const ( - // Version of KIND to install if not already present +var ( + must = shx.CommandBuilder{StopOnError: true} + + // DefaultKindVersion is the default version of KinD that is installed when it's not present DefaultKindVersion = "v0.12.0" ) @@ -91,9 +94,3 @@ func EnsureKindAt(version string) { kindURL := "https://github.com/kubernetes-sigs/kind/releases/download/{{.VERSION}}/kind-{{.GOOS}}-{{.GOARCH}}" mgx.Must(pkg.DownloadToGopathBin(kindURL, "kind", version)) } - -// Install the latest version of porter -func EnsurePorter() { - err := pkg.DownloadToGopathBin("https://cdn.porter.sh/{{.VERSION}}/porter-{{.GOOS}}-{{.GOARCH}}{{.EXT}}", "porter", "latest") - mgx.Must(err) -} diff --git a/tools/install_test.go b/tools/install_test.go index 52dd65e..65bdf0b 100644 --- a/tools/install_test.go +++ b/tools/install_test.go @@ -1,11 +1,12 @@ package tools_test import ( + "testing" + "get.porter.sh/magefiles/tools" "github.com/carolynvs/magex/pkg" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "testing" ) func TestEnsureKind(t *testing.T) { From f27ca1e67a6b894d1055ac2f08b8e6cdbbf6013f Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Fri, 8 Apr 2022 15:25:51 -0500 Subject: [PATCH 2/3] Run tests from forks too Signed-off-by: Carolyn Van Slyck --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index fbb11f1..6cc1e42 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,6 +1,6 @@ name: Test -on: [push] +on: [push,pull_request] jobs: test: From 061d351cdae64ab0c284f0d07a7e77904ceeb142 Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Mon, 11 Apr 2022 16:10:16 -0500 Subject: [PATCH 3/3] EnsurePorterAt should enforce the version check When we installed into GOPATH/bin, it made since to be less picky about the installed version to avoid constantly changing the installed version of the tool. Now that we install into a local directory, we can require that the version matches exactly, or else reinstall it. Signed-off-by: Carolyn Van Slyck --- porter/porter.go | 11 +++++------ porter/porter_test.go | 39 ++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/porter/porter.go b/porter/porter.go index 719d6b8..868e32f 100644 --- a/porter/porter.go +++ b/porter/porter.go @@ -7,6 +7,7 @@ import ( "runtime" "github.com/carolynvs/magex/mgx" + "github.com/carolynvs/magex/pkg" "github.com/carolynvs/magex/pkg/downloads" "github.com/carolynvs/magex/shx" "github.com/carolynvs/magex/xplat" @@ -29,7 +30,7 @@ func EnsurePorterAt(version string) { os.MkdirAll(runtimesDir, 0770) clientPath := filepath.Join(home, "porter"+xplat.FileExt()) - if _, err := os.Stat(clientPath); os.IsNotExist(err) { + if clientFound, _ := pkg.IsCommandAvailable(clientPath, version, "--version"); !clientFound { log.Println("Porter client not found at", clientPath) log.Println("Installing porter into", home) opts := downloads.DownloadOptions{ @@ -42,20 +43,18 @@ func EnsurePorterAt(version string) { // we don't yet publish arm64 for porter opts.UrlTemplate = "https://cdn.porter.sh/{{.VERSION}}/porter-darwin-amd64" } - err = downloads.Download(home, opts) - mgx.Must(err) + mgx.Must(downloads.Download(home, opts)) } runtimePath := filepath.Join(home, "runtimes", "porter-runtime") - if _, err := os.Stat(runtimePath); os.IsNotExist(err) { + if runtimeFound, _ := pkg.IsCommandAvailable(runtimePath, version, "--version"); !runtimeFound { log.Println("Porter runtime not found at", runtimePath) opts := downloads.DownloadOptions{ UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-linux-amd64", Name: "porter-runtime", Version: version, } - err = downloads.Download(runtimesDir, opts) - mgx.Must(err) + mgx.Must(downloads.Download(runtimesDir, opts)) } } diff --git a/porter/porter_test.go b/porter/porter_test.go index ce88bb6..e21f73e 100644 --- a/porter/porter_test.go +++ b/porter/porter_test.go @@ -1,6 +1,7 @@ package porter import ( + "io/ioutil" "os" "path/filepath" "testing" @@ -12,24 +13,28 @@ import ( ) func TestEnsurePorter(t *testing.T) { - check := func(wantVersion string) { - UseBinForPorterHome() - defer os.RemoveAll("bin") - - EnsurePorter() - require.FileExists(t, filepath.Join("bin", "porter"+xplat.FileExt()), "expected the porter client to be in bin") - assert.FileExists(t, filepath.Join("bin", "runtimes", "porter-runtime"), "expected the porter runtime to be in bin") - - ok, err := pkg.IsCommandAvailable("porter", wantVersion, "--version") - require.NoError(t, err) - assert.True(t, ok, "could not resolve the desired porter version") + testcases := []struct { + name string + wantVersion string + }{ + {name: "default version", wantVersion: DefaultPorterVersion}, + {name: "custom version", wantVersion: "v1.0.0-alpha.10"}, } - t.Run("EnsurePorter - DefaultVersion", func(t *testing.T) { - check(DefaultPorterVersion) - }) + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + tmp, err := ioutil.TempDir("", "magefiles") + require.NoError(t, err) + defer os.RemoveAll(tmp) - t.Run("EnsurePorterAt - CustomVersion", func(t *testing.T) { - check("v1.0.0-alpha.1") - }) + UsePorterHome(tmp) + EnsurePorterAt(tc.wantVersion) + require.FileExists(t, filepath.Join(tmp, "porter"+xplat.FileExt()), "expected the porter client to be in bin") + assert.FileExists(t, filepath.Join(tmp, "runtimes", "porter-runtime"), "expected the porter runtime to be in bin") + + ok, err := pkg.IsCommandAvailable("porter", tc.wantVersion, "--version") + require.NoError(t, err) + assert.True(t, ok, "could not resolve the desired porter version") + }) + } }