diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b3dfac883..a4643fcfff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,9 +82,9 @@ jobs: - name: Tests run: cargo test --workspace --profile ci --exclude nu_plugin_* ${{ matrix.flags }} - python-virtualenv: + std-lib-and-python-virtualenv: env: - NUSHELL_CARGO_TARGET: ci + NU_LOG_LEVEL: DEBUG strategy: fail-fast: true @@ -104,7 +104,19 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.4.4 - name: Install Nushell - run: cargo install --locked --path=. --profile ci --no-default-features + # prior to [*standard library: bring the tests into the main CI*](#8525) + # there was a `--profile ci` here in the `cargo install`, as well as + # `NUSHELL_CARGO_TARGET: ci` in the prelude above. + # + # this caused a "stackoverflow" error in the CI on windows, + # see [this failing job](https://github.com/nushell/nushell/actions/runs/4512034615/jobs/7944945590) + # + # the CI profile has been removed in 00b820de9021227d1910a9ea388297ee7aee308e + # as part of #8525. + run: cargo install --path . --locked --no-default-features + + - name: Standard library tests + run: nu crates/nu-utils/standard_library/tests.nu - name: Setup Python uses: actions/setup-python@v4 diff --git a/crates/nu-utils/standard_library/tests.nu b/crates/nu-utils/standard_library/tests.nu index a070a12642..2673452129 100644 --- a/crates/nu-utils/standard_library/tests.nu +++ b/crates/nu-utils/standard_library/tests.nu @@ -1,31 +1,23 @@ use std.nu * -def collect-modules [ - path: path, - module?: string -] { - let tests_path = ($path | default $env.FILE_PWD) - let module_search = ($module | default "test_*") - (ls ($tests_path | path join $"**/($module_search).nu") -f | get name) -} +# show a test record in a pretty way +# +# `$in` must be a `record`. +# +# the output would be like +# - " x " all in red if failed +# - " " all in green if passed +def show-pretty-test [indent: int = 4] { + let test = $in -def collect-commands [ - test_file: string, - module_name: string, - command?: string -] { - let commands = ( - nu -c $'use ($test_file) *; $nu.scope.commands | select name module_name | to nuon' - | from nuon - | where module_name == $module_name - | where ($it.name | str starts-with "test_") - | get name - ) - if $command == null { - $commands - } else { - $commands | where $it == $command - } + [ + (" " * $indent) + (if $test.pass { ansi green } else { ansi red}) + (if $test.pass { " " } else { char failed}) + " " + $"($test.module) ($test.name)" + (ansi reset) + ] | str join } # Test executor @@ -35,22 +27,61 @@ def main [ --path: path, # Path to look for tests. Default: directory of this file. --module: string, # Module to run tests. Default: all test modules found. --command: string, # Test command to run. Default: all test command found in the files. - --list, # Do not run any tests, just list them (dry run) + --list, # list the selected tests without running them. ] { - let dry_run = ($list | default false) - for test_file in (collect-modules $path $module) { - let $module_name = ($test_file | path parse).stem - - log info $"Run tests in ($module_name)" - let tests = (collect-commands $test_file $module_name $command) - - for test_case in $tests { - log debug $"Run test ($module_name) ($test_case)" - if $dry_run { - continue - } - - nu -c $'use ($test_file) ($test_case); ($test_case)' + let tests = ( + ls ($path | default $env.FILE_PWD | path join "test_*.nu") + | each {|row| {file: $row.name name: ($row.name | path parse | get stem)}} + | upsert test {|module| + nu -c $'use ($module.file) *; $nu.scope.commands | select name module_name | to nuon' + | from nuon + | where module_name == $module.name + | where ($it.name | str starts-with "test_") + | get name } + | flatten + | rename file module name + ) + + let tests_to_run = (if not ($command | is-empty) { + $tests | where name == $command + } else if not ($module | is-empty) { + $tests | where module == $module + } else { + $tests + }) + + if $list { + return ($tests_to_run | select module name file) + } + + let tests = ( + $tests_to_run + | group-by module + | transpose name tests + | each {|module| + log info $"Running tests in ($module.name)" + $module.tests | each {|test| + log debug $"Running test ($test.name)" + let did_pass = (try { + nu -c $'use ($test.file) ($test.name); ($test.name)' + true + } catch { false }) + + $test | merge ({pass: $did_pass}) + } + } + | flatten + ) + + if not ($tests | where not pass | is-empty) { + let text = ([ + $"(ansi purple)some tests did not pass (char lparen)see complete errors above(char rparen):(ansi reset)" + "" + ($tests | each {|test| ($test | show-pretty-test 4)} | str join "\n") + "" + ] | str join "\n") + + error make --unspanned { msg: $text } } }