Implement annotations support in test runner (#9406)

<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
Test runner now uses annotations instead of magic function names to pick
up code to run. Additionally skipping tests is now done on annotation
level so skipping and unskipping a test no longer requires changes to
the test code

In order for a function to be picked up by the test runner it needs to
meet following criteria:
* Needs to be private (all exported functions are ignored)
* Needs to contain one of valid annotations (and only the annotation)
directly above the definition, all other comments are ignored

Following are considered valid annotations:
* \# test
* \# test-skip
* \# before-all
* \# before-each
* \# after-each
* \# after-all

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Yethal
2023-07-02 10:41:33 +02:00
committed by GitHub
parent 7c80067900
commit 9ef1203ef9
13 changed files with 301 additions and 120 deletions

View File

@ -42,47 +42,57 @@ def "assert message short" [
assert str contains $output "test message"
}
def test_critical [] {
#[test]
def critical [] {
assert no message 99 critical
assert message CRITICAL critical CRT
}
def test_critical_short [] {
#[test]
def critical_short [] {
assert message short CRITICAL critical C
}
def test_error [] {
#[test]
def error [] {
assert no message CRITICAL error
assert message ERROR error ERR
}
def test_error_short [] {
#[test]
def error_short [] {
assert message short ERROR error E
}
def test_warning [] {
#[test]
def warning [] {
assert no message ERROR warning
assert message WARNING warning WRN
}
def test_warning_short [] {
#[test]
def warning_short [] {
assert message short WARNING warning W
}
def test_info [] {
#[test]
def info [] {
assert no message WARNING info
assert message INFO info "INF" # INF has to be quoted, otherwise it is the `inf` float
}
def test_info_short [] {
#[test]
def info_short [] {
assert message short INFO info I
}
def test_debug [] {
#[test]
def debug [] {
assert no message INFO debug
assert message DEBUG debug DBG
}
def test_debug_short [] {
#[test]
def debug_short [] {
assert message short DEBUG debug D
}

View File

@ -22,20 +22,23 @@ def run-command [
} | complete | get --ignore-errors stderr
}
def test_errors_during_deduction [] {
#[test]
def errors_during_deduction [] {
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25) "Cannot deduce log level prefix for given log level"
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25 --ansi (ansi red)) "Cannot deduce log level prefix for given log level"
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25 --level-prefix "abc") "Cannot deduce ansi for given log level"
}
def test_valid_calls [] {
#[test]
def valid_calls [] {
assert equal (run-command "DEBUG" "msg" "%MSG%" 25 --level-prefix "abc" --ansi (ansi default) | str trim --right) "msg"
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"($env.LOG_PREFIX.INFO) msg"
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" --level-prefix "abc" 20 | str trim --right) "abc msg"
assert equal (run-command "INFO" "msg" "%ANSI_START%%LEVEL% %MSG%%ANSI_STOP%" $env.LOG_LEVEL.CRITICAL | str trim --right) $"($env.LOG_ANSI.CRITICAL)CRT msg(ansi reset)"
}
def test_log_level_handling [] {
#[test]
def log_level_handling [] {
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"($env.LOG_PREFIX.INFO) msg"
assert equal (run-command "WARNING" "msg" "%LEVEL% %MSG%" 20 | str trim --right) ""
}

View File

@ -39,7 +39,8 @@ def "assert formatted" [
assert equal ($output | str trim --right) (format-message $message $format $prefix $ansi)
}
def "test_format_flag" [] {
#[test]
def format_flag [] {
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" critical
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" error
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" warning

View File

@ -1,6 +1,7 @@
use std *
def test_env_log_ansi [] {
#[test]
def env_log_ansi [] {
assert equal $env.LOG_ANSI.CRITICAL (ansi red_bold)
assert equal $env.LOG_ANSI.ERROR (ansi red)
assert equal $env.LOG_ANSI.WARNING (ansi yellow)
@ -8,7 +9,8 @@ def test_env_log_ansi [] {
assert equal $env.LOG_ANSI.DEBUG (ansi default_dimmed)
}
def test_env_log_level [] {
#[test]
def env_log_level [] {
assert equal $env.LOG_LEVEL.CRITICAL 50
assert equal $env.LOG_LEVEL.ERROR 40
assert equal $env.LOG_LEVEL.WARNING 30
@ -16,7 +18,8 @@ def test_env_log_level [] {
assert equal $env.LOG_LEVEL.DEBUG 10
}
def test_env_log_prefix [] {
#[test]
def env_log_prefix [] {
assert equal $env.LOG_PREFIX.CRITICAL "CRT"
assert equal $env.LOG_PREFIX.ERROR "ERR"
assert equal $env.LOG_PREFIX.WARNING "WRN"
@ -24,7 +27,8 @@ def test_env_log_prefix [] {
assert equal $env.LOG_PREFIX.DEBUG "DBG"
}
def test_env_log_short_prefix [] {
#[test]
def env_log_short_prefix [] {
assert equal $env.LOG_SHORT_PREFIX.CRITICAL "C"
assert equal $env.LOG_SHORT_PREFIX.ERROR "E"
assert equal $env.LOG_SHORT_PREFIX.WARNING "W"
@ -32,6 +36,7 @@ def test_env_log_short_prefix [] {
assert equal $env.LOG_SHORT_PREFIX.DEBUG "D"
}
def test_env_log_format [] {
#[test]
def env_log_format [] {
assert equal $env.LOG_FORMAT $"%ANSI_START%%DATE%|%LEVEL%|(ansi u)%MSG%%ANSI_STOP%"
}

View File

@ -1,34 +1,39 @@
use std *
def test_assert [] {
#[test]
def assert_basic [] {
assert true
assert (1 + 2 == 3)
assert error { assert false }
assert error { assert (1 + 2 == 4) }
}
def test_assert_not [] {
#[test]
def assert_not [] {
assert not false
assert not (1 + 2 == 4)
assert error { assert not true }
assert error { assert not (1 + 2 == 3) }
}
def test_assert_equal [] {
#[test]
def assert_equal [] {
assert equal (1 + 2) 3
assert equal (0.1 + 0.2 | into string | into decimal) 0.3 # 0.30000000000000004 == 0.3
assert error { assert equal 1 "foo" }
assert error { assert equal (1 + 2) 4 }
}
def test_assert_not_equal [] {
#[test]
def assert_not_equal [] {
assert not equal (1 + 2) 4
assert not equal 1 "foo"
assert not equal (1 + 2) "3"
assert error { assert not equal 1 1 }
}
def test_assert_error [] {
#[test]
def assert_error [] {
let failing_code = {|| missing_code_to_run}
assert error $failing_code
@ -37,33 +42,39 @@ def test_assert_error [] {
assert $assert_error_raised "The assert error should raise an error if there is no error in the executed code."
}
def test_assert_less [] {
#[test]
def assert_less [] {
assert less 1 2
assert error { assert less 1 1 }
}
def test_assert_less_or_equal [] {
#[test]
def assert_less_or_equal [] {
assert less or equal 1 2
assert less or equal 1 1
assert error { assert less or equal 1 0 }
}
def test_assert_greater [] {
#[test]
def assert_greater [] {
assert greater 2 1
assert error { assert greater 2 2 }
}
def test_assert_greater_or_equal [] {
#[test]
def assert_greater_or_equal [] {
assert greater or equal 1 1
assert greater or equal 2 1
assert error { assert greater or equal 0 1 }
}
def test_assert_length [] {
#[test]
def assert_length [] {
assert length [0, 0, 0] 3
assert error { assert length [0, 0] 3 }
}
def test_assert_skip [] {
assert skip # This test case is skipped on purpose
#[ignore]
def assert_skip [] {
assert true # This test case is skipped on purpose
}

View File

@ -6,6 +6,7 @@ use std log
# Each 'use' for that module in the test script will execute the def-env block.
# PWD at the time of the `use` will be what the export def-env block will see.
#[before-each]
def before-each [] {
# need some directories to play with
let base_path = ($nu.temp-path | path join $"test_dirs_(random uuid)")
@ -17,6 +18,7 @@ def before-each [] {
{base_path: $base_path, path_a: $path_a, path_b: $path_b}
}
#[after-each]
def after-each [] {
let base_path = $in.base_path
cd $base_path
@ -28,13 +30,15 @@ def cur_dir_check [expect_dir, scenario] {
log debug $"check dir ($expect_dir), scenario ($scenario)"
assert equal $expect_dir $env.PWD $"expected not PWD after ($scenario)"
}
def cur_ring_check [expect_dir:string, expect_position: int scenario:string] {
log debug $"check ring ($expect_dir), position ($expect_position) scenario ($scenario)"
assert ($expect_position < ($env.DIRS_LIST | length)) $"ring big enough after ($scenario)"
assert equal $expect_position $env.DIRS_POSITION $"position in ring after ($scenario)"
}
def test_dirs_command [] {
#[test]
def dirs_command [] {
# careful with order of these statements!
# must capture value of $in before executing `use`s
let $c = $in
@ -84,7 +88,8 @@ def test_dirs_command [] {
assert equal $env.PWD $c.base_path "drop changes PWD (regression test for #9449)"
}
def test_dirs_next [] {
#[test]
def dirs_next [] {
# must capture value of $in before executing `use`s
let $c = $in
# must set PWD *before* doing `use` that will run the def-env block in dirs module.
@ -105,7 +110,8 @@ def test_dirs_next [] {
}
def test_dirs_cd [] {
#[test]
def dirs_cd [] {
# must capture value of $in before executing `use`s
let $c = $in
# must set PWD *before* doing `use` that will run the def-env block in dirs module.

View File

@ -1,6 +1,7 @@
use std *
def test_iter_find [] {
#[test]
def iter_find [] {
let hastack1 = [1 2 3 4 5 6 7]
let hastack2 = [nushell rust shell iter std]
let hastack3 = [nu 69 2023-04-20 "std"]
@ -18,7 +19,8 @@ def test_iter_find [] {
assert equal $res null
}
def test_iter_intersperse [] {
#[test]
def iter_intersperse [] {
let res = ([1 2 3 4] | iter intersperse 0)
assert equal $res [1 0 2 0 3 0 4]
@ -38,7 +40,8 @@ def test_iter_intersperse [] {
assert equal $res [4]
}
def test_iter_scan [] {
#[test]
def iter_scan [] {
let scanned = ([1 2 3] | iter scan 0 {|x, y| $x + $y} -n)
assert equal $scanned [1, 3, 6]
@ -49,7 +52,8 @@ def test_iter_scan [] {
assert equal $scanned ["a" "ab" "abc" "abcd"]
}
def test_iter_filter_map [] {
#[test]
def iter_filter_map [] {
let res = ([2 5 "4" 7] | iter filter-map {|it| $it ** 2})
assert equal $res [4 25 49]
@ -60,7 +64,8 @@ def test_iter_filter_map [] {
assert equal $res [3 42 69]
}
def test_iter_find_index [] {
#[test]
def iter_find_index [] {
let res = (
["iter", "abc", "shell", "around", "nushell", "std"]
| iter find-index {|x| $x starts-with 's'}
@ -75,7 +80,8 @@ def test_iter_find_index [] {
assert equal $res 0
}
def test_iter_zip_with [] {
#[test]
def iter_zip_with [] {
let res = (
[1 2 3] | iter zip-with [2 3 4] {|a, b| $a + $b }
)
@ -101,7 +107,8 @@ def test_iter_zip_with [] {
]
}
def test_iter_flat_map [] {
#[test]
def iter_flat_map [] {
let res = (
[[1 2 3] [2 3 4] [5 6 7]] | iter flat-map {|it| $it | math sum}
)
@ -111,7 +118,8 @@ def test_iter_flat_map [] {
assert equal $res [11 22 33]
}
export def test_iter_zip_into_record [] {
#[test]
def iter_zip_into_record [] {
let headers = [name repo position]
let values = [rust github 1]

View File

@ -1,26 +1,30 @@
use std log
use std assert
#[before-each]
def before-each [] {
log debug "Setup is running"
{msg: "This is the context"}
}
#[after-each]
def after-each [] {
log debug $"Teardown is running. Context: ($in)"
}
def test_assert_pass [] {
#[test]
def assert_pass [] {
log debug $"Assert is running. Context: ($in)"
}
def test_assert_skip [] {
#[ignore]
def assert_skip [] {
log debug $"Assert is running. Context: ($in)"
assert skip
}
def test_assert_fail_skipped_by_default [] {
assert skip # Comment this line if you want to see what happens if a test fails
#[ignore]
def assert_fail_skipped_by_default [] {
# Change test-skip to test if you want to see what happens if a test fails
log debug $"Assert is running. Context: ($in)"
assert false
}

View File

@ -1,6 +1,7 @@
use std
def test_path_add [] {
#[test]
def path_add [] {
use std assert
let path_name = if "PATH" in $env { "PATH" } else { "Path" }
@ -33,6 +34,7 @@ def test_path_add [] {
}
}
def test_banner [] {
#[test]
def banner [] {
std assert ((std banner | lines | length) == 15)
}

View File

@ -3,6 +3,7 @@ use std xml xupdate
use std xml xinsert
use std assert
#[before-each]
def before-each [] {
{sample_xml: ('
<a>
@ -18,7 +19,8 @@ def before-each [] {
}
}
def test_xml_xaccess [] {
#[test]
def xml_xaccess [] {
let sample_xml = $in.sample_xml
assert equal ($sample_xml | xaccess [a]) [$sample_xml]
@ -28,7 +30,8 @@ def test_xml_xaccess [] {
assert equal ($sample_xml | xaccess [* * * {|e| $e.attributes != {}}]) [[tag, attributes, content]; [c, {a: b}, []]]
}
def test_xml_xupdate [] {
#[test]
def xml_xupdate [] {
let sample_xml = $in.sample_xml
assert equal ($sample_xml | xupdate [*] {|x| $x | update attributes {i: j}}) ('<a i="j"><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d></a>' | from xml)
@ -36,7 +39,8 @@ def test_xml_xupdate [] {
assert equal ($sample_xml | xupdate [* * * {|e| $e.attributes != {}}] {|x| $x | update content ['xml']}) {tag: a, attributes: {}, content: [[tag, attributes, content]; [b, {}, [[tag, attributes, content]; [c, {a: b}, [xml]]]], [c, {}, []], [d, {}, [[tag, attributes, content]; [e, {}, [[tag, attributes, content]; [null, null, z]]], [e, {}, [[tag, attributes, content]; [null, null, x]]]]]]}
}
def test_xml_xinsert [] {
#[test]
def xml_xinsert [] {
let sample_xml = $in.sample_xml
assert equal ($sample_xml | xinsert [a] {tag: b attributes:{} content: []}) ('<a><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d><b></b></a>' | from xml)