Debian Codegraph @

tar_test.go

raw
1 //go:build !windows
2
3 package desync
4
5 import (
6 "bytes"
7 "context"
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14 )
15
16 func TestTar(t *testing.T) {
17 // First make a tempdir and create a few dirs and files in it
18 base := t.TempDir()
19
20 dirs := []string{
21 "dir1/sub11",
22 "dir1/sub12",
23 "dir2/sub21",
24 "dir2/sub22",
25 }
26 for _, d := range dirs {
27 require.NoError(t, os.MkdirAll(filepath.Join(base, d), 0755))
28 }
29
30 files := []string{
31 "dir1/sub11/f11",
32 "dir1/sub11/f12",
33 }
34 for i, name := range files {
35 os.WriteFile(filepath.Join(base, name), fmt.Appendf(nil, "filecontent%d", i), 0644)
36 }
37
38 require.NoError(t, os.Symlink("dir1", filepath.Join(base, "symlink")))
39
40 // Encode it all into a buffer
41 fs := NewLocalFS(base, LocalFSOptions{})
42 b := new(bytes.Buffer)
43 require.NoError(t, Tar(context.Background(), b, fs))
44
45 // Decode it again
46 d := NewFormatDecoder(b)
47
48 // Define an array of what is expected in the test file
49 expected := []any{
50 FormatEntry{},
51 FormatFilename{}, // "dir1"
52 FormatEntry{},
53 FormatFilename{}, // "sub11"
54 FormatEntry{},
55 FormatFilename{}, // "f11"
56 FormatEntry{},
57 FormatPayload{},
58 FormatFilename{}, // "f12"
59 FormatEntry{},
60 FormatPayload{},
61 FormatGoodbye{},
62 FormatFilename{}, // "sub12"
63 FormatEntry{},
64 FormatGoodbye{},
65 FormatGoodbye{},
66 FormatFilename{}, // "dir2"
67 FormatEntry{},
68 FormatFilename{}, // "sub21"
69 FormatEntry{},
70 FormatGoodbye{},
71 FormatFilename{}, // "sub22"
72 FormatEntry{},
73 FormatGoodbye{},
74 FormatGoodbye{},
75 FormatFilename{}, // "symlink"
76 FormatEntry{},
77 FormatSymlink{},
78 FormatGoodbye{},
79 nil,
80 }
81
82 for _, exp := range expected {
83 v, err := d.Next()
84 require.NoError(t, err)
85 require.IsType(t, exp, v)
86 }
87 }