diff options
Diffstat (limited to 'src')
3 files changed, 52 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt index b5afddf116..218f6aeae2 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/stub_template.txt @@ -156,6 +156,11 @@ def Main(): try: sys.stdout.flush() if IsRunningFromZip(): + # If RUN_UNDER_RUNFILES equals 1, it means we need to + # change directory to the right runfiles directory. + # (So that the data files are accessible) + if os.environ.get("RUN_UNDER_RUNFILES") == "1": + os.chdir(os.path.join(module_space, "%workspace_name%")) retCode = subprocess.call(args) shutil.rmtree(os.path.dirname(module_space), True) exit(retCode) diff --git a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java index 58aed286d0..b3100c2c10 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java @@ -72,6 +72,7 @@ public class StandaloneTestStrategy extends TestStrategy { .put("PYTHON_RUNFILES", TestPolicy.RUNFILES_DIR) .put("RUNFILES_DIR", TestPolicy.RUNFILES_DIR) .put("TEST_TMPDIR", TestPolicy.TEST_TMP_DIR) + .put("RUN_UNDER_RUNFILES", "1") .build(); public static final TestPolicy DEFAULT_LOCAL_POLICY = new TestPolicy(ENV_VARS); diff --git a/src/test/shell/bazel/bazel_windows_example_test.sh b/src/test/shell/bazel/bazel_windows_example_test.sh index c62963561c..c8fb2bef07 100755 --- a/src/test/shell/bazel/bazel_windows_example_test.sh +++ b/src/test/shell/bazel/bazel_windows_example_test.sh @@ -167,5 +167,51 @@ function test_native_python_with_python3() { fi } +function test_python_test_with_data() { + touch BUILD + touch WORKSPACE + + mkdir data + cat >data/BUILD <<EOF +filegroup( + name = "test_data", + srcs = ["data.txt"], + visibility = ["//visibility:public"], +) +EOF + + cat >data/data.txt <<EOF +hello world +EOF + + mkdir src + cat >src/BUILD <<EOF +py_test( + name = "data_test", + srcs = ["data_test.py"], + data = ["//data:test_data"], +) +EOF + + cat >src/data_test.py <<EOF +import unittest +import os + +class MyTest(unittest.TestCase): + + def test_data(self): + with open("data/data.txt") as f: + line = f.readline().strip() + self.assertEqual(line, "hello world") + +if __name__ == '__main__': + print("CWD = " + os.getcwd()) + unittest.main() +EOF + + bazel test --test_output=errors //src:data_test >& $TEST_log \ + || fail "Test //src:test failed while expecting success" +} + run_suite "examples on Windows" |