-
Notifications
You must be signed in to change notification settings - Fork 0
/
filepath_test.go
182 lines (167 loc) · 5.16 KB
/
filepath_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"fmt"
"log"
"os"
"reflect"
"testing"
)
func TestParseArgs(t *testing.T) {
//Simpler list splitter for now
t.Parallel()
var tests = []struct {
args []string
inputs []string
output string
wantErr error
}{
{[]string{"1.hex", "2.hex", "3.hex"}, []string{"1.hex", "2.hex"}, "3.hex", nil},
{[]string{"1.hex"}, []string{}, "", fmt.Errorf("not enough files specified")},
}
for _, tt := range tests {
testname := fmt.Sprintf("%v", tt.args)
t.Run(testname, func(t *testing.T) {
inputs, output, err := parseArgs(tt.args)
if !reflect.DeepEqual(inputs, tt.inputs) {
t.Errorf("got %v, want %v", inputs, tt.inputs)
}
if output != tt.output {
t.Errorf("got %v, want %v", output, tt.output)
}
if err != tt.wantErr {
if err != nil && tt.wantErr != nil {
if err.Error() != tt.wantErr.Error() {
t.Errorf("got %v, want %v", err, tt.wantErr)
}
} else {
t.Errorf("got %v, want %v", err, tt.wantErr)
}
}
})
}
}
func TestValidateFiles(t *testing.T) {
//Testing input file that exists, one that doesnt
//Output file that does, output file that doesnt
file_exists_hex, err := os.CreateTemp("", "test_*_.hex")
if err != nil {
t.Error(err)
}
file_exists_hex.Close()
defer os.Remove(file_exists_hex.Name())
file_exists_bin, err := os.CreateTemp("", "test_*_.bin")
if err != nil {
t.Error(err)
}
file_exists_bin.Close()
defer os.Remove(file_exists_bin.Name())
file_exists_bad, err := os.CreateTemp("", "test_*_.bad")
if err != nil {
t.Error(err)
}
file_exists_bad.Close()
defer os.Remove(file_exists_bad.Name())
//Basic case, both files exist and should pass
err = validateFiles([]string{file_exists_bin.Name(), file_exists_hex.Name()}, "nope.bin")
if err != nil {
t.Error(err)
}
//Test non existing input file
err = validateFiles([]string{file_exists_bin.Name(), file_exists_hex.Name(), "nothere.bin"}, "nope.bin")
if err == nil {
t.Errorf("Should raise error on input file that doesnt exist")
}
err = validateFiles([]string{file_exists_bin.Name(), file_exists_hex.Name(), file_exists_bad.Name()}, "nope.bin")
if err == nil {
t.Errorf("Should raise error on bad file name even if it exists")
}
//Testing bad output files
err = validateFiles([]string{file_exists_bin.Name(), file_exists_hex.Name()}, "nope.lol")
if err == nil {
t.Errorf("Should raise error on output file of unknown type")
}
//Testing output file exists case, should prompt asking for confirmation of overwrite
tmpfile, err := os.CreateTemp("", "mockstdin")
if err != nil {
t.Error(err)
}
defer os.Remove(tmpfile.Name()) // clean up
content := []byte("y\r\n")
if _, err := tmpfile.Write(content); err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Seek(0, 0); err != nil {
log.Fatal(err)
}
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }() // Restore original Stdin at end of test
os.Stdin = tmpfile
err = validateFiles([]string{file_exists_hex.Name()}, file_exists_bin.Name())
if err != nil {
t.Errorf("Should allow user to confirm overwrite")
}
content = []byte("n\r\n")
if _, err := tmpfile.Seek(0, 0); err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Write(content); err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Seek(0, 0); err != nil {
log.Fatal(err)
}
err = validateFiles([]string{file_exists_hex.Name()}, file_exists_bin.Name())
if err == nil {
t.Errorf("Should raise error if user does not acknowledge overwrite")
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
}
func TestParseFileTypeAndStart(t *testing.T) {
//testing that it handles basic bin and hex files correctly
t.Parallel()
var tests = []struct {
path string
wantbasePath string
wantHex bool
wantN uint32
wantErr error
}{
{"test.hex", "test.hex", true, 0, nil},
{"test.bin", "test.bin", false, 0, nil},
{"test.bin:1024", "test.bin", false, 1024, nil},
{"test.bin:0x1024", "test.bin", false, 0x1024, nil},
{"test.bin:0b111", "test.bin", false, 7, nil},
{"test.bin:0b1011", "test.bin", false, 11, nil},
{"test.bad:0b1011", "test.bad", false, 0, fmt.Errorf("could not parse file type from test.bad:0b1011")},
{"test.bad:1024", "test.bad", false, 0, fmt.Errorf("could not parse file type from test.bad:1024")},
{"test.bad:0x1024", "test.bad", false, 0, fmt.Errorf("could not parse file type from test.bad:0x1024")},
{"test.bad", "test.bad", false, 0, fmt.Errorf("could not parse file type from test.bad")},
{"test.bin:x", "test.bin", false, 0, fmt.Errorf("could not parse file type from test.bin:x")},
}
for _, tt := range tests {
testname := tt.path
t.Run(testname, func(t *testing.T) {
isHex, n, basePath, err := parseFileTypeAndStart(tt.path)
if isHex != tt.wantHex {
t.Errorf("got %v, want %v", isHex, tt.wantHex)
}
if n != tt.wantN {
t.Errorf("got %v, want %v", n, tt.wantN)
}
if basePath != tt.wantbasePath {
t.Errorf("got %v, want %v", basePath, tt.wantbasePath)
}
if err != tt.wantErr {
if err != nil && tt.wantErr != nil {
if err.Error() != tt.wantErr.Error() {
t.Errorf("got %v, want %v", err, tt.wantErr)
}
} else {
t.Errorf("got %v, want %v", err, tt.wantErr)
}
}
})
}
}