Repository navigation
Expand file tree
/
Copy pathtoolkit.nu
More file actions
300 lines (255 loc) · 11.7 KB
/
Copy pathtoolkit.nu
File metadata and controls
300 lines (255 loc) · 11.7 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const numdinternals = ([numd commands.nu] | path join)
use $numdinternals [ build-modified-path compute-change-stats ]
export def main [] { }
# Run all tests (unit + integration)
#
# Output mode is auto-detected: when stdout is a terminal you get the human view
# (failures + a summary line); when it is piped or redirected you get machine-readable
# JSON. Force either with --json / --pretty.
export def 'main test' [
--json # force machine-readable JSON output even on a terminal
--pretty # force the human view even when output is piped
--all # human view: also list passing tests (default shows only non-passing)
--update # accept changes: stage modified integration test files
--fail # exit with non-zero code if any tests fail (for CI)
]: nothing -> nothing {
let results = (collect-unit-results) | append (collect-integration-results --update=$update)
if (machine-mode --json=$json --pretty=$pretty) {
print ($results | to json --raw)
} else {
print-human $results --all=$all --update=$update
}
if $fail and ($results | where status == 'failed' | is-not-empty) {
exit 1
}
}
# Run unit tests using nutest
#
# Machine (JSON / piped) rows use the flat schema:
# {type: 'unit', name, status: 'passed'|'failed', file: null, message}
# Note: status is 'passed'|'failed', NOT nutest's 'PASS'|'FAIL' 'result' column.
# message holds the assertion text on failure, null otherwise.
export def 'main test-unit' [
--json # force machine-readable JSON output even on a terminal
--pretty # force the human view even when output is piped
--all # human view: also list passing tests (default shows only failures)
] {
let flat = collect-unit-results
if (machine-mode --json=$json --pretty=$pretty) {
$flat | to json --raw
} else {
print-human $flat --all=$all
}
}
# Run integration tests (execute example markdown files)
#
# Machine rows use the flat schema:
# {type: 'integration', name, status: 'passed'|'changed'|'failed', file, message}
export def 'main test-integration' [
--json # force machine-readable JSON output even on a terminal
--pretty # force the human view even when output is piped
--all # human view: also list passing tests (default shows only non-passing)
--update # accept changes: stage modified files in git
] {
let flat = collect-integration-results --update=$update
if (machine-mode --json=$json --pretty=$pretty) {
$flat | to json --raw
} else {
print-human $flat --all=$all --update=$update
}
}
# Decide whether to emit machine-readable data instead of the human view.
# Why: agents capture stdout through a pipe, humans read it in a terminal.
# Not $nu.is-interactive because: it reports REPL-ness, not human-ness — it is false for
# any `nu toolkit.nu ...` script run (human or agent) and true for an agent driving the
# nushell MCP, so it detects the opposite of what we need. is-terminal --stdout is the tty test.
def machine-mode [--json --pretty]: nothing -> bool {
if $pretty { return false } # Not-piped override wins over everything
if $json { return true }
not (is-terminal --stdout)
}
# Collect unit test results as flat rows (no output side effects)
def collect-unit-results []: nothing -> table {
use ../nutest/nutest
nutest run-tests --path tests/ --returns table --display nothing
| each {|row|
let status = if $row.result == 'PASS' { 'passed' } else { 'failed' }
let message = if $status == 'failed' {
let msgs = $row.output | each {|o| $o.msg? } | compact
if ($msgs | is-empty) { null } else { $msgs | str join '; ' }
} else { null }
{type: 'unit' name: $row.test status: $status file: null message: $message}
}
}
# Collect integration test results as flat rows (executes example markdown files)
def collect-integration-results [--update]: nothing -> table {
# Why: datetime rendering follows the TZ of the spawned `nu --no-config-file` process, so committed
# outputs drift between machines unless every intermediate script runs under one zone.
# Setting $env.TZ inside the eval config would be too late — chrono reads TZ at spawn.
with-env {TZ: 'UTC'} { collect-integration-results-inner --update=$update }
}
def collect-integration-results-inner [--update]: nothing -> table {
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 --force ($simple_md | build-modified-path --suffix '_with_no_output')
# Why: `glob` returns a stream, so the directory walk would keep running while `par-each`
# already executes examples — and the examples write temp files into each other's folders
# (2_numd_commands_explanations saves an intermediate script next to simple_markdown.md).
# A temp `.nu` script then entered the test set and failed as invalid markdown, depending on
# readdir order. `let` collects the stream, so the test set is fixed before anything runs.
let example_files = glob z_examples/*/*.md --exclude [
*/*_with_no_output*
*/*_customized*
*/8_parse_frontmatter
*/run_once*
]
# Run all integration tests and collect results
let results = (
# Strip markdown and run main set of .md files in one loop
$example_files
| 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 --force $strip_markdown_path
# Run files with config set
numd render $file --save-intermed-script $'($file)_intermed.nu' --eval (open --raw ([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 render $path_simple_table --echo --no-stats --eval '$env.numd.table-width = 20'
| ansi strip
| save --force $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 render $path_simple_table --echo --no-stats --eval (open --raw ([z_examples numd_config_example2.nu] | path join))
| ansi strip
| save --force $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 render 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 render README.md --eval (open --raw ([z_examples numd_config_example1.nu] | path join)) --ignore-git-check
}
)
)
if $update {
let changed = $results | where status == 'changed'
if ($changed | is-not-empty) {
$changed | each {|r|
^git add $r.file
# Why: --stderr so staging notes never corrupt the JSON on stdout in machine mode
print --stderr $"(ansi green)Staged:(ansi reset) ($r.file)"
}
}
}
$results
}
# Print the human view: non-passing tests (or all with --all), then a summary line.
# Returns nothing so no wide table auto-renders and truncates the verdict column.
def print-human [flat: table --all --update]: nothing -> nothing {
let to_show = if $all { $flat } else { $flat | where status != 'passed' }
$to_show | each {|r| print-test-result $r }
print-summary $flat --update=$update
}
# Print the N passed, M failed [, K changed] headline
def print-summary [flat: table --update]: nothing -> nothing {
let passed = $flat | where status == 'passed' | length
let failed = $flat | where status == 'failed' | length
let changed = $flat | where status == 'changed' | length
let total = $flat | length
let parts = [
$"(ansi green_bold)($passed) passed(ansi reset)"
$"(ansi red_bold)($failed) failed(ansi reset)"
] | append (if $changed > 0 { [$"(ansi yellow_bold)($changed) changed(ansi reset)"] } else { [] })
print $"($parts | str join ', ') \(($total) total\)"
if $changed > 0 and not $update {
print $"(ansi attr_dimmed)Run with --update to accept changes(ansi reset)"
}
}
# Print a single test result with status indicator (and the assertion on failure)
def print-test-result [result: record]: nothing -> nothing {
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)"
if $result.status == 'failed' and ($result.message? | is-not-empty) {
print $" (ansi red)($result.message)(ansi reset)"
}
}
# Run an integration test and return unified result format
def run-integration-test [name: string command_src: closure]: nothing -> record {
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 message: null}
} catch {|err|
{type: 'integration' name: ($name | path basename) status: 'failed' file: $name message: $err.msg}
}
}
def update-dotnu-embeds []: nothing -> nothing {
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 --message $'($tag) nupm version'
git tag $tag
git push origin main --tags
}