-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbagit_test.go
151 lines (111 loc) · 3.23 KB
/
bagit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package bagit_test
import (
"errors"
"testing"
"github.com/artefactual-labs/bagit-gython"
"golang.org/x/sync/errgroup"
"gotest.tools/v3/assert"
"gotest.tools/v3/fs"
)
func setUp(tb testing.TB) *bagit.BagIt {
tb.Helper()
b, err := bagit.NewBagIt()
assert.NilError(tb, err)
tb.Cleanup(func() {
assert.NilError(tb, b.Cleanup())
})
return b
}
func BenchmarkValidate(b *testing.B) {
bagit := setUp(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = bagit.Validate("internal/testdata/valid-bag")
}
}
func TestConcurrency(t *testing.T) {
t.Parallel()
t.Run("Validates bag", func(t *testing.T) {
t.Parallel()
b := setUp(t)
err := b.Validate("internal/testdata/valid-bag")
assert.NilError(t, err)
})
t.Run("Returns ErrBusy if the resource is busy", func(t *testing.T) {
t.Parallel()
b := setUp(t)
// This test should pass because each call to Validate() creates its own
// distinct Python interpreter instance.
var g errgroup.Group
for i := 0; i < 3; i++ {
g.Go(func() error {
return b.Validate("internal/testdata/valid-bag")
})
}
err := g.Wait()
assert.ErrorIs(t, err, bagit.ErrBusy)
})
t.Run("Parallel execution", func(t *testing.T) {
t.Parallel()
// *bagit.BagIt is not shareable, each goroutine must create its own.
var g errgroup.Group
for i := 0; i < 3; i++ {
g.Go(func() error {
b := setUp(t)
return b.Validate("internal/testdata/valid-bag")
})
}
err := g.Wait()
assert.NilError(t, err)
})
}
func TestValidateBag(t *testing.T) {
t.Parallel()
t.Run("Fails validation", func(t *testing.T) {
t.Parallel()
b := setUp(t)
err := b.Validate("/tmp/691b8e7f-e6b7-41dd-bc47-868e2ff69333")
assert.Error(t, err, "invalid: Expected bagit.txt does not exist: /tmp/691b8e7f-e6b7-41dd-bc47-868e2ff69333/bagit.txt")
assert.Assert(t, errors.Is(err, bagit.ErrInvalid))
})
t.Run("Validates bag", func(t *testing.T) {
t.Parallel()
b := setUp(t)
err := b.Validate("internal/testdata/valid-bag")
assert.NilError(t, err)
})
}
func TestMakeBag(t *testing.T) {
t.Parallel()
t.Run("Creates bag", func(t *testing.T) {
t.Parallel()
tmpDir := fs.NewDir(t, "", fs.WithFile("test.txt", "abcd"))
b := setUp(t)
err := b.Make(tmpDir.Path())
assert.NilError(t, err)
assert.Assert(t, fs.Equal(tmpDir.Path(), fs.Expected(t,
fs.WithDir("data", fs.WithFile("test.txt", "abcd"), fs.MatchAnyFileMode),
fs.WithFile("bagit.txt", `BagIt-Version: 0.97
Tag-File-Character-Encoding: UTF-8
`, fs.MatchAnyFileMode),
fs.WithFile("bag-info.txt", "", fs.MatchAnyFileContent, fs.MatchAnyFileMode),
fs.WithFile("manifest-sha256.txt", "", fs.MatchAnyFileContent, fs.MatchAnyFileMode),
fs.WithFile("manifest-sha512.txt", "", fs.MatchAnyFileContent, fs.MatchAnyFileMode),
fs.WithFile("tagmanifest-sha256.txt", "", fs.MatchAnyFileContent, fs.MatchAnyFileMode),
fs.WithFile("tagmanifest-sha512.txt", "", fs.MatchAnyFileContent, fs.MatchAnyFileMode),
)))
})
t.Run("Reports creation failures", func(t *testing.T) {
t.Parallel()
b := setUp(t)
err := b.Make("non-existent-dir")
assert.ErrorContains(t, err, "does not exist")
})
}
func TestCleanup(t *testing.T) {
t.Parallel()
b, err := bagit.NewBagIt()
assert.NilError(t, err)
err = b.Cleanup()
assert.NilError(t, err)
}