-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoolkit.nu
More file actions
225 lines (193 loc) · 7.9 KB
/
toolkit.nu
File metadata and controls
225 lines (193 loc) · 7.9 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const numdinternals = ([numd commands.nu] | path join)
use $numdinternals [ build-modified-path compute-change-stats ]
export def main [] { }
# Run all tests (unit + integration)
export def 'main test' [
--json # output results as JSON for external consumption
--update # accept changes: stage modified integration test files
--fail # exit with non-zero code if any tests fail (for CI)
] {
if not $json { print $"(ansi attr_dimmed)Unit tests(ansi reset)" }
let unit = main test-unit --json=$json
if not $json { print $"(ansi attr_dimmed)Integration tests(ansi reset)" }
let integration = main test-integration --json=$json --update=$update
# Parse JSON if needed
let unit_data = if $json { $unit | from json } else { $unit }
let integration_data = if $json { $integration | from json } else { $integration }
let results = $unit_data | append $integration_data
# Print summary
let passed = $results | where status == 'passed' | length
let failed = $results | where status == 'failed' | length
let changed = $results | where status == 'changed' | length
let total = $results | length
if not $json {
print ""
print $"(ansi green_bold)($passed) passed(ansi reset), (ansi red_bold)($failed) failed(ansi reset), (ansi yellow_bold)($changed) changed(ansi reset) \(($total) total\)"
if $changed > 0 and not $update {
print $"(ansi attr_dimmed)Run with --update to accept changes(ansi reset)"
}
}
if $fail and $failed > 0 {
if $json { print ($results | to json --raw) }
exit 1
}
if $json { $results | to json --raw }
}
# Run unit tests using nutest
export def 'main test-unit' [
--json # output results as JSON for external consumption
] {
use ../nutest/nutest
# Get detailed table from nutest
let results = nutest run-tests --path tests/ --returns table --display nothing
# Convert to flat table format
let flat = $results
| each {|row|
let status = if $row.result == 'PASS' { 'passed' } else { 'failed' }
{type: 'unit' name: $row.test status: $status file: null}
}
if not $json {
$flat | each {|r| print-test-result $r }
}
if $json { $flat | to json --raw } else { $flat }
}
# Run integration tests (execute example markdown files)
export def 'main test-integration' [
--json # output results as JSON for external consumption
--update # accept changes: stage modified files in git
] {
use numd
# will be executed if dotnu-embeds-are-available
update-dotnu-embeds
# path join is used for windows compatability
let path_simple_table = [z_examples 5_simple_nu_table simple_nu_table.md] | path join
# clear outputs from simple markdown
let simple_md = ['z_examples' '1_simple_markdown' 'simple_markdown.md'] | path join
numd clear-outputs $simple_md --echo
| save -f ($simple_md | build-modified-path --suffix '_with_no_output')
# Run all integration tests and collect results
let results = (
# Strip markdown and run main set of .md files in one loop
glob z_examples/*/*.md --exclude [
*/*_with_no_output*
*/*_customized*
*/8_parse_frontmatter
*/run_once*
]
| par-each --keep-order {|file|
run-integration-test $file {
# Strip markdown
let strip_markdown_path = $file
| path parse
| get stem
| $in + '.nu'
| [z_examples 99_strip_markdown $in]
| path join
numd clear-outputs $file --strip-markdown --echo
| save -f $strip_markdown_path
# Run files with config set
numd run $file --save-intermed-script $'($file)_intermed.nu' --eval (open -r ([z_examples numd_config_example1.nu] | path join)) --ignore-git-check
}
}
# Run file with customized width of table
| append (
run-integration-test ($path_simple_table | build-modified-path --suffix '_customized_width20') {
let target = $path_simple_table | build-modified-path --suffix '_customized_width20'
numd run $path_simple_table --echo --no-stats --eval '$env.numd.table-width = 20'
| ansi strip
| save -f $target
}
)
# Run file with another config
| append (
run-integration-test ($path_simple_table | build-modified-path --suffix '_customized_example_config') {
let target = $path_simple_table | build-modified-path --suffix '_customized_example_config'
numd run $path_simple_table --echo --no-stats --eval (open -r ([z_examples numd_config_example2.nu] | path join))
| ansi strip
| save -f $target
}
)
# Run run-once test via --echo (file mutates by design, so we assert on output instead)
| append (
run-integration-test 'z_examples/6_edge_cases/run_once.md' {
let output = numd run z_examples/6_edge_cases/run_once.md --echo --no-stats
if ($output !~ '```nu no-run') or ($output =~ '```nu run-once') {
error make {msg: 'run-once was not rewritten to no-run'}
}
}
)
# Run readme
| append (
run-integration-test 'README.md' {
numd run README.md --eval (open -r ([z_examples numd_config_example1.nu] | path join)) --ignore-git-check
}
)
)
if not $json {
$results | each {|r| print-test-result $r }
}
if $update {
let changed = $results | where status == 'changed'
if ($changed | is-not-empty) {
$changed | each {|r|
^git add $r.file
print $"(ansi green)Staged:(ansi reset) ($r.file)"
}
}
}
if $json { $results | to json --raw } else { $results }
}
# Print a single test result with status indicator
def print-test-result [result: record] {
let icon = match $result.status {
'passed' => $"(ansi green)✓(ansi reset)"
'failed' => $"(ansi red)✗(ansi reset)"
'changed' => $"(ansi yellow)~(ansi reset)"
_ => "?"
}
let suffix = if $result.file != null { $" (ansi attr_dimmed)\(($result.file)\)(ansi reset)" } else { "" }
print $" ($icon) ($result.name)($suffix)"
}
# Run an integration test and return unified result format
def run-integration-test [name: string, command_src: closure] {
try {
do $command_src
# Check git diff to determine status
let diff_result = do { ^git diff --quiet $name } | complete
let status = if $diff_result.exit_code == 0 { 'passed' } else { 'changed' }
{type: 'integration' name: ($name | path basename) status: $status file: $name}
} catch {|err|
{type: 'integration' name: ($name | path basename) status: 'failed' file: $name}
}
}
def update-dotnu-embeds [] {
scope modules
| where name == 'dotnu'
| is-empty
| if $in { return }
dotnu embeds-update z_examples/8_parse_frontmatter/dotnu-test.nu
}
export def 'main release' [
--major (-M) # Bump major version (X.0.0)
--minor (-m) # Bump minor version (x.Y.0)
] {
git checkout main
let description = gh repo view --json description | from json | get description
let parts = git tag | lines | sort --natural | last | split row '.' | into int
let tag = if $major {
[($parts.0 + 1) 0 0]
} else if $minor {
[$parts.0 ($parts.1 + 1) 0]
} else {
[$parts.0 $parts.1 ($parts.2 + 1)]
} | str join '.'
open nupm.nuon
| update description ($description | str replace 'numd - ' '')
| update version $tag
| to nuon --indent 2
| save --force --raw nupm.nuon
git add nupm.nuon
git commit -m $'($tag) nupm version'
git tag $tag
git push origin main --tags
}