Skip to content

Commit

Permalink
Move UseFilesystem onto TestContext
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs-msft committed Jun 15, 2020
1 parent 487403a commit b01885a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
25 changes: 24 additions & 1 deletion pkg/context/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion pkg/porter/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
24 changes: 3 additions & 21 deletions pkg/porter/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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()

Expand All @@ -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
}
Expand All @@ -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)
}
Expand Down

0 comments on commit b01885a

Please sign in to comment.