-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtests.nu
More file actions
172 lines (147 loc) · 4.95 KB
/
tests.nu
File metadata and controls
172 lines (147 loc) · 4.95 KB
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
#!/usr/bin/env nu
use std assert
const NU_VERSION = '0.105.0'
def main [] {
let test_plan = (
scope commands
| where ($it.type == "custom")
and ($it.name | str starts-with "test ")
and not ($it.description | str starts-with "ignore")
| each { |test| create_execution_plan $test.name }
| str join ", "
)
let plan = $"run_tests [ ($test_plan) ]"
^$nu.current-exe --commands $"source ($env.CURRENT_FILE); ($plan)"
}
def create_execution_plan [test: string]: nothing -> string {
$"{ name: \"($test)\", execute: { ($test) } }"
}
def run_tests [tests: list<record<name: string, execute: closure>>] {
let results = $tests | par-each { run_test $in }
print_results $results
print_summary $results
if ($results | any { |test| $test.result == "FAIL" }) {
exit 1
}
}
def print_results [results: list<record<name: string, result: string>>] {
let display_table = $results | update result { |row|
let emoji = if ($row.result == "PASS") { "✅" } else { "❌" }
$"($emoji) ($row.result)"
}
if ("GITHUB_ACTIONS" in $env) {
print ($display_table | to md --pretty)
} else {
print $display_table
}
}
def print_summary [results: list<record<name: string, result: string>>]: nothing -> bool {
let success = $results | where ($it.result == "PASS") | length
let failure = $results | where ($it.result == "FAIL") | length
let count = $results | length
if ($failure == 0) {
print $"\nTesting completed: ($success) of ($count) were successful"
} else {
print $"\nTesting completed: ($failure) of ($count) failed"
}
}
def run_test [test: record<name: string, execute: closure>]: nothing -> record<name: string, result: string, error: string> {
try {
do ($test.execute)
{ result: $"PASS",name: $test.name, error: "" }
} catch { |error|
{ result: $"FAIL", name: $test.name, error: $"($error.msg) (format_error $error.debug)" }
}
}
def format_error [error: string] {
# Get the value for the text key in a partly non-json error message
let parsed = $error
| parse --regex ".+text: \"(.+)\""
if ($parsed | is-empty) {
return ''
}
$parsed
| first
| get capture0
| str replace --all --regex "\\\\n" " "
| str replace --all --regex " +" " "
}
def "test bin installed correctly" [] {
const paths = [
/usr/bin/nu,
/usr/libexec/nushell/nu_plugin_inc,
/usr/libexec/nushell/nu_plugin_gstat,
/usr/libexec/nushell/nu_plugin_query,
/usr/libexec/nushell/nu_plugin_polars,
/usr/libexec/nushell/nu_plugin_formats,
]
let exist = $paths | all {|p| $p | path exists }
assert equal $exist true
}
def "test Nu version is correct" [] {
let version = nu --version
assert greater or equal (compare-ver $version $NU_VERSION) 0
}
def "test nu is added as a shell" [] {
cat /etc/shells | print
let shell = open /etc/shells
| lines
| where ($it | str contains "nu")
| first
assert str contains $shell "/nu"
}
def 'test nu is not the only shell' [] {
let shells = open /etc/shells
| lines
| where not ($it | str contains "nu")
| length
assert greater $shells 0
}
def "test main plugins are installed" [] {
let plugins = (plugin list) | get name
assert ("formats" in $plugins)
assert ("gstat" in $plugins)
assert ("inc" in $plugins)
assert ("polars" in $plugins)
assert ("query" in $plugins)
}
def "test config initialized" [] {
let files = ls ~/.config/nushell
| select name size
| where name ends-with '.nu'
| insert file { |row| $row.name | parse --regex ".+/(.+\\.nu)" | first | get capture0 }
let env_size = $files | where file == "env.nu" | get size | first
let config_size = $files | where file == "config.nu" | get size | first
assert greater $env_size 300B
assert greater $config_size 350B
}
# Compare two version number, return `1` if first one is higher than second one,
# Return `0` if they are equal, otherwise return `-1`
# Examples:
# compare-ver 1.2.3 1.2.0 # Returns 1
# compare-ver 2.0.0 2.0.0 # Returns 0
# compare-ver 1.9.9 2.0.0 # Returns -1
# Format: Expects semantic version strings (major.minor.patch)
# - Optional 'v' prefix
# - Pre-release suffixes (-beta, -rc, etc.) are ignored
# - Missing segments default to 0
export def compare-ver [v1: string, v2: string] {
# Parse the version number: remove pre-release and build information,
# only take the main version part, and convert it to a list of numbers
def parse-ver [v: string] {
$v | str replace -r '^v' '' | str trim | split row -
| first | split row . | each { into int }
}
let a = parse-ver $v1
let b = parse-ver $v2
# Compare the major, minor, and patch parts; fill in the missing parts with 0
# If you want to compare more parts use the following code:
# for i in 0..([2 ($a | length) ($b | length)] | math max)
for i in 0..2 {
let x = $a | get -o $i | default 0
let y = $b | get -o $i | default 0
if $x > $y { return 1 }
if $x < $y { return (-1) }
}
0
}