Skip to content

Just test PoC #18273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# import recipes from internal repo, if present
import? '../build_impl.just'
# otherwise, use the local versions
import 'build_impl.just'
9 changes: 9 additions & 0 deletions build_impl.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'lib.just'

@_build LANGUAGE:
just install {{ LANGUAGE }};

[group('build')]
build LANGUAGE: (_build LANGUAGE)

alias b := build
1 change: 1 addition & 0 deletions cpp/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import? '../../cpp.just'
4 changes: 4 additions & 0 deletions cpp/ql/test/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '../../../test.just'

[group('test')]
test *ARGS=".": (_require_semmle_code "C++ language tests") (_language_tests "cpp" ARGS)
2 changes: 2 additions & 0 deletions impl.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'build.just'
import 'test.just'
9 changes: 9 additions & 0 deletions install.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'lib.just'

@_install LANGUAGE:
cd {{ source_dir() }}; bazel run //{{ trim_end_match(LANGUAGE, '/') }}:install

[group('build')]
install LANGUAGE: (_install LANGUAGE)

alias i := install
9 changes: 9 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'build.just'
import 'test.just'
import 'install.just'
import? 'local.just'

mod rust

@_default:
{{ just_executable() }} --list --list-submodules
15 changes: 15 additions & 0 deletions lib.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import? '../semmle-code.just' # internal repo just file, if present

set fallback
set allow-duplicate-recipes
set allow-duplicate-variables
set unstable

error := style("error") + "error" + NORMAL + ":"

[no-exit-message]
@_require_semmle_code WHAT:
if [ -z "${SEMMLE_CODE:-}" ]; then \
echo "{{ error }} running {{ WHAT }} requires running from an internal repository checkout" >&2; \
exit 1; \
fi
24 changes: 24 additions & 0 deletions rust/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import '../build.just'
import '../install.just'
import '../test.just'

@_default: generate (build "rust")

[group('build')]
install LANGUAGE='rust': (_install LANGUAGE)

[group('build')]
build LANGUAGE='rust': (_build LANGUAGE)

[group('generate')]
generate:
bazel run @codeql//rust/codegen

alias gen := generate
alias g := generate

[group('test')]
integration-tests: (_test_roots 'integration' source_dir() 'ql/integration-tests')

[group('test')]
language-tests: (_test_roots 'language rust' source_dir() 'ql/test')
4 changes: 4 additions & 0 deletions rust/ql/integration-tests/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '../../../test.just'

[group('test')]
test *ARGS=".": (_integration_tests ARGS)
4 changes: 4 additions & 0 deletions rust/ql/test/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '../../../test.just'

[group('test')]
test *ARGS=".": (_language_tests "rust" ARGS)
4 changes: 4 additions & 0 deletions test.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# import recipes from internal repo, if present
import? '../test_impl.just'
# otherwise, use the local versions
import 'test_impl.just'
81 changes: 81 additions & 0 deletions test_impl.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
print_test_roots := ""

# We want to restrict how much memory codeql threads use, or we can OOM.
RAM := if os() == "linux" { "3000" } else { "2048" }

[no-cd, positional-arguments, no-exit-message]
_language_tests LANGUAGE *ARGS: (_build LANGUAGE)
shift;
codeql test run --search-path={{ source_dir() }} -j0 --ram {{ RAM }} "$@";

@_integration_tests:
echo "No integration tests implemented yet"

[no-cd, script:'python3', positional-arguments, no-exit-message]
_unit_tests *ARGS:
from pathlib import Path as P
import subprocess
import os
import sys
import shutil

def bazel_path(a, cwd):
return P(a).resolve().joinpath("...").relative_to(cwd)

flags = [arg for arg in sys.argv[1:] if arg.startswith('-')]
args = [arg for arg in sys.argv[1:] if not arg.startswith('-')]
cwd = P("{{ source_dir() }}").resolve()
bazel = shutil.which("bazel")
args[:] = [f"//{bazel_path(a, cwd)}" for a in args]
res = subprocess.run([bazel, "test"] + flags + args, cwd=cwd).returncode
sys.exit(res)

[group('test'), 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)

alias t := test

[script:'python3', positional-arguments, no-exit-message]
_test_roots KIND SOURCE_DIR *ROOTS:
import sys
import subprocess
import os
from pathlib import Path as P
workspace_dir = P("{{ justfile_dir() }}")
just = "{{ just_executable() }}"
kind = sys.argv[1]
if kind.startswith("language"):
_, _, language = kind.partition(" ")
targets = ["_language_tests", language]
else:
targets = [f"_{kind}_tests"]
source_dir = P(sys.argv[2])
roots = sys.argv[3:]
if os.environ.get("JUST_PRINT_TEST_ROOTS"):
source_dir = source_dir.relative_to(workspace_dir)
for root in roots:
print(source_dir / root)
else:
sys.exit(subprocess.run([just] + targets + roots, cwd=source_dir).returncode)
Loading