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

@ -120,16 +120,6 @@ export def error [
}
}
# Skip the current test case
#
# # Examples
#
# if $condition { assert skip }
export def skip [] {
error make {msg: "ASSERT:SKIP"}
}
# Assert $left == $right
#
# For more documentation see the assert command

View File

@ -1,6 +1,113 @@
use log.nu
# Here we store the map of annotations internal names and the annotation actually used during test creation
# The reason we do that is to allow annotations to be easily renamed without modifying rest of the code
# Functions with no annotations or with annotations not on the list are rejected during module evaluation
# test and test-skip annotations may be used multiple times throughout the module as the function names are stored in a list
# Other annotations should only be used once within a module file
# If you find yourself in need of multiple before- or after- functions it's a sign your test suite probably needs redesign
def valid-annotations [] {
{
"#[test]": "test",
"#[ignore]": "test-skip",
"#[before-each]": "before-each"
"#[before-all]": "before-all"
"#[after-each]": "after-each"
"#[after-all]": "after-all"
}
}
# Returns a table containing the list of function names together with their annotations (comments above the declaration)
#
# The way this works is we first generate an ast for a file and then extract all tokens containing keyword def with internalcall shape
# Then for each token we extract the next token (the actual function name) and the previous token (usually closing bracket of previous function)
# We then open the file as raw text and extract the last line of substring starting from the span end of previous token up to span start of the def token which contains the annotation and surrounding whitespaces
# We take the last line only in order to not pick up unrelated comments from the source code
def get-annotated [
file: path
] {
let raw_file = (open $file)
let ast = (
^$nu.current-exe --ide-ast $file
| from json
| enumerate
| flatten
)
$ast
| where content == def and shape == shape_internalcall
| insert function_name {|x|
$ast
| get ($x.index + 1)
| get content
}
| insert annotation {|x|
# index == 0 means the function is defined on the first lines of the file in which case there will be no preceding tokens
if $x.index > 0 {
let preceding_span = (
$ast
| get ($x.index - 1)
| get span
)
$raw_file
| str substring $preceding_span.end..$x.span.start
| lines
| where $it != ''
| last
| str trim
| str join (char nl)
} else {
''
}
}
| select function_name annotation
}
# Takes table of function names and their annotations such as the one returned by get-annotated
#
# Returns a record where keys are internal names of valid annotations and values are corresponding function names
# Annotations that allow multiple functions are of type list<string>
# Other annotations are of type string
# Result gets merged with the template record so that the output shape remains consistent regardless of the table content
def create-test-record [] {
let input = $in
let template_record = {
before-each: '',
before-all: '',
after-each: '',
after-all: '',
test-skip: []
}
let test_record = (
$input
| where annotation in (valid-annotations|columns)
| update annotation {|x|
valid-annotations
| get $x.annotation
}
| group-by annotation
| transpose key value
| update value {|x|
$x.value.function_name
| if $x.key in ["test", "test-skip"] {
$in
} else {
get 0
}
}
| transpose -i -r -d
)
$template_record
| merge $test_record
}
def throw-error [error: record] {
error make {
msg: $"(ansi red)($error.msg)(ansi reset)"
@ -41,17 +148,16 @@ def show-pretty-test [indent: int = 4] {
] | str join
}
def get-commands [
file: path
] {
^$nu.current-exe --ide-ast $file
| from json
| get content
| split list def
| skip 1
| each {get 0}
}
# Takes a test record and returns the execution result
# Test is executed via following steps:
# * Public function with random name is generated that runs specified test in try/catch block
# * Module file is opened
# * Random public function is appended to the end of the file
# * Modified file is saved under random name
# * Nu subprocess is spawned
# * Inside subprocess the modified file is imported and random function called
# * Output of the random function is serialized into nuon and returned to parent process
# * Modified file is removed
def run-test [
test: record
] {
@ -67,11 +173,7 @@ export def ($test_function_name) [] {
($test.after-each)
} catch { |err|
($test.after-each)
if $err.msg == "ASSERT:SKIP" {
exit 2
} else {
$err | get raw
}
$err | get raw
}
}
"
@ -84,7 +186,6 @@ export def ($test_function_name) [] {
let result = (
^$nu.current-exe -c $"use ($rendered_module_path) *; ($test_function_name)|to nuon"
| complete
)
rm $rendered_module_path
@ -92,16 +193,23 @@ export def ($test_function_name) [] {
return $result
}
# Takes a module record and returns a table with following columns:
#
# * file - path to file under test
# * name - name of the module under test
# * test - name of specific test
# * result - test execution result
def run-tests-for-module [
module: record
module: record<file: path name: string before-each: string after-each: string before-all: string after-all: string test: list test-skip: list>
] {
let global_context = if $module.before-all {
let global_context = if not ($module.before-all|is-empty) {
log info $"Running before-all for module ($module.name)"
run-test {
file: $module.file,
before-each: 'let context = {}',
after-each: '',
test: 'before-all'
test: $module.before-all
}
| if $in.exit_code == 0 {
$in.stdout
@ -116,56 +224,86 @@ def run-tests-for-module [
{}
}
# since tests are skipped based on their annotation and never actually executed we can generate their list in advance
let skipped_tests = (
if not ($module.test-skip|is-empty) {
$module
| update test $module.test-skip
| reject test-skip
| flatten
| insert result 'skip'
} else {
[]
}
)
let tests = (
$module
| flatten
| rename -c [tests test]
| reject test-skip
| flatten test
| update before-each {|x|
if $module.before-each {
$"let context = \(($global_context)|merge \(before-each\)\)"
if not ($module.before-each|is-empty) {
$"let context = \(($global_context)|merge \(($module.before-each)\)\)"
} else {
$"let context = ($global_context)"
}
}
| update after-each {|x|
if $module.after-each {
'$context | after-each'
if not ($module.after-each|is-empty) {
$"$context | ($module.after-each)"
} else {
''
}
}
| each {|test|
log info $"Running ($test.test) in module ($module.name) with context ($global_context)"
log info $"Running ($test.test) in module ($module.name)"
log debug $"Global context is ($global_context)"
$test|insert result {|x|
run-test $test
| match $in.exit_code {
0 => "pass",
2 => "skip",
_ => "fail",
| if $in.exit_code == 0 {
'pass'
} else {
'fail'
}
}
}
| append $skipped_tests
| select file name test result
)
if $module.after-all {
if not ($module.after-all|is-empty) {
log info $"Running after-all for module ($module.name)"
run-test {
file: $module.file,
before-each: $"let context = ($global_context)",
after-each: '',
test: 'after-all'
test: $module.after-all
}
}
return $tests
}
# Run tests for nushell code
#
# By default all detected tests are executed
# Test list can be filtered out by specifying either path to search for, name of the module to run tests for or specific test name
# In order for a function to be recognized as a test by the test runner it needs to be annotated with # test
# Following annotations are supported by the test runner:
# * test - test case to be executed during test run
# * test-skip - test case to be skipped during test run
# * before-all - function to run at the beginning of test run. Returns a global context record that is piped into every test function
# * before-each - function to run before every test case. Returns a per-test context record that is merged with global context and piped into test functions
# * after-each - function to run after every test case. Receives the context record just like the test cases
# * after-all - function to run after all test cases have been executed. Receives the global context record
export def run-tests [
--path: path, # Path to look for tests. Default: current directory.
--module: string, # Test module to run. Default: all test modules found.
--test: string, # Individual test to run. Default: all test command found in the files.
--list, # list the selected tests without running them.
--path: path, # Path to look for tests. Default: current directory.
--module: string, # Test module to run. Default: all test modules found.
--test: string, # Individual test to run. Default: all test command found in the files.
--list, # list the selected tests without running them.
] {
let module_search_pattern = ('**' | path join ({
stem: ($module | default "*")
extension: nu
@ -197,25 +335,24 @@ export def run-tests [
let modules = (
ls ($path | path join $module_search_pattern)
| each {|row| {file: $row.name name: ($row.name | path parse | get stem)}}
| upsert commands {|module|
get-commands $module.file
| insert commands {|module|
get-annotated $module.file
}
| upsert tests {|module| $module.commands|where $it starts-with "test_"}
| filter {|x| ($x.tests|length) > 0}
| filter {|x| if ($test|is-empty) {true} else {$test in $x.tests}}
| filter {|x| ($x.commands|length) > 0 and ($x.commands.annotation|any {|y| $y in (valid-annotations|columns)})} # if file has no private functions at all or no functions annotated with a valid annotation we drop it
| upsert commands {|module|
$module.commands
| create-test-record
}
| flatten
| filter {|x| ($x.test|length) > 0}
| filter {|x| if ($test|is-empty) {true} else {$test in $x.test}}
| filter {|x| if ($module|is-empty) {true} else {$module == $x.name}}
| upsert before-each {|module| "before-each" in $module.commands}
| upsert before-all {|module| "before-all" in $module.commands}
| upsert after-each {|module| "after-each" in $module.commands}
| upsert after-all {|module| "after-all" in $module.commands}
| reject commands
| rename file name tests
| update tests {|x|
if ($test|is-empty) {
$x.tests
| update test {|x|
$x.test
| if ($test|is-empty) {
$in
} else {
$x.tests
| where $it == $test
where $it == $test
}
}
)
@ -233,9 +370,8 @@ export def run-tests [
run-tests-for-module $module
}
| flatten
| select name test result
)
if not ($results | where result == "fail" | is-empty) {
if ($results | any {|x| $x.result == fail}) {
let text = ([
$"(ansi purple)some tests did not pass (char lparen)see complete errors below(char rparen):(ansi reset)"
""
@ -245,5 +381,4 @@ export def run-tests [
error make --unspanned { msg: $text }
}
}