-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzip_test.go
37 lines (33 loc) · 1.25 KB
/
zip_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
package modmake
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)
func TestZipArchive_Extract(t *testing.T) {
tmp, err := os.MkdirTemp("", "ZipArchive_Extract-*")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tmp))
}()
inputPath := Path(tmp, "input.txt")
require.NoError(t, os.WriteFile(inputPath.String(), []byte("A test file"), 0644))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
zipPath := Path(tmp, "test.zip")
err = Zip(zipPath).AddFileWithPath(inputPath, "input.txt").Create().Run(ctx)
require.NoError(t, err)
require.NoError(t, os.Remove(inputPath.String()))
require.NoError(t, os.WriteFile(inputPath.String(), []byte("A new message"), 0644))
require.NoError(t, Zip(zipPath).AddFileWithPath(inputPath, "input.txt").Update().Run(ctx))
assert.NoError(t, Zip(zipPath).Extract(Path(tmp)).Run(ctx), "Failed to extract directory")
fi, err := os.Stat(inputPath.String())
assert.NoError(t, err, "Failed to stat the extracted file")
assert.False(t, fi.IsDir(), "Should be a file, not a directory")
data, err := os.ReadFile(inputPath.String())
assert.NoError(t, err)
assert.Equal(t, "A new message", string(data))
}