]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
92a7e455 JH |
4 | "fmt" |
5 | "github.com/stretchr/testify/assert" | |
6 | "os" | |
6988a057 JH |
7 | "testing" |
8 | ) | |
9 | ||
72dd37f1 JH |
10 | func TestNewFlattenedFileObject(t *testing.T) { |
11 | type args struct { | |
92a7e455 JH |
12 | fileRoot string |
13 | filePath []byte | |
14 | fileName []byte | |
72dd37f1 JH |
15 | } |
16 | tests := []struct { | |
17 | name string | |
18 | args args | |
19 | want *flattenedFileObject | |
92a7e455 | 20 | wantErr assert.ErrorAssertionFunc |
72dd37f1 JH |
21 | }{ |
22 | { | |
92a7e455 | 23 | name: "with valid file", |
72dd37f1 | 24 | args: args{ |
92a7e455 JH |
25 | fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), |
26 | fileName: []byte("testfile.txt"), | |
27 | filePath: []byte{0, 0}, | |
72dd37f1 JH |
28 | }, |
29 | want: &flattenedFileObject{ | |
30 | FlatFileHeader: NewFlatFileHeader(), | |
31 | FlatFileInformationForkHeader: FlatFileInformationForkHeader{}, | |
29f329ae | 32 | FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8)), |
92a7e455 | 33 | FlatFileDataForkHeader: FlatFileDataForkHeader{ |
85767504 JH |
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}, | |
72dd37f1 | 38 | }, |
92a7e455 | 39 | FileData: nil, |
72dd37f1 | 40 | }, |
92a7e455 | 41 | wantErr: assert.NoError, |
72dd37f1 JH |
42 | }, |
43 | { | |
44 | name: "when file path is invalid", | |
45 | args: args{ | |
92a7e455 JH |
46 | fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), |
47 | fileName: []byte("nope.txt"), | |
72dd37f1 | 48 | }, |
92a7e455 JH |
49 | want: nil, |
50 | wantErr: assert.Error, | |
72dd37f1 JH |
51 | }, |
52 | } | |
53 | for _, tt := range tests { | |
54 | t.Run(tt.name, func(t *testing.T) { | |
16a4ad70 | 55 | got, err := NewFlattenedFileObject(tt.args.fileRoot, tt.args.filePath, tt.args.fileName, 0) |
29f329ae | 56 | if tt.wantErr(t, err, fmt.Sprintf("NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)) { |
72dd37f1 JH |
57 | return |
58 | } | |
29f329ae JH |
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) | |
92a7e455 | 64 | assert.Equalf(t, tt.want, got, "NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName) |
72dd37f1 JH |
65 | }) |
66 | } | |
92a7e455 | 67 | } |