diff --git a/pkg/document/bundle_test.go b/pkg/document/bundle_test.go index 98854f04f..a1da66f78 100644 --- a/pkg/document/bundle_test.go +++ b/pkg/document/bundle_test.go @@ -9,18 +9,12 @@ import ( "sigs.k8s.io/kustomize/v3/pkg/gvk" "sigs.k8s.io/kustomize/v3/pkg/types" - "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/testutil" ) func TestNewBundle(t *testing.T) { - fSys := testutil.SetupTestFs(t, "testdata") - bundle, err := document.NewBundle(fSys, "/", "/") - if err != nil { - t.Fatalf("Building Bundle Failed: %v", err) - } - + bundle := testutil.NewTestBundle(t, "testdata") require := require.New(t) require.NotNil(bundle) @@ -28,12 +22,7 @@ func TestNewBundle(t *testing.T) { func TestBundleDocumentFiltering(t *testing.T) { - fSys := testutil.SetupTestFs(t, "testdata") - bundle, err := document.NewBundle(fSys, "/", "/") - if err != nil { - t.Fatalf("Building Bundle Failed: %v", err) - } - + bundle := testutil.NewTestBundle(t, "testdata") assert := assert.New(t) t.Run("GetKustomizeResourceMap", func(t *testing.T) { diff --git a/testutil/testdatafs.go b/testutil/testdatafs.go index 571cf3678..7a93121f4 100644 --- a/testutil/testdatafs.go +++ b/testutil/testdatafs.go @@ -5,7 +5,10 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/require" "sigs.k8s.io/kustomize/v3/pkg/fs" + + "opendev.org/airship/airshipctl/pkg/document" ) // SetupTestFs help manufacture a fake file system for testing purposes. It @@ -13,26 +16,28 @@ import ( // to the tests themselves, and will write each of those files (preserving // names) to an in-memory file system and return that fs func SetupTestFs(t *testing.T, fixtureDir string) fs.FileSystem { + t.Helper() x := fs.MakeFakeFS() files, err := ioutil.ReadDir(fixtureDir) - if err != nil { - t.Fatalf("Unable to read fixture directory %s: %v", fixtureDir, err) - } + require.NoError(t, err, "Failed to read fixture directory, setting up testfs failed") for _, file := range files { fileName := file.Name() filePath := filepath.Join(fixtureDir, fileName) fileBytes, err := ioutil.ReadFile(filePath) - if err != nil { - t.Fatalf("Error reading fixture %s: %v", filePath, err) - } - // nolint: errcheck + require.NoError(t, err, "Failed to read file, setting up testfs failed") err = x.WriteFile(filepath.Join("/", file.Name()), fileBytes) - if err != nil { - t.Fatalf("Error writing fixture %s: %v", filePath, err) - } + require.NoError(t, err, "Failed to write file, setting up testfs failed") } return x } + +// NewTestBundle helps to create a new bundle with FakeFs containing documents from fixtureDir +func NewTestBundle(t *testing.T, fixtureDir string) document.Bundle { + t.Helper() + b, err := document.NewBundle(SetupTestFs(t, fixtureDir), "/", "/") + require.NoError(t, err, "Failed to build a bundle, setting up TestBundle failed") + return b +}