Skip to content

Instantly share code, notes, and snippets.

@sevein
Created April 25, 2023 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevein/5440495a5f4ee397e2d00fdd5fcfa9d9 to your computer and use it in GitHub Desktop.
Save sevein/5440495a5f4ee397e2d00fdd5fcfa9d9 to your computer and use it in GitHub Desktop.
Testdata wrapper with mutator function
diff --git a/pkg/transfer/bag_validation_test.go b/pkg/transfer/bag_validation_test.go
index 578f0b29..8f48247e 100644
--- a/pkg/transfer/bag_validation_test.go
+++ b/pkg/transfer/bag_validation_test.go
@@ -2,20 +2,21 @@ package transfer_test
import (
"os"
+ "path/filepath"
"testing"
"gotest.tools/v3/assert"
- tfs "gotest.tools/v3/fs"
"gitlab.artefactual.com/clients/ste/sdps_preprocessing/pkg/transfer"
"gitlab.artefactual.com/clients/ste/sdps_preprocessing/pkg/wfdef"
+ "gitlab.artefactual.com/clients/ste/sdps_preprocessing/testdata"
)
func TestBagValidation(t *testing.T) {
t.Parallel()
type params struct {
- transferDir string
+ sample *testdata.SampleTransfer
newTransferErr string
wdf wfdef.Version
}
@@ -30,13 +31,47 @@ func TestBagValidation(t *testing.T) {
}{
"No errors or warnings in transfer": {
params: params{
- transferDir: "../../testdata/9999---no-errors-or-warnings-wfdf-1.0---Bag---IPLv3c/",
- wdf: wfdef.Version("1.0"),
+ sample: testdata.NewSampleTransfer(
+ "../../testdata/9999---no-errors-or-warnings-wfdf-1.0---Bag---IPLv3c", nil,
+ ),
+ wdf: wfdef.Version("1.0"),
},
ex: expected{
transferErrors: transferErrors(t),
},
},
+ "MC error on unexpected sub-directories": {
+ params: params{
+ sample: testdata.NewSampleTransfer(
+ "../../testdata/9999---no-errors-or-warnings-wfdf-1.0---Bag---IPLv3c",
+ func(t *testing.T, path string) {
+ // Add unexpected sub-directories in master and access.
+ dataDir := filepath.Join(path, "no-errors-or-warnings", "data")
+ assert.NilError(t, os.Mkdir(filepath.Join(dataDir, "access", "extra1"), ModeDir))
+ assert.NilError(t, os.Mkdir(filepath.Join(dataDir, "master", "extra2"), ModeDir))
+ },
+ ),
+ wdf: wfdef.Version("1.0"),
+ },
+ ex: expected{
+ transferErrors: &transfer.TransferErrors{
+ Errors: map[transfer.ErrorType][]*transfer.TransferError{
+ transfer.ErrorTypeMandatoryCritical: {
+ {
+ ErrType: transfer.ErrorTypeMandatoryCritical,
+ Message: "there are unexpected folders or files",
+ Details: []string{"file path: no-errors-or-warnings/data/access/extra1"},
+ },
+ {
+ ErrType: transfer.ErrorTypeMandatoryCritical,
+ Message: "there are unexpected folders or files",
+ Details: []string{"file path: no-errors-or-warnings/data/master/extra2"},
+ },
+ },
+ },
+ },
+ },
+ },
}
for name, tc := range testCases {
@@ -45,7 +80,7 @@ func TestBagValidation(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
- tBag, err := transfer.NewBagTransfer(tc.params.transferDir, tc.params.wdf)
+ tBag, err := transfer.NewBagTransfer(tc.params.sample.Path(t), tc.params.wdf)
if tc.params.newTransferErr != "" {
assert.ErrorContains(t, err, tc.params.newTransferErr)
@@ -60,47 +95,4 @@ func TestBagValidation(t *testing.T) {
assert.DeepEqual(t, tc.ex.transferErrors, transferErrors)
})
}
-
- t.Run("MC error on unexpected sub-directories", func(t *testing.T) {
- t.Parallel()
-
- td := tfs.NewDir(t, "test",
- tfs.FromDir(
- "../../testdata/9999---no-errors-or-warnings-wfdf-1.0---Bag---IPLv3c/",
- ),
- )
-
- // Add unexpected sub-directories in master and access.
- for _, d := range []string{
- td.Join("no-errors-or-warnings", "data", "access", "extra1"),
- td.Join("no-errors-or-warnings", "data", "master", "extra2"),
- } {
- if err := os.Mkdir(d, ModeDir); err != nil {
- t.Errorf("add test Bag dir: %v", err)
- }
- }
-
- b, err := transfer.NewBagTransfer(td.Path(), wfdef.Version("1.0"))
- assert.NilError(t, err)
-
- got := transfer.NewValidator()
- b.Validate(got)
-
- assert.DeepEqual(t, got, &transfer.TransferErrors{
- Errors: map[transfer.ErrorType][]*transfer.TransferError{
- transfer.ErrorTypeMandatoryCritical: {
- {
- ErrType: transfer.ErrorTypeMandatoryCritical,
- Message: "there are unexpected folders or files",
- Details: []string{"file path: no-errors-or-warnings/data/access/extra1"},
- },
- {
- ErrType: transfer.ErrorTypeMandatoryCritical,
- Message: "there are unexpected folders or files",
- Details: []string{"file path: no-errors-or-warnings/data/master/extra2"},
- },
- },
- },
- })
- })
}
diff --git a/testdata/testdata.go b/testdata/testdata.go
new file mode 100644
index 00000000..debf4668
--- /dev/null
+++ b/testdata/testdata.go
@@ -0,0 +1,46 @@
+package testdata
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/otiai10/copy"
+ "gotest.tools/v3/assert"
+)
+
+type SampleMutator = func(t *testing.T, path string)
+
+type SampleTransfer struct {
+ name string
+ path string
+ mutator SampleMutator
+}
+
+func NewSampleTransfer(path string, mutator SampleMutator) *SampleTransfer {
+ return &SampleTransfer{
+ name: filepath.Base(path),
+ path: path,
+ mutator: mutator,
+ }
+}
+
+func (st *SampleTransfer) Path(t *testing.T) string {
+ if st.mutator == nil {
+ return st.path
+ }
+
+ tmp, err := os.MkdirTemp("", "test_*")
+ assert.NilError(t, err, "Cannot create temporary directory.")
+
+ // E.g.: /tmp/test_3088682795/9999---no-errors-or-warnings-wfdf-1.0---Bag---IPLv3c.
+ dest := filepath.Join(tmp, st.name)
+
+ err = copy.Copy(st.path, dest)
+ assert.NilError(t, err, "Cannot copy sample transfer into temporary directory.")
+
+ // Run user-provided mutator.
+ st.mutator(t, dest)
+
+ return dest
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment