]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | "github.com/stretchr/testify/assert" | |
6 | "os" | |
7 | "testing" | |
8 | ) | |
9 | ||
10 | func TestNewFlattenedFileObject(t *testing.T) { | |
11 | type args struct { | |
12 | fileRoot string | |
13 | filePath []byte | |
14 | fileName []byte | |
15 | } | |
16 | tests := []struct { | |
17 | name string | |
18 | args args | |
19 | want *flattenedFileObject | |
20 | wantErr assert.ErrorAssertionFunc | |
21 | }{ | |
22 | { | |
23 | name: "with valid file", | |
24 | args: args{ | |
25 | fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), | |
26 | fileName: []byte("testfile.txt"), | |
27 | filePath: []byte{0, 0}, | |
28 | }, | |
29 | want: &flattenedFileObject{ | |
30 | FlatFileHeader: NewFlatFileHeader(), | |
31 | FlatFileInformationForkHeader: FlatFileInformationForkHeader{}, | |
32 | FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8)), | |
33 | FlatFileDataForkHeader: FlatFileDataForkHeader{ | |
34 | ForkType: [4]byte{0x4d, 0x41, 0x43, 0x52}, // DATA | |
35 | CompressionType: [4]byte{0, 0, 0, 0}, | |
36 | RSVD: [4]byte{0, 0, 0, 0}, | |
37 | DataSize: [4]byte{0x00, 0x00, 0x00, 0x17}, | |
38 | }, | |
39 | FileData: nil, | |
40 | }, | |
41 | wantErr: assert.NoError, | |
42 | }, | |
43 | { | |
44 | name: "when file path is invalid", | |
45 | args: args{ | |
46 | fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), | |
47 | fileName: []byte("nope.txt"), | |
48 | }, | |
49 | want: nil, | |
50 | wantErr: assert.Error, | |
51 | }, | |
52 | } | |
53 | for _, tt := range tests { | |
54 | t.Run(tt.name, func(t *testing.T) { | |
55 | got, err := NewFlattenedFileObject(tt.args.fileRoot, tt.args.filePath, tt.args.fileName) | |
56 | if tt.wantErr(t, err, fmt.Sprintf("NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)) { | |
57 | return | |
58 | } | |
59 | ||
60 | // Clear the file timestamp fields to work around problems running the tests in multiple timezones | |
61 | // TODO: revisit how to test this by mocking the stat calls | |
62 | got.FlatFileInformationFork.CreateDate = make([]byte, 8) | |
63 | got.FlatFileInformationFork.ModifyDate = make([]byte, 8) | |
64 | assert.Equalf(t, tt.want, got, "NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName) | |
65 | }) | |
66 | } | |
67 | } |