Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ build --incompatible_default_to_explicit_init_py

# Windows makes use of runfiles for some rules
build --enable_runfiles
# TODO(f0rmiga): remove this so that other features don't start relying on it.
startup --windows_enable_symlinks
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag is required to enable runfiles on windows

1 change: 1 addition & 0 deletions python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ filegroup(
name = "files",
srcs = glob(
include = [
"*.exe",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interpreter was missing from py3_runtime.all_files on windows

"bin/**",
"DLLs/**",
"extensions/**",
Expand Down
45 changes: 33 additions & 12 deletions python/tests/toolchains/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

load("//python:versions.bzl", "PLATFORMS", "TOOL_VERSIONS")

_WINDOWS_RUNNER_TEMPLATE = """\
@ECHO OFF
set PATHEXT=.COM;.EXE;.BAT
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way windows figures out something is executable is through this environment variable.

powershell.exe -c "& ./{interpreter_path} {run_acceptance_test_py}"
"""

def _acceptance_test_impl(ctx):
workspace = ctx.actions.declare_file("/".join([ctx.attr.python_version, "WORKSPACE"]))
ctx.actions.expand_template(
Expand Down Expand Up @@ -52,7 +58,8 @@ def _acceptance_test_impl(ctx):
},
)

py3_runtime = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime
toolchain = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"]
py3_runtime = toolchain.py3_runtime
interpreter_path = py3_runtime.interpreter_path
if not interpreter_path:
interpreter_path = py3_runtime.interpreter.short_path
Expand All @@ -61,9 +68,9 @@ def _acceptance_test_impl(ctx):
executable = ctx.actions.declare_file("run_test_{}.bat".format(ctx.attr.python_version))
ctx.actions.write(
output = executable,
content = "call {interpreter_path} {run_acceptance_test_py}".format(
interpreter_path = interpreter_path.replace("/", "\\"),
run_acceptance_test_py = run_acceptance_test_py.short_path.replace("/", "\\"),
content = _WINDOWS_RUNNER_TEMPLATE.format(
interpreter_path = interpreter_path.replace("../", "external/"),
run_acceptance_test_py = run_acceptance_test_py.short_path,
),
is_executable = True,
)
Expand Down Expand Up @@ -96,26 +103,40 @@ def _acceptance_test_impl(ctx):
)]

_acceptance_test = rule(
_acceptance_test_impl,
implementation = _acceptance_test_impl,
doc = "TODO",
attrs = {
"is_windows": attr.bool(mandatory = True),
"python_version": attr.string(mandatory = True),
"test_location": attr.string(mandatory = True),
"is_windows": attr.bool(
doc = "TODO",
mandatory = True,
),
"python_version": attr.string(
doc = "TODO",
mandatory = True,
),
"test_location": attr.string(
doc = "TODO",
mandatory = True,
),
"_build_bazel_tmpl": attr.label(
doc = "TODO",
allow_single_file = True,
default = "//python/tests/toolchains/workspace_template:BUILD.bazel.tmpl",
default = Label("//python/tests/toolchains/workspace_template:BUILD.bazel.tmpl"),
),
"_python_version_test": attr.label(
doc = "TODO",
allow_single_file = True,
default = "//python/tests/toolchains/workspace_template:python_version_test.py",
default = Label("//python/tests/toolchains/workspace_template:python_version_test.py"),
),
"_run_acceptance_test": attr.label(
doc = "TODO",
allow_single_file = True,
default = "//python/tests/toolchains:run_acceptance_test.py",
default = Label("//python/tests/toolchains:run_acceptance_test.py"),
),
"_workspace_tmpl": attr.label(
doc = "TODO",
allow_single_file = True,
default = "//python/tests/toolchains/workspace_template:WORKSPACE.tmpl",
default = Label("//python/tests/toolchains/workspace_template:WORKSPACE.tmpl"),
),
},
test = True,
Expand Down
42 changes: 34 additions & 8 deletions python/tests/toolchains/run_acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,49 @@

import os
import subprocess
import sys
import tempfile
import unittest


class TestPythonVersion(unittest.TestCase):
@classmethod
def setUpClass(cls):
os.chdir("%test_location%")
python_version_test_dirname = os.path.dirname(
os.path.realpath("python_version_test.py")
)
rules_python_path = os.path.normpath(
os.path.join(python_version_test_dirname, "..", "..", "..", "..")
)

is_windows = "win" in sys.platform
if is_windows:
newline = "\r\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe python has a built in constant for this. Would be nice.

os.environ["HOME"] = tempfile.mkdtemp()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use TEST_TMPDIR provided by Bazel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that'd be much better since it should show up in sandbox paths, making things more debuggable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
os.environ["HOME"] = tempfile.mkdtemp()
home = os.path.join(os.environ["TEST_TMPDIR"], "HOME")
os.mkdir(home)
os.environ["HOME"] = home

os.environ["LocalAppData"] = tempfile.mkdtemp()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel requires these variables be set or some flags be explicitly provided. This is easier IMO

else:
newline = "\n"

with open(".bazelrc", "w") as bazelrc:
bazelrc.write(
newline.join(
[
'build --override_repository rules_python="{}"'.format(
rules_python_path.replace("\\", "/")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel can understand Windows paths with forward slashes.

),
"build --test_output=errors",
]
)
)

def test_match_toolchain(self):
stream = os.popen("bazel run @python_toolchain_host//:python3 -- --version")
output = stream.read()
self.assertEqual(output, "Python %python_version%\n")
output = stream.read().strip()
self.assertEqual(output, "Python %python_version%")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline can be different on different system. Better to just strip whitespace.


subprocess.run("bazel test //...", shell=True, check=True)


if __name__ == "__main__":
os.chdir("%test_location%")
python_version_test_dirname = os.path.dirname(os.path.realpath("python_version_test.py"))
rules_python_path = os.path.join(python_version_test_dirname, "..", "..", "..", "..")
with open(".bazelrc", "w") as bazelrc:
bazelrc.write("build --override_repository rules_python=\"{}\"\n".format(rules_python_path))
bazelrc.write("build --test_output=errors\n")
unittest.main()