Skip to content

Commit

Permalink
adds filtering option to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astrale-sharp committed Jul 8, 2023
1 parent 5d6725f commit 9d6ee8c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
19 changes: 16 additions & 3 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ Commands:
Options:
-h, --help print this help text
--double run check with double-precision
-f, --filter <arg> only run integration tests which contain any of the
args (comma-separated). requires itest.
Examples:
check.sh fmt clippy
check.sh
check.sh --double clippy
check.sh test itest -f variant,static
RUSTUP_TOOLCHAIN=nightly check.sh
EOF

Expand Down Expand Up @@ -140,7 +143,7 @@ function cmd_test() {
function cmd_itest() {
findGodot && \
run cargo build -p itest "${extraCargoArgs[@]}" && \
run "$godotBin" --path itest/godot --headless
run "$godotBin" --path itest/godot --headless -- "[${extraArgs[@]}]"
}

function cmd_doc() {
Expand All @@ -159,6 +162,8 @@ function cmd_dok() {
# between `itest` compilations and `check.sh` runs.
extraCargoArgs=("--no-default-features")
cmds=()
eatArgsAsFilter=false
extraArgs=()

for arg in "$@"; do
case "$arg" in
Expand All @@ -172,9 +177,17 @@ for arg in "$@"; do
fmt | clippy | test | itest | doc | dok)
cmds+=("$arg")
;;
-f | --filtering)
eatArgsAsFilter=true
;;
*)
log "Unrecognized argument '$arg'. Use '$0 --help' to see what's available."
exit 2
if $eatArgsAsFilter; then
extraArgs+=("$arg")
eatArgsAsFilter=false
else
log "Unrecognized argument '$arg'. Use '$0 --help' to see what's available."
exit 2
fi
;;
esac
done
Expand Down
8 changes: 7 additions & 1 deletion itest/godot/TestRunner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ func _ready():
await get_tree().physics_frame

var allow_focus := true
var filters: Array = []
var unrecognized_args: Array = []
for arg in OS.get_cmdline_user_args():
match arg:
"--disallow-focus":
allow_focus = false
_:
unrecognized_args.push_back(arg)
if not arg.begins_with("[") or not arg.ends_with("]"):
unrecognized_args.push_back(arg)

var args = arg.lstrip("[").rstrip("]").split(",")
filters.append_array(args)

if unrecognized_args:
push_error("Unrecognized arguments: ", unrecognized_args)
Expand Down Expand Up @@ -53,6 +58,7 @@ func _ready():
gdscript_suites.size(),
allow_focus,
self,
filters
)

var exit_code: int = 0 if success else 1
Expand Down
2 changes: 1 addition & 1 deletion itest/godot/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ config_version=5

config/name="IntegrationTests"
run/main_scene="res://TestRunner.tscn"
config/features=PackedStringArray("4.0")
config/features=PackedStringArray("4.1")
run/flush_stdout_on_print=true

[debug]
Expand Down
8 changes: 6 additions & 2 deletions itest/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ unsafe impl ExtensionLibrary for runner::IntegrationTests {}
sys::plugin_registry!(__GODOT_ITEST: RustTestCase);

/// Finds all `#[itest]` tests.
fn collect_rust_tests() -> (Vec<RustTestCase>, usize, bool) {
fn collect_rust_tests(filters: &[String]) -> (Vec<RustTestCase>, usize, bool) {
let mut all_files = std::collections::HashSet::new();
let mut tests: Vec<RustTestCase> = vec![];
let mut is_focus_run = false;
Expand All @@ -99,7 +99,7 @@ fn collect_rust_tests() -> (Vec<RustTestCase>, usize, bool) {
}

// Only collect tests if normal mode, or focus mode and test is focused.
if !is_focus_run || test.focused {
if (!is_focus_run || test.focused) && passes_filter(filters, test.name) {
all_files.insert(test.file);
tests.push(*test);
}
Expand All @@ -126,3 +126,7 @@ struct RustTestCase {
line: u32,
function: fn(&TestContext),
}

pub(crate) fn passes_filter(filters: &[String], test_name: &str) -> bool {
filters.is_empty() || filters.iter().any(|x| test_name.contains(x))
}
15 changes: 12 additions & 3 deletions itest/rust/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use godot::engine::{Engine, Node};
use godot::log::godot_error;
use godot::obj::Gd;

use crate::{RustTestCase, TestContext};
use crate::{passes_filter, RustTestCase, TestContext};

#[derive(GodotClass, Debug)]
#[class(init)]
Expand All @@ -33,10 +33,19 @@ impl IntegrationTests {
gdscript_file_count: i64,
allow_focus: bool,
scene_tree: Gd<Node>,
filters: VariantArray,
) -> bool {
println!("{}Run{} Godot integration tests...", FMT_CYAN_BOLD, FMT_END);

let (rust_tests, rust_file_count, focus_run) = super::collect_rust_tests();
let filters: Vec<String> = filters.iter_shared().map(|v| v.to::<String>()).collect();
let gdscript_tests = gdscript_tests
.iter_shared()
.filter(|test| {
let test_name = get_property(test, "method_name");
passes_filter(filters.as_slice(), &test_name)
})
.collect::<Array<_>>();
let (rust_tests, rust_file_count, focus_run) =
super::collect_rust_tests(filters.as_slice());
self.focus_run = focus_run;
if focus_run {
println!(" {FMT_CYAN}Focused run{FMT_END} -- execute only selected Rust tests.")
Expand Down

0 comments on commit 9d6ee8c

Please sign in to comment.