From 0c617b39f1f5fe04233439399c177519a7c4353c Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Wed, 13 Sep 2023 11:37:17 -0700 Subject: [PATCH] Load Python rules in integration tests instead of using builtins (#2) This is to facilitate moving the rule implementation out of Bazel itself and into rules_python. This also makes the tests pass when an allowlist is used to enforce that the Python rules are being loaded through rules_python instead of used from builtins. This the same change as before, but with some internal fixes applied. PiperOrigin-RevId: 565113515 Change-Id: I999dbfaace8cce2168e718093fe418d548ab1b73 --- .../test_cc_shared_library/BUILD.builtin_test | 1 + src/test/shell/integration/aquery_test.sh | 2 ++ .../shell/integration/configured_query_test.sh | 15 +++++++++++++++ .../integration/modify_execution_info_test.sh | 2 ++ .../shell/integration/nestedset_as_skykey_test.sh | 2 ++ .../integration/param_file_flag_per_line_test.sh | 1 + src/test/shell/integration/python_stub_test.sh | 6 ++++++ src/test/shell/integration/python_test.sh | 4 ++++ src/test/shell/integration/run_test.sh | 9 +++++++-- src/test/shell/integration/runfiles_test.sh | 4 ++++ .../shell/integration/stub_finds_runfiles_test.sh | 3 +++ 11 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test index 88e2202ce01942..c60473a3c432e3 100644 --- a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test @@ -13,6 +13,7 @@ load( "exports_test", ) load("@rules_testing//lib:analysis_test.bzl", "analysis_test") +load("@rules_python//python:py_test.bzl", "py_test") LINKABLE_MORE_THAN_ONCE = "LINKABLE_MORE_THAN_ONCE" diff --git a/src/test/shell/integration/aquery_test.sh b/src/test/shell/integration/aquery_test.sh index 70fc640b8d97fb..05b1c4fcc5b664 100755 --- a/src/test/shell/integration/aquery_test.sh +++ b/src/test/shell/integration/aquery_test.sh @@ -1430,6 +1430,8 @@ function test_aquery_include_template_substitution_for_template_expand_of_py_bin local pkg="${FUNCNAME[0]}" mkdir -p "$pkg" || fail "mkdir -p $pkg" cat > "$pkg/BUILD" <<'EOF' +load("@rules_python//python:py_binary.bzl", "py_binary") + py_binary( name='foo', srcs=['foo.py'] diff --git a/src/test/shell/integration/configured_query_test.sh b/src/test/shell/integration/configured_query_test.sh index 4ddac2aefca188..b853bd67912a17 100755 --- a/src/test/shell/integration/configured_query_test.sh +++ b/src/test/shell/integration/configured_query_test.sh @@ -255,6 +255,8 @@ function test_show_transitive_config_fragments() { local -r pkg=$FUNCNAME mkdir -p $pkg cat > $pkg/BUILD <<'EOF' +load("@rules_python//python:py_library.bzl", "py_library") + cc_library( name = "cclib", srcs = ["mylib.cc"], @@ -326,6 +328,8 @@ function test_show_transitive_config_fragments_alias() { local -r pkg=$FUNCNAME mkdir -p $pkg cat > $pkg/BUILD <<'EOF' +load("@rules_python//python:py_library.bzl", "py_library") + cc_library( name = "cclib_with_py_dep", srcs = ["mylib2.cc"], @@ -385,6 +389,8 @@ function test_show_transitive_config_fragments_host_deps() { local -r pkg=$FUNCNAME mkdir -p $pkg cat > $pkg/BUILD <<'EOF' +load("@rules_python//python:py_library.bzl", "py_library") + cc_library( name = "cclib_with_py_dep", srcs = ["mylib2.cc"], @@ -414,6 +420,8 @@ function test_show_transitive_config_fragments_through_output_file() { local -r pkg=$FUNCNAME mkdir -p $pkg cat > $pkg/BUILD <<'EOF' +load("@rules_python//python:py_library.bzl", "py_library") + cc_library( name = "cclib_with_py_dep", srcs = ["mylib2.cc"], @@ -443,6 +451,8 @@ function test_show_direct_config_fragments() { local -r pkg=$FUNCNAME mkdir -p $pkg cat > $pkg/BUILD <<'EOF' +load("@rules_python//python:py_library.bzl", "py_library") + cc_library( name = "cclib", srcs = ["mylib.cc"], @@ -866,6 +876,8 @@ function test_starlark_output_mode() { local -r pkg=$FUNCNAME mkdir -p $pkg cat > $pkg/BUILD <<'EOF' +load("@rules_python//python:py_library.bzl", "py_library") + py_library( name = "pylib", srcs = ["pylib.py"], @@ -991,6 +1003,7 @@ EOF cat > $pkg/BUILD <<'EOF' load(":rules.bzl", "bool_flag", "list_flag", "root_rule") +load("@rules_python//python:py_library.bzl", "py_library") exports_files(["rules.bzl"]) @@ -1060,6 +1073,8 @@ function test_starlark_build_options_invalid_arg() { mkdir -p $pkg cat > $pkg/BUILD <<'EOF' +load("@rules_python//python:py_library.bzl", "py_library") + py_library( name = "foo", srcs = ["pylib.py"], diff --git a/src/test/shell/integration/modify_execution_info_test.sh b/src/test/shell/integration/modify_execution_info_test.sh index 9e98dc7fc21fbe..d627c99202c5d7 100755 --- a/src/test/shell/integration/modify_execution_info_test.sh +++ b/src/test/shell/integration/modify_execution_info_test.sh @@ -170,6 +170,8 @@ EOF mkdir -p "$pkg" || fail "mkdir -p $pkg" echo "load('//$pkg:shell.bzl', 'starlark_shell')" > "$pkg/BUILD" cat >> "$pkg/BUILD" <<'EOF' +load("@rules_python//python:py_binary.bzl", "py_binary") + starlark_shell( name = "shelly", output = "ok.txt", diff --git a/src/test/shell/integration/nestedset_as_skykey_test.sh b/src/test/shell/integration/nestedset_as_skykey_test.sh index 6669c51079f315..567aca22704c0e 100755 --- a/src/test/shell/integration/nestedset_as_skykey_test.sh +++ b/src/test/shell/integration/nestedset_as_skykey_test.sh @@ -128,6 +128,8 @@ function test_dirty_file() { export DONT_SANITY_CHECK_SERIALIZATION=1 cat > foo/BUILD < package/BUILD < test/BUILD << EOF +load("@rules_python//python:py_binary.bzl", "py_binary") + py_binary(name = "main3", python_version = "PY3", srcs = ["main3.py"], @@ -91,6 +93,8 @@ EOF function test_can_build_py_library_at_top_level_regardless_of_version() { mkdir -p test cat > test/BUILD << EOF +load("@rules_python//python:py_library.bzl", "py_library") + py_library( name = "lib3", srcs = ["lib3.py"], @@ -112,6 +116,8 @@ function test_python_through_bash_without_runfile_links() { mkdir -p python_through_bash cat > python_through_bash/BUILD << EOF +load("@rules_python//python:py_binary.bzl", "py_binary") + py_binary( name = "inner", srcs = ["inner.py"], diff --git a/src/test/shell/integration/python_test.sh b/src/test/shell/integration/python_test.sh index 8b73fe9b17da3a..16c68f15df52e1 100755 --- a/src/test/shell/integration/python_test.sh +++ b/src/test/shell/integration/python_test.sh @@ -84,6 +84,8 @@ fi function test_python_binary_empty_files_in_runfiles_are_regular_files() { mkdir -p test/mypackage cat > test/BUILD <<'EOF' +load("@rules_python//python:py_test.bzl", "py_test") + py_test( name = "a", srcs = [ @@ -128,6 +130,8 @@ function test_building_transitive_py_binary_runfiles_trees() { touch main.py script.sh chmod u+x script.sh cat > BUILD <<'EOF' +load("@rules_python//python:py_binary.bzl", "py_binary") + py_binary( name = 'py-tool', srcs = ['main.py'], diff --git a/src/test/shell/integration/run_test.sh b/src/test/shell/integration/run_test.sh index 9fb73d72b5f37d..c1f38cb96fbb48 100755 --- a/src/test/shell/integration/run_test.sh +++ b/src/test/shell/integration/run_test.sh @@ -65,8 +65,13 @@ add_to_bazelrc "test --notest_loasd" function write_py_files() { mkdir -p py || fail "mkdir py failed" - echo "py_binary(name = 'binary', srcs = ['binary.py'])" > py/BUILD - echo "py_test(name = 'test', srcs = ['test.py'])" >> py/BUILD + cat > py/BUILD <<'EOF' +load("@rules_python//python:py_binary.bzl", "py_binary") +load("@rules_python//python:py_test.bzl", "py_test") + +py_binary(name = "binary", srcs = ["binary.py"]) +py_test(name = "test", srcs = ["test.py"]) +EOF echo "print('Hello, Python World!')" >py/py.py chmod +x py/py.py diff --git a/src/test/shell/integration/runfiles_test.sh b/src/test/shell/integration/runfiles_test.sh index fe4edc6ef07283..ba9c054e14e77c 100755 --- a/src/test/shell/integration/runfiles_test.sh +++ b/src/test/shell/integration/runfiles_test.sh @@ -150,11 +150,15 @@ function test_foo_runfiles() { local -r pkg=$FUNCNAME create_pkg $pkg cat > BUILD << EOF +load("@rules_python//python:py_library.bzl", "py_library") + py_library(name = "root", srcs = ["__init__.py"], visibility = ["//visibility:public"]) EOF cat > $pkg/BUILD << EOF +load("@rules_python//python:py_binary.bzl", "py_binary") + sh_binary(name = "foo", srcs = [ "x/y/z.sh" ], data = [ ":py", diff --git a/src/test/shell/integration/stub_finds_runfiles_test.sh b/src/test/shell/integration/stub_finds_runfiles_test.sh index aeb3d97b68ed7c..de7948c2cc1d6a 100755 --- a/src/test/shell/integration/stub_finds_runfiles_test.sh +++ b/src/test/shell/integration/stub_finds_runfiles_test.sh @@ -37,6 +37,9 @@ fi function set_up() { mkdir -p pkg pkg/java cat > pkg/BUILD << 'EOF' +load("@rules_python//python:py_binary.bzl", "py_binary") +load("@rules_python//python:py_test.bzl", "py_test") + java_binary(name = "javabin", main_class = "test.ExitZero", srcs = [ "java/ExitZero.java", ])