#!/bin/bash # # Copyright 2015 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Test rules provided in Bazel not tested by examples # # Load test environment source $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/test-setup.sh \ || { echo "test-setup.sh not found!" >&2; exit 1; } function test_sh_test() { mkdir -p a cat > a/BUILD < a/success_test.sh < a/fail_test.sh < mypkg/echoer.sh <&2 exit 1 fi echo EXTRA ACTION FILE: \$1 EOF chmod +x mypkg/echoer.sh cat > mypkg/Hello.java < mypkg/BUILD <& $TEST_log \ || fail "Building with action listener failed" expect_log "EXTRA ACTION FILE" } function test_with_arguments() { mkdir -p mypkg cat > mypkg/BUILD < mypkg/check_expected_argument.sh < BUILD < true.sh <pkg/BUILD cc_library( name = "a", srcs = ["a.cc"], ) cc_library( name = "b", srcs = ["b.cc"], deps = [":a"], ) cc_binary( name = "main", srcs = ["main.cc"], deps = [":b"], ) EOF cat <<'EOF' >pkg/a.cc #include std::string get_hello(std::string world) { return "Hello, " + world + "!"; } EOF cat <<'EOF' >pkg/b.cc #include #include std::string get_hello(std::string); void print_hello(std::string world) { std::cout << get_hello(world) << std::endl; } EOF cat <<'EOF' >pkg/main.cc #include void print_hello(std::string); int main() { print_hello(std::string("World")); } EOF bazel build //pkg:a >& $TEST_log \ || fail "Failed to build //pkg:a" bazel build //pkg:b >& $TEST_log \ || fail "Failed to build //pkg:b" bazel run //pkg:main >& $TEST_log \ || fail "Failed to run //pkg:main" expect_log "Hello, World!" ./bazel-bin/pkg/main >& $TEST_log \ || fail "Failed to run //pkg:main" expect_log "Hello, World!" } function test_genrule_default_env() { mkdir -p pkg cat <<'EOF' >pkg/BUILD genrule( name = "test", outs = ["test.out"], cmd = "(echo \"PATH=$$PATH\"; echo \"TMPDIR=$$TMPDIR\") > $@", ) EOF local old_path="${PATH-}" local old_tmpdir="${TMPDIR-}" export PATH="/bin:/usr/bin:/random/path" export TMPDIR="/some/path" # batch mode to force reload of the environment bazel --batch build //pkg:test || fail "Failed to build //pkg:test" export PATH="$old_path" export TMPDIR="$old_tmpdir" assert_contains "PATH=/bin:/usr/bin:/random/path" \ bazel-genfiles/pkg/test.out assert_contains "TMPDIR=/some/path" \ bazel-genfiles/pkg/test.out } function test_genrule_remote() { cat > WORKSPACE < package/BUILD < package/in.sh << EOF #!/bin/bash echo "Hi" EOF chmod +x package/in.sh bazel build @r//package:abs_dep >$TEST_log 2>&1 || fail "Should build" } function test_genrule_remote_d() { cat > WORKSPACE < package/BUILD <<'EOF' genrule( name = "hi", outs = [ "a/b", "c/d" ], cmd = "echo 'hi' | tee $(@D)/a/b $(@D)/c/d", ) EOF bazel build @r//package:hi >$TEST_log 2>&1 || fail "Should build" expect_log bazel-genfiles/external/r/package/a/b expect_log bazel-genfiles/external/r/package/c/d } function test_python_with_workspace_name() { create_new_workspace cd ${new_workspace_dir} mkdir -p {module_a,module_b} local remote_path="${new_workspace_dir}" cat > module_a/BUILD < module_b/BUILD < module_a/foo.py < module_b/bar.py < module_b/bar2.py < WORKSPACE < module1/BUILD < module2/BUILD < module1/fib.py < module2/bez.py <$TEST_log expect_log "The number is 42" expect_log "Print the number 42" expect_log "Fib(10) is 89" bazel run @remote//module_b:bar2 >$TEST_log expect_log "The number is 42" } run_suite "rules test"