From b01885ac0e2998049b9a83e6e0af4bd675023486 Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Mon, 15 Jun 2020 17:27:38 -0500 Subject: [PATCH] Move UseFilesystem onto TestContext --- pkg/context/helpers.go | 25 ++++++++++++++++++++++++- pkg/porter/credentials_test.go | 2 +- pkg/porter/helpers.go | 24 +++--------------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/pkg/context/helpers.go b/pkg/context/helpers.go index fd0e49f270..119cefa770 100644 --- a/pkg/context/helpers.go +++ b/pkg/context/helpers.go @@ -13,11 +13,13 @@ import ( "get.porter.sh/porter/pkg/test" "github.com/spf13/afero" + "github.com/stretchr/testify/require" ) type TestContext struct { *Context + cleanupDirs []string capturedErr *bytes.Buffer capturedOut *bytes.Buffer T *testing.T @@ -63,7 +65,28 @@ func NewTestCommand() CommandBuilder { } } -// TODO: Replace these functions with a union file system for test data +// UseFilesystem has porter's context use the OS filesystem instead of an in-memory filesystem +// Returns the temp porter home directory created for the test +func (c *TestContext) UseFilesystem() string { + c.FileSystem = &afero.Afero{Fs: afero.NewOsFs()} + + testDir, err := ioutil.TempDir("/tmp", "porter") + require.NoError(c.T, err) + c.cleanupDirs = append(c.cleanupDirs, testDir) + + return testDir +} + +func (c *TestContext) AddCleanupDir(dir string) { + c.cleanupDirs = append(c.cleanupDirs, dir) +} + +func (c *TestContext) Cleanup() { + for _, dir := range c.cleanupDirs { + c.FileSystem.RemoveAll(dir) + } +} + func (c *TestContext) AddTestFile(src, dest string) []byte { c.T.Helper() diff --git a/pkg/porter/credentials_test.go b/pkg/porter/credentials_test.go index faaaad1c3e..a71eeb361c 100644 --- a/pkg/porter/credentials_test.go +++ b/pkg/porter/credentials_test.go @@ -181,7 +181,7 @@ kool-kreds 2019-06-24`}, func TestGenerateNoCredentialDirectory(t *testing.T) { p := NewTestPorter(t) - home := p.UseFilesystem() + home := p.TestConfig.TestContext.UseFilesystem() p.CreateBundleDir() p.TestConfig.TestContext.CopyFile("testdata/bundle.json", filepath.Join(p.BundleDir, "bundle.json")) diff --git a/pkg/porter/helpers.go b/pkg/porter/helpers.go index 6b12cddfa1..e192b59678 100644 --- a/pkg/porter/helpers.go +++ b/pkg/porter/helpers.go @@ -20,7 +20,6 @@ import ( "get.porter.sh/porter/pkg/secrets" cnabcreds "github.com/cnabio/cnab-go/credentials" "github.com/cnabio/cnab-go/secrets/host" - "github.com/spf13/afero" "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" ) @@ -36,9 +35,6 @@ type TestPorter struct { // directory where the integration test is being executed BundleDir string - - // tempDirectories that need to be cleaned up at the end of the testRun - cleanupDirs []string } // NewTestPorter initializes a porter test client, with the output buffered, and an in-memory file system. @@ -75,7 +71,7 @@ func (p *TestPorter) SetupIntegrationTest() { p.NewCommand = exec.Command p.TestCredentials.SecretsStore = secrets.NewSecretStore(&host.SecretStore{}) - homeDir := p.UseFilesystem() + homeDir := p.TestConfig.TestContext.UseFilesystem() p.TestConfig.SetupIntegrationTest(homeDir) bundleDir := p.CreateBundleDir() @@ -101,24 +97,12 @@ func (p *TestPorter) SetupIntegrationTest() { require.NoError(t, err, "could not save test credentials") } -// UseFilesystem has porter's context use the OS filesystem instead of an in-memory filesystem -// Returns the temp porter home directory created for the test -func (p *TestPorter) UseFilesystem() string { - p.FileSystem = &afero.Afero{Fs: afero.NewOsFs()} - - homeDir, err := ioutil.TempDir("/tmp", "porter") - require.NoError(p.T(), err) - p.cleanupDirs = append(p.cleanupDirs, homeDir) - - return homeDir -} - func (p *TestPorter) CreateBundleDir() string { bundleDir, err := ioutil.TempDir("", "bundle") require.NoError(p.T(), err) p.BundleDir = bundleDir - p.cleanupDirs = append(p.cleanupDirs, p.BundleDir) + p.TestConfig.TestContext.AddCleanupDir(p.BundleDir) return bundleDir } @@ -130,9 +114,7 @@ func (p *TestPorter) T() *testing.T { func (p *TestPorter) CleanupIntegrationTest() { os.Unsetenv(config.EnvHOME) - for _, dir := range p.cleanupDirs { - p.FileSystem.RemoveAll(dir) - } + p.TestConfig.TestContext.Cleanup() os.Chdir(p.TestDir) }