Skip to content

Commit

Permalink
Just test PoC
Browse files Browse the repository at this point in the history
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 <language> can only be
`rust`):
* `just install <language>` to install a language pack in-tree
  `<language>` can be omitted if running from inside its directory.
* `just build <language>` will build `target/intree/codeql-<language>`
  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 <language>` 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.
  • Loading branch information
Paolo Tranquilli committed Dec 12, 2024
1 parent 066db76 commit 1d88e13
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
61 changes: 61 additions & 0 deletions impl.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/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 test args are of different kinds
# or for different languages, or also if they are across the external/internal
# repository boundary
# TODO: allow some degree of mixing maybe?
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
5 changes: 5 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'impl.just'
import? '../justfile' # internal repo just file, if present

@_default:
{{ just_executable() }} --list
4 changes: 4 additions & 0 deletions rust/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '../justfile'

install LANGUAGE='rust': (_install LANGUAGE)
build LANGUAGE='rust': (_build LANGUAGE)
3 changes: 3 additions & 0 deletions rust/ql/integration-tests/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import '../../justfile'

test *ARGS=".": (_run_integration_tests ARGS)
5 changes: 5 additions & 0 deletions rust/ql/test/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env just --justfile

import '../../justfile'

test *ARGS=".": (_run_language_tests "rust" ARGS)

0 comments on commit 1d88e13

Please sign in to comment.