8000 Add unit test to check edges · coder/coder@ec5604e · GitHub
[go: up one dir, main page]

Skip to content

Commit ec5604e

Browse files
committed
Add unit test to check edges
1 parent 7b2844d commit ec5604e

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

provisionersdk/archive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func dirHasExt(dir string, ext string) (bool, error) {
3434

3535
// Tar archives a Terraform directory.
3636
func Tar(w io.Writer, directory string, limit int64) error {
37-
// The total bytes written must be under the limit.
38-
w = xio.NewLimitWriter(w, limit)
37+
// The total bytes written must be under the limit, so use -1
38+
w = xio.NewLimitWriter(w, limit-1)
3939
tarWriter := tar.NewWriter(w)
4040

4141
const tfExt = ".tf"

provisionersdk/archive_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ import (
1515

1616
func TestTar(t *testing.T) {
1717
t.Parallel()
18+
t.Run("HeaderBreakLimit", func(t *testing.T) {
19+
t.Parallel()
20+
dir := t.TempDir()
21+
file, err := os.CreateTemp(dir, "*.tf")
22+
require.NoError(t, err)
23+
_ = file.Close()
24+
// A header is 512 bytes
25+
err = provisionersdk.Tar(io.Discard, dir, 100)
26+
require.Error(t, err)
27+
})
28+
t.Run("HeaderAndContent", func(t *testing.T) {
29+
t.Parallel()
30+
dir := t.TempDir()
31+
file, err := os.CreateTemp(dir, "*.tf")
32+
require.NoError(t, err)
33+
_, _ = file.Write(make([]byte, 100))
34+
_ = file.Close()
35+
// Pay + header is 1024 bytes (padding)
36+
err = provisionersdk.Tar(io.Discard, dir, 1025)
37+
require.NoError(t, err)
38+
39+
// Limit is 1 byte too small (n == limit is a failure, must be under)
40+
err = provisionersdk.Tar(io.Discard, dir, 1024)
41+
require.Error(t, err)
42+
})
43+
1844
t.Run("NoTF", func(t *testing.T) {
1945
t.Parallel()
2046
dir := t.TempDir()
@@ -97,7 +123,8 @@ func TestTar(t *testing.T) {
97123
}
98124
}
99125
archive := new(bytes.Buffer)
100-
err := provisionersdk.Tar(archive, dir, 1024)
126+
// Headers are chonky so raise the limit to something reasonable
127+
err := provisionersdk.Tar(archive, dir, 1024<<2)
101128
require.NoError(t, err)
102129
dir = t.TempDir()
103130
err = provisionersdk.Untar(dir, archive)

0 commit comments

Comments
 (0)
0