/
module_test.go
164 lines (132 loc) · 3.48 KB
/
module_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
package profile
import (
"os"
"runtime"
"strings"
"testing"
"github.com/lmorg/murex/shell/autocomplete"
"github.com/lmorg/murex/test/count"
)
func testCreateModuleStruct() (posix, plan9, windows Module, err error) {
var pwd string
pwd, err = os.Getwd()
if err != nil {
return
}
if runtime.GOOS == "windows" {
pwd = strings.ReplaceAll(pwd, `\`, "/")
}
// this is a terrible kludge!
source := "../../../../../../../../../../.." + pwd + "/module_test.mx"
posix = Module{
Name: "test-module-posix",
Summary: "A test module",
Version: "1.0",
Source: source,
Package: "/..",
Disabled: false,
Dependencies: Dependencies{
Optional: []string{"foo", "bar"},
Required: []string{"sh"},
Platform: []string{"posix"},
},
}
plan9 = Module{
Name: "test-module-plan9",
Summary: "A test module",
Version: "1.0",
Source: source,
Package: "/..",
Disabled: false,
Dependencies: Dependencies{
Optional: []string{"foo", "bar"},
Required: []string{"rc"},
Platform: []string{"plan9"},
},
}
windows = Module{
Name: "test-module-windows",
Summary: "A test module",
Version: "1.0",
Source: source,
Package: "/..",
Disabled: false,
Dependencies: Dependencies{
Optional: []string{"foo", "bar"},
Required: []string{"cmd.exe"},
Platform: []string{"windows"},
},
}
return
}
func TestIsDisabled(t *testing.T) {
count.Tests(t, 3)
disabled = []string{
"foo",
"bar",
}
test := isDisabled("test")
foo := isDisabled("foo")
bar := isDisabled("bar")
if test {
t.Errorf("isDisabled true for value not in []string")
}
if !foo || !bar {
t.Errorf("isDisabled false for value in []string")
}
}
func TestPath(t *testing.T) {
m, _, _, err := testCreateModuleStruct()
if err != nil {
t.Skipf("Unable to get current working directory: %s", err)
}
count.Tests(t, 2)
path := m.Path()
if strings.Contains(path, `\`) {
t.Errorf("`\\` found if path (this could break Windows compatibility): %s", path)
t.Log("(for the sake of standardization, all paths in Murex should be separated by `/` - even on Windows)")
}
if strings.Contains(path, "//") {
t.Errorf("Possible missing path value in %s", path)
}
}
func TestValidate(t *testing.T) {
posix, plan9, windows, err := testCreateModuleStruct()
if err != nil {
t.Skipf("Unable to get current working directory: %s", err)
}
if runtime.GOOS == "windows" {
t.Fatal("This cannot be tested on Windows because drive letter prefixes")
}
_, err = os.Stat(posix.Path())
if err != nil {
t.Skip("Unable to stat path. Skipping this test until murex is run for the first time")
}
count.Tests(t, 6)
autocomplete.GlobalExes = map[string]bool{
"sh": true,
"rc": true,
"cmd.exe": true,
}
errPosix := posix.validate()
errPlan9 := plan9.validate()
errWindows := windows.validate()
if runtime.GOOS != "plan9" && runtime.GOOS != "windows" && errPosix != nil {
t.Errorf("Failed to validate: %s", err)
}
if runtime.GOOS == "plan9" && errPlan9 != nil {
t.Errorf("Failed to validate: %s", err)
}
if runtime.GOOS == "windows" && errWindows != nil {
t.Errorf("Failed to validate: %s", err)
}
if (runtime.GOOS == "plan9" || runtime.GOOS == "windows") && errPosix == nil {
t.Errorf("posix dependency ignored on non-posix OS")
}
if runtime.GOOS != "plan9" && errPlan9 == nil {
t.Errorf("plan9 dependency ignored on non-plan9 OS")
}
if runtime.GOOS != "windows" && errWindows == nil {
t.Errorf("windows dependency ignored on non-windows OS")
}
}