-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuunit.nu
executable file
·138 lines (127 loc) · 2.71 KB
/
nuunit.nu
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
#!/usr/bin/env nu
export def main [
--test-spec-module-name = "test-spec.nu"
--as-json
] {
if (is-not-valid-test-spec $test_spec_module_name) {
return $"Invalid test spec module: ($test_spec_module_name)"
}
let module = $test_spec_module_name | split row '/' | last | str replace '.nu' ''
let importScript = $"use ($test_spec_module_name) *"
let testResults = run-test-discoverer $module $importScript
| run-tests
$testResults
| output-tests $as_json
| print
$testResults
| get exit_code
| sort --reverse
| get -i 0
| default 0
| exit $in
}
def is-not-valid-test-spec [spec] {
not ($spec | path exists)
}
def run-test-discoverer [module testImportScript] {
run-nushell [
--commands
$"($testImportScript)
(inline-source-code discover-tests)
discover-tests '($module)' '($testImportScript)'
| to nuon"
]
| from nuon
}
def inline-source-code [command] {
try {
view source $command
} catch {
view source $"nuunit ($command)"
| str replace 'nuunit ' ''
}
}
export def discover-tests [module testImportScript] {
scope modules
| where name == $module
| get commands
| flatten
| where name =~ '^test'
| get name
| enumerate
| each {|it|
{
id: ($it.index + 1)
name: $it.item
exec: $"($testImportScript)
try {
if \(scope commands | where name == 'before each' | is-empty) {
null
} else {
before each
}
| ($it.item)
| if \(scope commands | where name == 'after each' | is-empty) {
null
} else {
after each
}
} catch {|err|
print -e $err.debug
exit 1
}"
}
}
}
def run-tests [] {
$in
| par-each {|test|
do {
run-nushell [
--commands
$test.exec
]
}
| complete
| insert id $test.id
| insert name $test.name
}
}
def output-tests [asJson] {
let tests = $in
if ($asJson) {
$tests | to json
} else {
$tests | to tap
}
}
def "to tap" [] {
$in
| each {|testResult|
if $testResult.exit_code == 0 {
$"ok ($testResult.id) - ($testResult.name)"
} else {
$testResult
| select stdout stderr exit_code
| to yaml
| lines
| each {" " ++ $in}
| [
$"not ok ($testResult.id) - ($testResult.name)"
" ---"
...$in
" ..."
]
| str join (char newline)
}
}
| [
"TAP version 14"
$"1..($in | length)"
...$in
]
| str join (char newline)
}
def run-nushell [params: list] {
^$nu.current-exe --no-config-file ...$params
}