From 3d93b381b848f53ee6479d732f1821c8a724dc2d Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Thu, 12 Dec 2024 09:50:38 +0100 Subject: [PATCH] Just test PoC This is a rough PoC showcasing [just](https://github.com/casey/just) as a builder/test runner. After installing `just` you can (in this PoC can only be `rust`): * `just install ` to install a language pack in-tree `` can be omitted if running from inside its directory. * `just build ` will build `target/intree/codeql-` from the internal repository, if available, otherwise it will fall back to `install`. * `just test TESTS... FLAGS...` will run the provided tests, if they are of the same kind (integration or QL tests), passing FLAGS to the runner. For QL tests the appropriate `build ` is run. Notably, for QL tests this command works also if using `codeql` standalone from the internal repository (using `install` and `codeql` from `PATH`). If running from within a test directory, `TESTS` can be omitted and defaults to all tests in the current directory. --- impl.just | 58 ++++++++++++++++++++++++++++++ justfile | 5 +++ rust/justfile | 4 +++ rust/ql/integration-tests/justfile | 3 ++ rust/ql/test/justfile | 5 +++ 5 files changed, 75 insertions(+) create mode 100755 impl.just create mode 100755 justfile create mode 100644 rust/justfile create mode 100644 rust/ql/integration-tests/justfile create mode 100755 rust/ql/test/justfile diff --git a/impl.just b/impl.just new file mode 100755 index 0000000000000..5284bbafea065 --- /dev/null +++ b/impl.just @@ -0,0 +1,58 @@ +#!/usr/bin/env just --justfile + +set allow-duplicate-recipes +set allow-duplicate-variables +set unstable +set quiet + +_install LANGUAGE: + bazel run //{{ trim_end_match(LANGUAGE, '/') }}:install + +_build LANGUAGE: + if [ -n ${SEMMLE_CODE:-} ]; then \ + cd $SEMMLE_CODE; ./build target/intree/codeql-{{ LANGUAGE }}; \ + else \ + just install {{ LANGUAGE }}; \ + fi + +build LANGUAGE: (_build LANGUAGE) +install LANGUAGE: (_install LANGUAGE) + +[no-cd, script:'python3', positional-arguments, no-exit-message] +test +ARGS: # TODO: fuzzy test chooser when no arguments are provided! + import pathlib + import subprocess + import os + import sys + # avoid infinite recursion: this happens when arguments have kinds of tests + if os.environ.get("CODEQL_JUSTFILE_TEST"): + print("No common test handler found", file=sys.stderr) + sys.exit(1) + os.environ["CODEQL_JUSTFILE_TEST"] = "true" + + flags = [arg for arg in sys.argv[1:] if arg.startswith('-')] + args = [arg for arg in sys.argv[1:] if not arg.startswith('-')] + common_path = pathlib.Path(os.path.commonpath(args)).resolve() + if not common_path.is_dir(): + common_path = common_path.parent + ret = subprocess.run( + ['{{ just_executable() }}', 'test'] + flags + [pathlib.Path(a).resolve().relative_to(common_path) for a in args], + cwd=common_path).returncode + sys.exit(ret) + +[no-cd] +_run_language_tests LANGUAGE *ARGS: (_build LANGUAGE) + if [ -n ${SEMMLE_CODE:-} ]; then \ + $SEMMLE_CODE/target/intree/codeql-{{ LANGUAGE }}/codeql test run $ARGS; \ + else \ + codeql --search-path={{ source_dir() }} test run $ARGS; \ + fi + +[no-cd] +_run_integration_tests *ARGS: + if [ -n ${SEMMLE_CODE:-} ]; then \ + $SEMMLE_CODE/tools/pytest $ARGS; \ + else \ + echo "integration tests require running from an internal repository working copy" >&2; \ + exit 1; \ + fi diff --git a/justfile b/justfile new file mode 100755 index 0000000000000..e2c44720524d3 --- /dev/null +++ b/justfile @@ -0,0 +1,5 @@ +import 'impl.just' +import? '../justfile' # internal repo just file, if present + +@_default: + {{ just_executable() }} --list diff --git a/rust/justfile b/rust/justfile new file mode 100644 index 0000000000000..1bbf38aef00f2 --- /dev/null +++ b/rust/justfile @@ -0,0 +1,4 @@ +import '../justfile' + +install LANGUAGE='rust': (_install LANGUAGE) +build LANGUAGE='rust': (_build LANGUAGE) diff --git a/rust/ql/integration-tests/justfile b/rust/ql/integration-tests/justfile new file mode 100644 index 0000000000000..3e4a93107a2e5 --- /dev/null +++ b/rust/ql/integration-tests/justfile @@ -0,0 +1,3 @@ +import '../../justfile' + +test *ARGS=".": (_run_integration_tests ARGS) diff --git a/rust/ql/test/justfile b/rust/ql/test/justfile new file mode 100755 index 0000000000000..fa355af10764f --- /dev/null +++ b/rust/ql/test/justfile @@ -0,0 +1,5 @@ +#!/usr/bin/env just --justfile + +import '../../justfile' + +test *ARGS=".": (_run_language_tests "rust" ARGS)