-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
81 lines (72 loc) · 1.65 KB
/
scanner_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
package main
import (
"reflect"
"strings"
"testing"
)
// mockReadCloser is a helper type for mocking io.ReadCloser
type mockReadCloser struct {
reader *strings.Reader
}
func newMockReadCloser(content string) *mockReadCloser {
return &mockReadCloser{reader: strings.NewReader(content)}
}
func (mrc *mockReadCloser) Read(p []byte) (n int, err error) {
return mrc.reader.Read(p)
}
func (mrc *mockReadCloser) Close() error {
return nil
}
// TestParseProcfile tests the parseProcfile function with various procfile contents.
func TestParseProcfile(t *testing.T) {
tests := []struct {
name string
content string
want []entry
wantErr bool
}{
{
name: "Valid procfile",
content: "web: npm start\nworker: npm run worker",
want: []entry{
{name: "web", cmd: "npm start"},
{name: "worker", cmd: "npm run worker"},
},
wantErr: false,
},
{
name: "Empty procfile",
content: "",
want: nil,
wantErr: true,
},
{
name: "Invalid format",
content: "web npm start",
want: nil,
wantErr: true,
},
{
name: "Duplicate names",
content: "web: npm start\nweb: npm run something",
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock the file reading
mockFile := newMockReadCloser(tt.content)
got, err := parseProcfile(mockFile)
// Check for expected error
if (err != nil) != tt.wantErr {
t.Errorf("parseProcfile() error = %v, wantErr %v", err, tt.wantErr)
return
}
// Check for expected result
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseProcfile() = %v, want %v", got, tt.want)
}
})
}
}