Skip to content

Commit

Permalink
Disable WORKSPACE in shell integration tests
Browse files Browse the repository at this point in the history
Final part of #23087

Working towards #23023

PiperOrigin-RevId: 661381915
Change-Id: I1fca0e24bf2f424a3e438ab6fb68e9ae815118f6
  • Loading branch information
meteorcloudy authored and copybara-github committed Aug 9, 2024
1 parent 8cee5f5 commit ca02978
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 156 deletions.
19 changes: 1 addition & 18 deletions src/test/shell/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@rules_python//python:defs.bzl", "py_test")
load("//:workspace_deps.bzl", "gen_workspace_stanza")

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

Expand All @@ -11,6 +10,7 @@ exports_files([
"sandboxing_test_utils.sh",
"unittest.bash",
"shell_utils.sh",
"testenv.sh",
])

filegroup(
Expand All @@ -22,23 +22,6 @@ filegroup(
visibility = ["//src:__pkg__"],
)

gen_workspace_stanza(
name = "testenv_sh",
out = "testenv.sh",
repos = [
"bazel_skylib",
"rules_cc",
"rules_java",
"rules_license",
"rules_pkg",
"rules_proto",
"rules_python",
"rules_testing",
],
template = "testenv.sh.tmpl",
visibility = ["//visibility:public"],
)

sh_library(
name = "bashunit",
srcs = [
Expand Down
4 changes: 1 addition & 3 deletions src/test/shell/bazel/bazel_coverage_hermetic_py_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${CURRENT_DIR}/../integration_test_setup.sh" \
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }

disable_bzlmod

# Fetch hermetic python and register toolchain.
function set_up() {
cat >>WORKSPACE <<EOF
cat >>MODULE.bazel <<EOF
register_toolchains(
"//:python_toolchain",
)
Expand Down
2 changes: 0 additions & 2 deletions src/test/shell/bazel/cc_import_starlark_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ function test_all_starlark_written_tests() {

cd "$workspace_dir"

setup_skylib_support

# TODO(gnish): Re-enable tests once bazel picks up changes.
# bazel test --experimental_starlark_cc_import tools/build_defs/cc/tests:cc_import_tests
}
Expand Down
3 changes: 0 additions & 3 deletions src/test/shell/integration/bazel_command_log_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ source "${CURRENT_DIR}/../integration_test_setup.sh" \

log="$(bazel --batch info command_log)"

# Less network flakiness without bzlmod
disable_bzlmod

function tear_down() {
# Clean up after ourselves.
bazel --nobatch shutdown
Expand Down
3 changes: 0 additions & 3 deletions src/test/shell/integration/client_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${CURRENT_DIR}/../integration_test_setup.sh" \
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }

# Less network flakiness without bzlmod
disable_bzlmod

function strip_lines_from_bazel_cc() {
# sed can't redirect back to its input file (it'll only generate an empty
# file). In newer versions of gnu sed there is a -i option to edit in place.
Expand Down
9 changes: 7 additions & 2 deletions src/test/shell/integration/dump_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ EOF
expect_log '"PACKAGE:b":'

bazel dump --memory=full,count >& $TEST_log || fail "dump failed"
# //a, //b, //external and EXTERNAL_PACKAGE
expect_log_n '"com.google.devtools.build.lib.skyframe.PackageValue": 1' 4
if [[ $PRODUCT_NAME == "bazel" ]]; then
# WORSKAPCE disabled: //a, //b
expect_log_n '"com.google.devtools.build.lib.skyframe.PackageValue": 1' 2
else
# WORKSPACE enabled: //a, //b, //external and EXTERNAL_PACKAGE
expect_log_n '"com.google.devtools.build.lib.skyframe.PackageValue": 1' 4
fi
}

function test_memory_needle() {
Expand Down
6 changes: 5 additions & 1 deletion src/test/shell/integration/loading_phase_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,15 @@ function test_bazel_bin_is_not_a_package() {
local -r pkg="${FUNCNAME[0]}"
mkdir "$pkg" || fail "Could not mkdir $pkg"
echo "filegroup(name = '$pkg')" > "$pkg/BUILD"
add_bazel_skylib "MODULE.bazel"
# Ensure bazel-<pkg> is created.
bazel build --symlink_prefix="foo_prefix-" "//$pkg" || fail "build failed"
[[ -d "foo_prefix-bin" ]] || fail "bazel-bin was not created"

if [[ $PRODUCT_NAME == "bazel" ]]; then
# Remove tools dir set up by copy_tools_directory from testenv.sh
rm -rf tools
fi

# Assert that "//..." does not expand to //foo_prefix-*
bazel query //... >& "$TEST_log"
expect_log_once "//$pkg:$pkg"
Expand Down
1 change: 1 addition & 0 deletions src/test/shell/integration/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ add_to_bazelrc "test --notest_loasd"
#### HELPER FUNCTIONS ##################################################

function write_py_files() {
add_rules_python "MODULE.bazel"
mkdir -p py || fail "mkdir py failed"

cat > py/BUILD <<'EOF'
Expand Down
127 changes: 3 additions & 124 deletions src/test/shell/testenv.sh.tmpl → src/test/shell/testenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ BAZEL_RUNFILES="$TEST_SRCDIR/_main"

# WORKSPACE file
workspace_file="${BAZEL_RUNFILES}/WORKSPACE"
distdir_bzl_file="${BAZEL_RUNFILES}/distdir.bzl"

# Where to register toolchains
TOOLCHAIN_REGISTRAION_FILE="MODULE.bazel"
Expand Down Expand Up @@ -306,6 +305,9 @@ build --incompatible_use_toolchain_resolution_for_java_rules
# Enable Bzlmod in all shell integration tests
common --enable_bzlmod
# Disable WORKSPACE in all shell integration tests
common --noenable_workspace
# Verify compatibility before the flip (https://github.com/bazelbuild/bazel/issues/12821)
common --nolegacy_external_runfiles
Expand Down Expand Up @@ -537,119 +539,6 @@ function setup_objc_test_support() {
IOS_SDK_VERSION=$(xcrun --sdk iphoneos --show-sdk-version)
}

function setup_skylib_support() {
mkdir -p rules/private
touch rules/private/BUILD
cat >> WORKSPACE << EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{bazel_skylib}
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()
EOF
}

function add_rules_cc_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_cc}
EOF
}

function add_rules_java_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_java}
EOF
}

function add_rules_license_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_license}
EOF
}

function add_rules_pkg_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_pkg}
EOF
}

function add_rules_proto_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_proto}
EOF
}

function add_rules_python_to_workspace() {
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_python}
EOF
}

function add_rules_testing_to_workspace() {
mkdir lib
touch lib/BUILD
cat >> "$1"<<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{rules_testing}
EOF
}

function create_workspace_with_default_repos() {
write_workspace_file "${1:-WORKSPACE}" "${2:-main}"
workspace_file=${1:-WORKSPACE}
write_workspace_file "${workspace_file}" "${2:-main}"
write_default_lockfile "$(dirname ${workspace_file})/MODULE.bazel.lock"
echo "$1"
}

# Write the default WORKSPACE file, wiping out any custom WORKSPACE setup.
function write_workspace_file() {
cat > "$1" << EOF
workspace(name = "$2")
EOF
add_rules_cc_to_workspace "WORKSPACE"
add_rules_java_to_workspace "WORKSPACE"
add_rules_license_to_workspace "WORKSPACE"
add_rules_pkg_to_workspace "WORKSPACE"
add_rules_proto_to_workspace "WORKSPACE"
add_rules_python_to_workspace "WORKSPACE"

maybe_setup_python_windows_workspace
}

# If the current platform is Windows, registers our custom Windows Python
# toolchain. Otherwise does nothing.
#
# Since this modifies the WORKSPACE file, it must be called between test cases.
function maybe_setup_python_windows_workspace() {
if [[ ! $PLATFORM =~ msys ]]; then
return
fi

# --extra_toolchains has left-to-right precedence semantics, but the bazelrc
# is processed before the command line. This means that any matching
# toolchains added to the bazelrc will always take precedence over toolchains
# set up by test cases. Instead, we add the toolchain to WORKSPACE so that it
# has lower priority than whatever is passed on the command line.
cat >> WORKSPACE << EOF
register_toolchains("//tools/python/windows:py_toolchain")
EOF
}

# Extract the module version used in the default lock file.
function get_version_from_default_lock_file() {
lockfile=$(rlocation io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock)
Expand Down Expand Up @@ -717,13 +606,6 @@ function setup_module_dot_bazel() {
echo $module_dot_bazel
}

# Set up a lockfile to avoid accessing BCR for tests with a clean workspace.
function write_default_lockfile() {
module_lockfile=${1:-MODULE.bazel.lock}
touch "$(dirname ${module_lockfile})/MODULE.bazel"
cp -f $(rlocation io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock) ${module_lockfile}
}

workspaces=()
# Set-up a new, clean workspace with only the tools installed.
function create_new_workspace() {
Expand All @@ -736,8 +618,6 @@ function create_new_workspace() {

copy_tools_directory

write_workspace_file "WORKSPACE" "$WORKSPACE_NAME"

# Suppress the echo from setup_module_dot_bazel
setup_module_dot_bazel > /dev/null

Expand Down Expand Up @@ -775,7 +655,6 @@ function cleanup_workspace() {
try_with_timeout rm -fr "$i"
fi
done
write_workspace_file "WORKSPACE" "$WORKSPACE_NAME"
# Suppress the echo from setup_module_dot_bazel
setup_module_dot_bazel > /dev/null
fi
Expand Down

0 comments on commit ca02978

Please sign in to comment.