@@ -28,6 +28,11 @@ export def 'main test-unit' [
2828}
2929
3030# Run integration tests
31+ #
32+ # These are snapshot tests: each test runs a command and saves the output to a file.
33+ # The files are committed to git, so `git diff` reveals any behavioral changes.
34+ # The `run-snapshot-test` helper embeds the generating code as header comments,
35+ # making each snapshot self-documenting.
3136export def 'main test-integration' [
3237 -- json # output results as JSON for external consumption
3338] {
@@ -36,6 +41,7 @@ export def 'main test-integration' [
3641 (test-dependencies-keep_builtins )
3742 (test-embeds-remove )
3843 (test-embeds-update )
44+ (test-coverage )
3945 ]
4046 # Run numd on README if available
4147 | if (scope modules | where name == ' numd' | is-not-empty ) {
@@ -44,19 +50,11 @@ export def 'main test-integration' [
4450 | if $json { to json -- raw } else { }
4551}
4652
47- # Test dependencies command
48- def 'test-dependencies' [] {
49- let output_file = [' tests' ' output-yaml' ' dependencies.yaml' ] | path join
50-
53+ # Run command and save output with source code as header comment
54+ def run-snapshot-test [name : string output_file : string command_src : closure ] {
5155 mkdir ($output_file | path dirname )
5256 rm - f $output_file
5357
54- let command_src = {
55- glob ([tests assets b * ] | path join | str replace - a ' \' ' /' )
56- | dependencies ... $in
57- | to yaml
58- }
59-
6058 let command_text = view source $command_src
6159 | lines | skip | drop | str trim
6260 | each { $' # ($in )' }
@@ -65,54 +63,68 @@ def 'test-dependencies' [] {
6563 $command_text + (char nl ) + (do $command_src )
6664 | save - f $output_file
6765
68- {test : ' dependencies' file : $output_file }
66+ {test : $name file : $output_file }
67+ }
68+
69+ # Test dependencies command
70+ def 'test-dependencies' [] {
71+ run-snapshot-test ' dependencies' ([tests output-yaml dependencies.yaml ] | path join ) {
72+ glob ([tests assets b * ] | path join | str replace - a ' \' ' /' )
73+ | dependencies ... $in
74+ | to yaml
75+ }
6976}
7077
7178# Test dependencies command with keep-builtins option
7279def 'test-dependencies-keep_builtins' [] {
73- let output_file = [' tests' ' output-yaml' ' dependencies --keep_bulitins.yaml' ] | path join
74-
75- mkdir ($output_file | path dirname )
76- rm - f $output_file
77-
78- let command_src = {
80+ run-snapshot-test ' dependencies --keep-builtins' ([tests output-yaml ' dependencies --keep_builtins.yaml' ] | path join ) {
7981 glob ([tests assets b * ] | path join | str replace - a ' \' ' /' )
8082 | dependencies ... $in -- keep-builtins
8183 | to yaml
8284 }
83-
84- let command_text = view source $command_src
85- | lines | skip | drop | str trim
86- | each { $' # ($in )' }
87- | str join (char nl )
88-
89- $command_text + (char nl ) + (do $command_src )
90- | save - f $output_file
91-
92- {test : ' dependencies --keep-builtins' file : $output_file }
9385}
9486
9587# Test embeds-remove command
9688def 'test-embeds-remove' [] {
97- let input_file = ' tests/assets/dotnu-capture.nu'
98- let output_file = ' tests/assets/dotnu-capture-clean.nu'
99-
100- open $input_file
101- | dotnu embeds-remove
102- | save - f $output_file
103-
104- {test : ' embeds-remove' file : $output_file }
89+ run-snapshot-test ' embeds-remove' ([tests assets dotnu-capture-clean.nu ] | path join ) {
90+ open ([tests assets dotnu-capture.nu ] | path join )
91+ | dotnu embeds-remove
92+ }
10593}
10694
10795# Test embeds-update command
10896def 'test-embeds-update' [] {
109- let input_file = ' tests/assets/dotnu-capture.nu'
110- let output_file = ' tests/assets/dotnu-capture-updated.nu'
97+ run-snapshot-test ' embeds-update' ([tests assets dotnu-capture-updated.nu ] | path join ) {
98+ dotnu embeds-update ([tests assets dotnu-capture.nu ] | path join ) -- echo
99+ }
100+ }
111101
112- dotnu embeds-update $input_file -- echo
113- | save - f $output_file
102+ # Test coverage: find public API commands without tests
103+ def 'test-coverage' [] {
104+ run-snapshot-test ' coverage' ([tests output-yaml coverage-untested.yaml ] | path join ) {
105+ # Public API from mod.nu
106+ let public_api = open ([dotnu mod.nu ] | path join )
107+ | lines
108+ | where $it =~ ' ^\s+"'
109+ | each { $in | str trim | str replace - r ' ^"([^"]+)".*' ' $1' }
114110
115- {test : ' embeds-update' file : $output_file }
111+ # Find untested commands
112+ let untested = [" dotnu/*.nu" " tests/test_commands.nu" " toolkit.nu" ]
113+ | each { glob $in }
114+ | flatten
115+ | dependencies ... $in
116+ | filter-commands-with-no-tests
117+ | where caller in $public_api
118+ | select caller
119+
120+ # Output as yaml
121+ {
122+ public_api_count : ($public_api | length )
123+ tested_count : (($public_api | length ) - ($untested | length ))
124+ untested : ($untested | get caller )
125+ }
126+ | to yaml
127+ }
116128}
117129
118130# Test numd on README
0 commit comments