bazel.tests: prebuild the bazel self-extraction to speed up test

Factor out the common parts of tests & cache the bazel
self-extraction (ugh) to a common store path.
This commit is contained in:
Profpatsch 2019-06-11 21:43:53 +02:00
parent 44f97b56d9
commit 629c050b5d
3 changed files with 83 additions and 33 deletions

View File

@ -1,4 +1,4 @@
{ stdenv, writeText, runCommandCC, bazel }: { stdenv, writeText, runCommandCC, bazel, runLocal, bazelTest }:
# Tests that certain executables are available in bazel-executed bash shells. # Tests that certain executables are available in bazel-executed bash shells.
@ -22,21 +22,22 @@ let
) )
''; '';
runLocal = name: script: runCommandCC name { preferLocalBuild = true; } script; workspaceDir = runLocal "our_workspace" {} ''
workspaceDir = runLocal "our_workspace" ''
mkdir $out mkdir $out
cp ${WORKSPACE} $out/WORKSPACE cp ${WORKSPACE} $out/WORKSPACE
cp ${fileIn} $out/input.txt cp ${fileIn} $out/input.txt
cp ${fileBUILD} $out/BUILD cp ${fileBUILD} $out/BUILD
''; '';
testBazel = runLocal "bazel-test-bash-tools" '' testBazel = bazelTest {
export HOME=$(mktemp -d) name = "bazel-test-bash-tools";
cp -r ${workspaceDir} wd && chmod +w wd && cd wd inherit workspaceDir;
${bazel}/bin/bazel build :tool_usage
cp bazel-genfiles/output.txt $out bazelScript = ''
echo "Testing content" && [ "$(cat $out | wc -l)" == "2" ] && echo "OK" ${bazel}/bin/bazel build :tool_usage
''; cp bazel-genfiles/output.txt $out
echo "Testing content" && [ "$(cat $out | wc -l)" == "2" ] && echo "OK"
'';
};
in testBazel in testBazel

View File

@ -1,5 +1,7 @@
{ stdenv, callPackage, lib, fetchurl, fetchpatch, runCommand, makeWrapper { stdenv, callPackage, lib, fetchurl, fetchpatch, runCommand, runCommandCC, makeWrapper
, zip, unzip, bash, writeCBin, coreutils # this package (through the fixpoint glass)
, bazel
, lr, xe, zip, unzip, bash, writeCBin, coreutils
, which, python, perl, gawk, gnused, gnutar, gnugrep, gzip, findutils , which, python, perl, gawk, gnused, gnutar, gnugrep, gzip, findutils
# Apple dependencies # Apple dependencies
, cctools, clang, libcxx, CoreFoundation, CoreServices, Foundation , cctools, clang, libcxx, CoreFoundation, CoreServices, Foundation
@ -109,10 +111,61 @@ stdenv.mkDerivation rec {
# nix-build . -A bazel.tests # nix-build . -A bazel.tests
# #
# in the nixpkgs checkout root to exercise them locally. # in the nixpkgs checkout root to exercise them locally.
passthru.tests = { passthru.tests =
pythonBinPath = callPackage ./python-bin-path-test.nix {}; let
bashTools = callPackage ./bash-tools-test.nix {}; runLocal = name: attrs: script: runCommandCC name ({
}; preferLocalBuild = true;
} // attrs) script;
# bazel wants to extract itself into $install_dir/install every time it runs,
# so lets do that only once.
extracted =
let install_dir =
# `install_base` field printed by `bazel info`, minus the hash.
# yes, this path is kinda magic. Sorry.
"$HOME/.cache/bazel/_bazel_nixbld";
in runLocal "bazel-extracted-homedir" { passthru.install_dir = install_dir; } ''
export HOME=$(mktemp -d)
touch WORKSPACE # yeah, everything sucks
install_base="$(${bazel}/bin/bazel info | grep install_base)"
# assert its actually below install_dir
[[ "$install_base" =~ ${install_dir} ]] \
|| (echo "oh no! $install_base but we are \
trying to copy ${install_dir} to $out instead!"; exit 1)
cp -R ${install_dir} $out
'';
bazelTest = { name, bazelScript, workspaceDir }:
runLocal name {} (
# skip extraction caching on Darwin, because nobody knows how Darwin works
(lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
# set up home with pre-unpacked bazel
export HOME=$(mktemp -d)
mkdir -p ${extracted.install_dir}
cp -R ${extracted}/install ${extracted.install_dir}
# https://stackoverflow.com/questions/47775668/bazel-how-to-skip-corrupt-installation-on-centos6
# Bazel checks whether the mtime of the install dir files
# is >9 years in the future, otherwise it extracts itself again.
# see PosixFileMTime::IsUntampered in src/main/cpp/util
# What the hell bazel.
${lr}/bin/lr -0 -U ${extracted.install_dir} | ${xe}/bin/xe -N0 -0 touch --date="9 years 6 months" {}
'')
+
''
# Note https://github.com/bazelbuild/bazel/issues/5763#issuecomment-456374609
# about why to create a subdir for the workspace.
cp -r ${workspaceDir} wd && chmod u+w wd && cd wd
${bazelScript}
touch $out
'');
in {
pythonBinPath = callPackage ./python-bin-path-test.nix{ inherit runLocal bazelTest; };
bashTools = callPackage ./bash-tools-test.nix { inherit runLocal bazelTest; };
};
name = "bazel-${version}"; name = "bazel-${version}";

View File

@ -1,4 +1,4 @@
{ stdenv, lib, writeText, runCommandCC, bazel }: { stdenv, lib, writeText, bazel, bazelTest, runLocal }:
let let
WORKSPACE = writeText "WORKSPACE" '' WORKSPACE = writeText "WORKSPACE" ''
@ -29,9 +29,7 @@ let
) )
''; '';
runLocal = name: script: runCommandCC name { preferLocalBuild = true; } script; workspaceDir = runLocal "our_workspace" {} ''
workspaceDir = runLocal "our_workspace" ''
mkdir $out mkdir $out
cp ${WORKSPACE} $out/WORKSPACE cp ${WORKSPACE} $out/WORKSPACE
mkdir $out/python mkdir $out/python
@ -40,17 +38,15 @@ let
cp ${pythonBUILD} $out/python/BUILD.bazel cp ${pythonBUILD} $out/python/BUILD.bazel
''; '';
testBazel = runLocal "bazel-test-builtin-rules" '' testBazel = bazelTest {
export HOME=$(mktemp -d) name = "bazel-test-builtin-rules";
# Note https://github.com/bazelbuild/bazel/issues/5763#issuecomment-456374609 inherit workspaceDir;
# about why to create a subdir for the workspace. bazelScript = ''
cp -r ${workspaceDir} wd && chmod u+w wd && cd wd ${bazel}/bin/bazel \
${bazel}/bin/bazel \ run \
run \ --host_javabase='@local_jdk//:jdk' \
--host_javabase='@local_jdk//:jdk' \ //python:bin
//python:bin '';
};
touch $out
'';
in testBazel in testBazel