-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrobustness_test.go
More file actions
75 lines (64 loc) · 2.51 KB
/
Copy pathrobustness_test.go
File metadata and controls
75 lines (64 loc) · 2.51 KB
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
package msix
import (
"bytes"
"context"
"errors"
"io"
"os"
"strings"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestBuild_TempFilesCleanedUp verifies both the reader spill (from AddFileFromReader)
// and the compressed spills are removed from the temp dir once Build returns.
func TestBuild_TempFilesCleanedUp(t *testing.T) {
dir := t.TempDir()
b := goldenBaseBuilder()
b.(*builder).tempDir = dir
b.AddFileFromReader("data.bin", strings.NewReader("hello reader payload"))
b.AddFileFromBytes("App.exe", []byte("MZ"))
var buf bytes.Buffer
require.NoError(t, b.Build(context.Background(), &buf))
entries, err := os.ReadDir(dir)
require.NoError(t, err)
assert.Empty(t, entries, "temp dir should be empty after Build (reader + compressed spills removed)")
}
// TestBuild_SourceOpenedOncePerBuild verifies a deflate payload source is read
// exactly once (compressed to a spill, which is then replayed for output/digests).
func TestBuild_SourceOpenedOncePerBuild(t *testing.T) {
var opens int64
b := goldenBaseBuilder().AddFileSource("big.dat", func() (io.ReadCloser, error) {
atomic.AddInt64(&opens, 1)
return io.NopCloser(strings.NewReader("payload data that will be deflated to a spill")), nil
})
var buf bytes.Buffer
require.NoError(t, b.Build(context.Background(), &buf))
assert.Equal(t, int64(1), atomic.LoadInt64(&opens),
"deflate source must be read exactly once; the compressed spill is replayed thereafter")
}
// TestBuild_ValidationErrorsAggregated verifies required-field validation reports all
// problems at once via errors.Join.
func TestBuild_ValidationErrorsAggregated(t *testing.T) {
err := NewBuilder().
AddApplication(NewApplication().Build()).
Build(context.Background(), io.Discard)
require.Error(t, err)
msg := err.Error()
assert.Contains(t, msg, "identity name is required")
assert.Contains(t, msg, "identity version is required")
assert.Contains(t, msg, "identity publisher is required")
assert.Contains(t, msg, "application[0] id is required")
}
type errReader struct{}
func (errReader) Read([]byte) (int, error) { return 0, errors.New("boom") }
// TestBuild_ReaderErrorSurfacedAtBuild verifies a failing AddFileFromReader source is
// reported (deferred) from Build, naming the package path.
func TestBuild_ReaderErrorSurfacedAtBuild(t *testing.T) {
err := goldenBaseBuilder().
AddFileFromReader("bad.bin", errReader{}).
Build(context.Background(), io.Discard)
require.Error(t, err)
assert.Contains(t, err.Error(), "bad.bin")
}