Skip to content
Merged
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
7 changes: 5 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module(name = "jsonnet", version = "0.0.0")
module(
name = "jsonnet",
version = "0.0.0",
)

bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest")
bazel_dep(name = "rules_python", version = "0.40.0")

build_defs = use_extension("//tools/build_defs:extensions.bzl", "build_defs")
use_repo(
build_defs,
"default_python3_headers",
"io_bazel_rules_jsonnet",
)

register_toolchains("//platform_defs:default_python3_toolchain")
13 changes: 3 additions & 10 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ workspace(name = "jsonnet")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "io_bazel_rules_jsonnet",
commit = "ad2b4204157ddcf7919e8bd210f607f8a801aa7f",
remote = "https://github.com/bazelbuild/rules_jsonnet.git",
shallow_since = "1556260764 +0200",
)

git_repository(
name = "com_google_googletest",
remote = "https://github.com/google/googletest.git",
# If updating googletest version, also update CMakeLists.txt.in.
commit = "2fe3bd994b3189899d93f1d5a881e725e046fdc2", # release: release-1.8.1
commit = "2fe3bd994b3189899d93f1d5a881e725e046fdc2", # release: release-1.8.1
remote = "https://github.com/google/googletest.git",
shallow_since = "1535728917 -0400",
)

Expand All @@ -22,5 +15,5 @@ register_toolchains("//platform_defs:default_python3_toolchain")

# This allows building C++ against python3 headers.
load("//tools/build_defs:python_repo.bzl", "python_headers")
python_headers(name = "default_python3_headers")

python_headers(name = "default_python3_headers")
27 changes: 7 additions & 20 deletions cpp/testdata/BUILD
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
package(default_visibility = ["//visibility:public"])
load("//tools/build_defs:golden_test.bzl", "jsonnet_json_golden_test")

load(
"@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl",
"jsonnet_to_json_test",
"jsonnet_library",
)
package(default_visibility = ["//visibility:public"])

jsonnet_to_json_test(
jsonnet_json_golden_test(
name = "example_test",
src = "example.jsonnet",
golden = "example_golden.json",
)

jsonnet_to_json_test(
jsonnet_json_golden_test(
name = "importing_test",
src = "importing.jsonnet",
data = ["example.jsonnet"],
golden = "importing_golden.json",
imports = ["."],
deps = [":testlib"],
)

jsonnet_to_json_test(
jsonnet_json_golden_test(
name = "invalid_test",
src = "invalid.jsonnet",
error = 1,
expect_error = True,
golden = "invalid.out",
)

jsonnet_library(
name = "testlib",
srcs = [
"example.jsonnet",
"importing.jsonnet",
],
)

filegroup(
name = "testdata",
srcs = [
Expand Down
7 changes: 3 additions & 4 deletions platform_defs/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
load("@rules_python//python:defs.bzl", "py_runtime", "py_runtime_pair")

py_runtime(
name = "python3",
interpreter_path = "/usr/bin/python3",
stub_shebang = "#!/usr/bin/env python3",
python_version = "PY3",
stub_shebang = "#!/usr/bin/env python3",
)

py_runtime_pair(
Expand All @@ -16,6 +16,5 @@ py_runtime_pair(
toolchain(
name = "default_python3_toolchain",
toolchain = ":just_python3",
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
toolchain_type = "@rules_python//python:toolchain_type",
)

4 changes: 3 additions & 1 deletion python/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

cc_binary(
name = "_jsonnet.so",
srcs = ["_jsonnet.c"],
Expand All @@ -21,8 +23,8 @@ py_test(
srcs = ["_jsonnet_test.py"],
data = [
"testdata/basic_check.jsonnet",
"testdata/binary1230123.bin",
"testdata/binary123.bin",
"testdata/binary1230123.bin",
"testdata/trivial.jsonnet",
"testdata/trivial_no_eol.jsonnet",
],
Expand Down
2 changes: 1 addition & 1 deletion stdlib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ filegroup(

cc_library(
name = "std",
hdrs = ["std.jsonnet.h"],
srcs = [":gen-std-jsonnet-h"],
hdrs = ["std.jsonnet.h"],
includes = ["."],
linkstatic = 1,
)
Expand Down
8 changes: 8 additions & 0 deletions test_suite/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package(default_visibility = ["//visibility:private"])

# Export the tests.source so it can be used by Bazel tests elsewhere in the repo.
sh_library(
name = "tests_sh_lib",
srcs = ["tests.source"],
visibility = ["//tools/build_defs:__pkg__"],
)
2 changes: 1 addition & 1 deletion third_party/json/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
licenses(["permissive"])

package(default_visibility = ["//visibility:public"])

cc_library(
Expand All @@ -10,4 +11,3 @@ cc_library(
],
includes = ["."],
)

4 changes: 2 additions & 2 deletions third_party/md5/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
licenses(["permissive"])

package(default_visibility = ["//visibility:public"])

cc_library(
Expand All @@ -9,7 +10,6 @@ cc_library(
hdrs = [
"md5.h",
],
linkopts = ["-lm"],
includes = ["."],
linkopts = ["-lm"],
)

9 changes: 1 addition & 8 deletions tools/build_defs/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
# load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load(":python_repo.bzl", "python_headers")

def _impl(repository_ctx):
git_repository(
name = "io_bazel_rules_jsonnet",
commit = "ad2b4204157ddcf7919e8bd210f607f8a801aa7f",
remote = "https://github.com/bazelbuild/rules_jsonnet.git",
shallow_since = "1556260764 +0200",
)

python_headers(name = "default_python3_headers")

build_defs = module_extension(
Expand Down
87 changes: 87 additions & 0 deletions tools/build_defs/golden_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Bazel rules for running golden tests."""

def _gen_test_script(ctx):
return """\
#!/bin/bash

set -uo pipefail

# We don't run 'init' or 'deinit' from tests.source, as these
# just create & remove a TMP dir and Bazel deals with that for us;
# instead we just need to ensure that the path for the Bazel
# managed temp dir is available in TMP_DIR.

TMP_DIR="$TEST_TMPDIR"
VERBOSE=true
DISABLE_ERROR_TESTS=
SUMMARY_ONLY=

source ./test_suite/tests.source

GOLDEN_OUTPUT=$(cat '{golden_path}')

test_eval '{jsonnet_path}' '{input_path}' '{expected_exit_code}' "$GOLDEN_OUTPUT" '{golden_kind}'

if [ $FAILED -eq 0 ] ; then
echo "$0: All $EXECUTED test scripts pass."
exit 0
else
echo "$0: FAILED: $FAILED / $EXECUTED"
exit 1
fi
""".format(
jsonnet_path = ctx.executable.jsonnet.short_path,
input_path = ctx.file.src.short_path,
expected_exit_code = 1 if ctx.attr.expect_error else 0,
golden_path = ctx.file.golden.short_path,
golden_kind = "PLAIN",
)

def _jsonnet_json_golden_test_impl(ctx):
test_script = ctx.actions.declare_file(ctx.label.name)
ctx.actions.write(
output = test_script,
is_executable = True,
content = _gen_test_script(ctx),
)
return DefaultInfo(
executable = test_script,
runfiles = ctx.runfiles(
transitive_files = ctx.attr._test_sh_lib.files,
files = [
ctx.executable.jsonnet,
ctx.file.src,
ctx.file.golden,
] + ctx.files.data,
),
)

jsonnet_json_golden_test = rule(
implementation = _jsonnet_json_golden_test_impl,
test = True,
attrs = {
"src": attr.label(
mandatory = True,
allow_single_file = True,
),
"data": attr.label_list(allow_files = True),
"golden": attr.label(
mandatory = True,
allow_single_file = True,
),
"jsonnet": attr.label(
default = "//cmd:jsonnet",
executable = True,
cfg = "exec",
),
"expect_error": attr.bool(
doc = "If True, the golden file is the expected stderr output from jsonnet",
),
"canonicalize_golden": attr.bool(
doc = "If True, the golden file will be reformatted prior to comparing against the jsonnet output",
),
"_test_sh_lib": attr.label(
default = "//test_suite:tests_sh_lib",
),
},
)
41 changes: 21 additions & 20 deletions tools/build_defs/python_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ cc_library(
"""

def _impl(repository_ctx):
rctx = repository_ctx
if "/" in rctx.attr.path or "\\" in rctx.attr.path:
# Canonicalize the path
realpath = rctx.path(rctx.attr.path)
else:
# Find it in $PATH
realpath = rctx.which(rctx.attr.path)
rctx.symlink(realpath, "bin/python")
include_path = rctx.execute([
realpath, "-c", "import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())",
])
if include_path.return_code != 0:
fail("Failed to locate Python headers:\n" + include_path.stderr)
rctx.symlink(include_path.stdout.strip(), "include")
rctx.file(
"WORKSPACE",
'workspace(name = "{}")\n'.format(rctx.name),
)
rctx.file("BUILD", build_file_contents)

rctx = repository_ctx
if "/" in rctx.attr.path or "\\" in rctx.attr.path:
# Canonicalize the path
realpath = rctx.path(rctx.attr.path)
else:
# Find it in $PATH
realpath = rctx.which(rctx.attr.path)
rctx.symlink(realpath, "bin/python")
include_path = rctx.execute([
realpath,
"-c",
"import distutils.sysconfig; print(distutils.sysconfig.get_python_inc())",
])
if include_path.return_code != 0:
fail("Failed to locate Python headers:\n" + include_path.stderr)
rctx.symlink(include_path.stdout.strip(), "include")
rctx.file(
"WORKSPACE",
'workspace(name = "{}")\n'.format(rctx.name),
)
rctx.file("BUILD", build_file_contents)

python_headers = repository_rule(
implementation = _impl,
Expand Down
Loading