Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate testing module and bzlmod tests for toolchains/python #335

Merged
merged 13 commits into from
Feb 23, 2023
Merged
3 changes: 3 additions & 0 deletions .github/build-and-test
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ if [[ ${BZLMOD_ENABLED-} = true ]]; then
toolchains/java
toolchains/posix
toolchains/rust
toolchains/python
)
declare -ra test_dirs=(
testing/core
testing/go-bzlmod
testing/java
testing/posix
testing/rust
testing/python
)
declare -ra extra_flags=(
--config=bzlmod
Expand All @@ -60,6 +62,7 @@ else
testing/java
testing/posix
testing/rust
testing/python
)
declare -ra extra_flags=(
)
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ optional.
<pre>
nixpkgs_python_configure(<a href="#nixpkgs_python_configure-name">name</a>, <a href="#nixpkgs_python_configure-python2_attribute_path">python2_attribute_path</a>, <a href="#nixpkgs_python_configure-python2_bin_path">python2_bin_path</a>, <a href="#nixpkgs_python_configure-python3_attribute_path">python3_attribute_path</a>,
<a href="#nixpkgs_python_configure-python3_bin_path">python3_bin_path</a>, <a href="#nixpkgs_python_configure-repository">repository</a>, <a href="#nixpkgs_python_configure-repositories">repositories</a>, <a href="#nixpkgs_python_configure-nix_file_deps">nix_file_deps</a>, <a href="#nixpkgs_python_configure-nixopts">nixopts</a>,
<a href="#nixpkgs_python_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_python_configure-quiet">quiet</a>, <a href="#nixpkgs_python_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_python_configure-target_constraints">target_constraints</a>)
<a href="#nixpkgs_python_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_python_configure-quiet">quiet</a>, <a href="#nixpkgs_python_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_python_configure-target_constraints">target_constraints</a>, <a href="#nixpkgs_python_configure-register">register</a>)
</pre>

Define and register a Python toolchain provided by nixpkgs.
Expand Down Expand Up @@ -1600,6 +1600,15 @@ default is <code>None</code>
Constraints for the target platform.

</p>
</td>
</tr>
<tr id="nixpkgs_python_configure-register">
<td><code>register</code></td>
<td>

optional.
default is <code>True</code>

</td>
</tr>
</tbody>
Expand Down
3 changes: 3 additions & 0 deletions testing/python/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import %workspace%/../../.bazelrc.common
import %workspace%/../../.bazelrc.cc
import %workspace%/../../.bazelrc.java
Empty file added testing/python/BUILD.bazel
Empty file.
52 changes: 52 additions & 0 deletions testing/python/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module(name = "rules_nixpkgs_python_testing")

bazel_dep(name = "rules_nixpkgs_python")
local_path_override(
module_name = "rules_nixpkgs_python",
path = "../../toolchains/python",
)

bazel_dep(name = "rules_nixpkgs_core")
local_path_override(
module_name = "rules_nixpkgs_core",
path = "../../core",
)

bazel_dep(name = "rules_nixpkgs_java")
local_path_override(
module_name = "rules_nixpkgs_java",
path = "../../toolchains/java",
)

bazel_dep(name = "rules_nixpkgs_cc")
local_path_override(
module_name = "rules_nixpkgs_cc",
path = "../../toolchains/cc",
)

bazel_dep(name = "bazel_skylib", version = "1.0.3")
bazel_dep(name = "rules_cc", version = "0.0.4")

# TODO[AH] Remove these transitive dependencies once nixpkgs_java_configure has
# become a module extension in rules_nixpkgs_java.
bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "rules_java", version = "4.0.0")
java_toolchains = use_extension("@rules_java//java:extensions.bzl", "toolchains")
use_repo(java_toolchains, "remote_java_tools")

non_module_deps = use_extension("//:non_module_deps.bzl", "non_module_deps")

use_repo(non_module_deps, "nixpkgs_java_runtime")
use_repo(non_module_deps, "nixpkgs_java_runtime_toolchain")
register_toolchains("@nixpkgs_java_runtime_toolchain//:all")

use_repo(non_module_deps, "nixpkgs_config_cc")
use_repo(non_module_deps, "nixpkgs_config_cc_info")
use_repo(non_module_deps, "nixpkgs_config_cc_toolchains")
register_toolchains("@nixpkgs_config_cc_toolchains//:all")

use_repo(non_module_deps, "nixpkgs")
use_repo(non_module_deps, "nixpkgs_python_toolchain")
use_repo(non_module_deps, "nixpkgs_python_configure_test")
use_repo(non_module_deps, "nixpkgs_python_toolchain_python3")
register_toolchains("@nixpkgs_python_toolchain//:all")
43 changes: 43 additions & 0 deletions testing/python/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
workspace(name = "rules_nixpkgs_python_testing")

local_repository(
name = "rules_nixpkgs_python",
path = "../../toolchains/python",
)

local_repository(
name = "rules_nixpkgs_core",
path = "../../core",
)

local_repository(
name = "rules_nixpkgs_java",
path = "../../toolchains/java",
)

local_repository(
name = "rules_nixpkgs_cc",
path = "../../toolchains/cc",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "bazel_skylib",
sha256 = "f24ab666394232f834f74d19e2ff142b0af17466ea0c69a3f4c276ee75f6efce",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.0/bazel-skylib-1.4.0.tar.gz",
],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

load(
"//tests:nixpkgs_repositories.bzl",
"nixpkgs_repositories",
)

nixpkgs_repositories(bzlmod = False)
Empty file.
1 change: 1 addition & 0 deletions testing/python/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions testing/python/nixpkgs.nix
11 changes: 11 additions & 0 deletions testing/python/non_module_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load(
"//tests:nixpkgs_repositories.bzl",
"nixpkgs_repositories",
)

def _non_module_deps_impl(ctx):
nixpkgs_repositories(bzlmod = True)

non_module_deps = module_extension(
implementation = _non_module_deps_impl,
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
load("@nixpkgs_python_toolchain//:defs.bzl", "interpreter")
package(default_testonly = 1)

# Test nixpkgs_python_configure() by running some Python code.
test_suite(
name = "python-test",
Expand All @@ -9,8 +12,10 @@ test_suite(

py_test(
name = "resolve-interpreter",
main = "@interpreter_test//:test.py",
srcs = ["@interpreter_test//:test.py"],
main = "interpreter_test.py",
srcs = ["interpreter_test.py"],
args = ["$(rootpath {interpreter})".format(interpreter = interpreter)],
data = [interpreter],
srcs_version = "PY3",
python_version = "PY3",
visibility = ["//visibility:public"],
Expand Down
12 changes: 12 additions & 0 deletions testing/python/tests/interpreter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys
import os

executable_path = os.path.realpath(sys.executable)
interpreter_path = os.path.realpath(sys.argv[1])

print("executable_path = " + executable_path)
print("interpreter_path = " + interpreter_path)

if executable_path != interpreter_path:
sys.stderr.write("sys.executable different to interpreter target")
sys.exit(1)
42 changes: 42 additions & 0 deletions testing/python/tests/nixpkgs_repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_local_repository")
load("@rules_nixpkgs_cc//:cc.bzl", "nixpkgs_cc_configure")
load("@rules_nixpkgs_java//:java.bzl", "nixpkgs_java_configure")
load("@rules_nixpkgs_python//:python.bzl", "nixpkgs_python_configure")
load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package")

def nixpkgs_repositories(*, bzlmod):
nixpkgs_local_repository(
name = "nixpkgs",
nix_file = "//:nixpkgs.nix",
nix_file_deps = ["//:flake.lock"],
)

# Tests implicitly depend on Java
nixpkgs_java_configure(
name = "nixpkgs_java_runtime",
attribute_path = "jdk11.home",
repository = "@nixpkgs",
toolchain = True,
register = not bzlmod,
toolchain_name = "nixpkgs_java",
toolchain_version = "11",
)

# Python depends on a CC toolchain being available
nixpkgs_cc_configure(
name = "nixpkgs_config_cc",
repository = "@nixpkgs",
register = not bzlmod,
)

nixpkgs_package(
name = "nixpkgs_python_configure_test",
nix_file = "//tests:python-test.nix",
repository = "@nixpkgs",
)

nixpkgs_python_configure(
python2_attribute_path = "python2",
repository = "@nixpkgs",
register = not bzlmod,
)
4 changes: 4 additions & 0 deletions toolchains/python/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Skip docs directories in bzlmod mode. Stardoc is not yet bzlmod compatible.
build:bzlmod --deleted_packages=docs
build:bzlmod --registry=file://%workspace%/../../registry

import %workspace%/../../.bazelrc.common
import %workspace%/../../.bazelrc.cc
import %workspace%/../../.bazelrc.java
11 changes: 10 additions & 1 deletion toolchains/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Rules for importing a Python toolchain from Nixpkgs.
<pre>
nixpkgs_python_configure(<a href="#nixpkgs_python_configure-name">name</a>, <a href="#nixpkgs_python_configure-python2_attribute_path">python2_attribute_path</a>, <a href="#nixpkgs_python_configure-python2_bin_path">python2_bin_path</a>, <a href="#nixpkgs_python_configure-python3_attribute_path">python3_attribute_path</a>,
<a href="#nixpkgs_python_configure-python3_bin_path">python3_bin_path</a>, <a href="#nixpkgs_python_configure-repository">repository</a>, <a href="#nixpkgs_python_configure-repositories">repositories</a>, <a href="#nixpkgs_python_configure-nix_file_deps">nix_file_deps</a>, <a href="#nixpkgs_python_configure-nixopts">nixopts</a>,
<a href="#nixpkgs_python_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_python_configure-quiet">quiet</a>, <a href="#nixpkgs_python_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_python_configure-target_constraints">target_constraints</a>)
<a href="#nixpkgs_python_configure-fail_not_supported">fail_not_supported</a>, <a href="#nixpkgs_python_configure-quiet">quiet</a>, <a href="#nixpkgs_python_configure-exec_constraints">exec_constraints</a>, <a href="#nixpkgs_python_configure-target_constraints">target_constraints</a>, <a href="#nixpkgs_python_configure-register">register</a>)
</pre>

Define and register a Python toolchain provided by nixpkgs.
Expand Down Expand Up @@ -220,6 +220,15 @@ default is <code>None</code>
Constraints for the target platform.

</p>
</td>
</tr>
<tr id="nixpkgs_python_configure-register">
<td><code>register</code></td>
<td>

optional.
default is <code>True</code>

</td>
</tr>
</tbody>
Expand Down
28 changes: 0 additions & 28 deletions toolchains/python/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,3 @@ load("@rules_nixpkgs_docs//:dependencies_2.bzl", "docs_dependencies_2")
docs_dependencies_2()

### end generic dependencies for rendering documentation

### dependencies for tests

load("//:python.bzl", "nixpkgs_python_configure")

nixpkgs_python_configure(
python2_attribute_path = "python2",
repository = "@nixpkgs", # `@nixpkgs` reused from `docs_dependencies_2`
)

load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_package")

nixpkgs_package(
name = "nixpkgs_python_configure_test",
nix_file = "//tests:python-test.nix",
repository = "@nixpkgs",
)

# The interpreter_test rule creates a repository with a generated test.py file.
# It takes a target label for a python interpreter, which we load from defs.bzl
# in the python toolchain repository. The generated test.py file is run by the
# //tests:resolve-interpreter test target. It checks the resolved interpreter
# label matches the sys.executable path.
load("//tests:interpreter-test.bzl", "interpreter_test")
load("@nixpkgs_python_toolchain//:defs.bzl", "interpreter")
interpreter_test(name = "interpreter_test", interpreter_label = interpreter)

### end dependencies for tests
8 changes: 6 additions & 2 deletions toolchains/python/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ runCommand "bazel-nixpkgs-python-toolchain"
''}}
visibility = ["//visibility:public"],
)
exports_files(["bin/python"])
EOF
''
""".format(
Expand All @@ -126,7 +127,8 @@ def nixpkgs_python_configure(
fail_not_supported = True,
quiet = False,
exec_constraints = None,
target_constraints = None):
target_constraints = None,
register = True):
"""Define and register a Python toolchain provided by nixpkgs.

Creates `nixpkgs_package`s for Python 2 or 3 `py_runtime` instances and a
Expand Down Expand Up @@ -195,4 +197,6 @@ def nixpkgs_python_configure(
exec_constraints = exec_constraints,
target_constraints = target_constraints,
)
native.register_toolchains("@%s//:toolchain" % name)

if register:
native.register_toolchains("@{}//:toolchain".format(name))
28 changes: 0 additions & 28 deletions toolchains/python/tests/interpreter-test.bzl

This file was deleted.