-
-
Notifications
You must be signed in to change notification settings - Fork 594
tests(pystar): py_runtime_pair and py_runtime analysis tests #1441
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2023 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. | ||
|
||
load(":py_runtime_tests.bzl", "py_runtime_test_suite") | ||
|
||
py_runtime_test_suite(name = "py_runtime_tests") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
# Copyright 2023 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. | ||
"""Starlark tests for py_runtime rule.""" | ||
|
||
load("@rules_python_internal//:rules_python_config.bzl", "config") | ||
load("@rules_testing//lib:analysis_test.bzl", "analysis_test") | ||
load("@rules_testing//lib:test_suite.bzl", "test_suite") | ||
load("@rules_testing//lib:truth.bzl", "matching") | ||
load("@rules_testing//lib:util.bzl", rt_util = "util") | ||
load("//python:py_runtime.bzl", "py_runtime") | ||
load("//python:py_runtime_info.bzl", "PyRuntimeInfo") | ||
load("//tests:py_runtime_info_subject.bzl", "py_runtime_info_subject") | ||
load("//tests/base_rules:util.bzl", br_util = "util") | ||
|
||
_tests = [] | ||
|
||
_SKIP_TEST = { | ||
"target_compatible_with": ["@platforms//:incompatible"], | ||
} | ||
|
||
def _test_bootstrap_template(name): | ||
# The bootstrap_template arg isn't present in older Bazel versions, so | ||
# we have to conditionally pass the arg and mark the test incompatible. | ||
if config.enable_pystar: | ||
py_runtime_kwargs = {"bootstrap_template": "bootstrap.txt"} | ||
attr_values = {} | ||
else: | ||
py_runtime_kwargs = {} | ||
attr_values = _SKIP_TEST | ||
|
||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
interpreter_path = "/py", | ||
python_version = "PY3", | ||
**py_runtime_kwargs | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_bootstrap_template_impl, | ||
attr_values = attr_values, | ||
) | ||
|
||
def _test_bootstrap_template_impl(env, target): | ||
env.expect.that_target(target).provider( | ||
PyRuntimeInfo, | ||
factory = py_runtime_info_subject, | ||
).bootstrap_template().path().contains("bootstrap.txt") | ||
|
||
_tests.append(_test_bootstrap_template) | ||
|
||
def _test_cannot_have_both_inbuild_and_system_interpreter(name): | ||
if br_util.is_bazel_6_or_higher(): | ||
py_runtime_kwargs = { | ||
"interpreter": "fake_interpreter", | ||
"interpreter_path": "/some/path", | ||
} | ||
attr_values = {} | ||
else: | ||
py_runtime_kwargs = { | ||
"interpreter_path": "/some/path", | ||
} | ||
attr_values = _SKIP_TEST | ||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
python_version = "PY3", | ||
**py_runtime_kwargs | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_cannot_have_both_inbuild_and_system_interpreter_impl, | ||
expect_failure = True, | ||
attr_values = attr_values, | ||
) | ||
|
||
def _test_cannot_have_both_inbuild_and_system_interpreter_impl(env, target): | ||
env.expect.that_target(target).failures().contains_predicate( | ||
matching.str_matches("one of*interpreter*interpreter_path"), | ||
) | ||
|
||
_tests.append(_test_cannot_have_both_inbuild_and_system_interpreter) | ||
|
||
def _test_cannot_specify_files_for_system_interpreter(name): | ||
if br_util.is_bazel_6_or_higher(): | ||
py_runtime_kwargs = {"files": ["foo.txt"]} | ||
attr_values = {} | ||
else: | ||
py_runtime_kwargs = {} | ||
attr_values = _SKIP_TEST | ||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
interpreter_path = "/foo", | ||
python_version = "PY3", | ||
**py_runtime_kwargs | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_cannot_specify_files_for_system_interpreter_impl, | ||
expect_failure = True, | ||
attr_values = attr_values, | ||
) | ||
|
||
def _test_cannot_specify_files_for_system_interpreter_impl(env, target): | ||
env.expect.that_target(target).failures().contains_predicate( | ||
matching.str_matches("files*must be empty"), | ||
) | ||
|
||
_tests.append(_test_cannot_specify_files_for_system_interpreter) | ||
|
||
def _test_in_build_interpreter(name): | ||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
interpreter = "fake_interpreter", | ||
python_version = "PY3", | ||
files = ["file1.txt"], | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_in_build_interpreter_impl, | ||
) | ||
|
||
def _test_in_build_interpreter_impl(env, target): | ||
info = env.expect.that_target(target).provider(PyRuntimeInfo, factory = py_runtime_info_subject) | ||
info.python_version().equals("PY3") | ||
info.files().contains_predicate(matching.file_basename_equals("file1.txt")) | ||
info.interpreter().path().contains("fake_interpreter") | ||
|
||
_tests.append(_test_in_build_interpreter) | ||
|
||
def _test_must_have_either_inbuild_or_system_interpreter(name): | ||
if br_util.is_bazel_6_or_higher(): | ||
py_runtime_kwargs = {} | ||
attr_values = {} | ||
else: | ||
py_runtime_kwargs = { | ||
"interpreter_path": "/some/path", | ||
} | ||
attr_values = _SKIP_TEST | ||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
python_version = "PY3", | ||
**py_runtime_kwargs | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_must_have_either_inbuild_or_system_interpreter_impl, | ||
expect_failure = True, | ||
attr_values = attr_values, | ||
) | ||
|
||
def _test_must_have_either_inbuild_or_system_interpreter_impl(env, target): | ||
env.expect.that_target(target).failures().contains_predicate( | ||
matching.str_matches("one of*interpreter*interpreter_path"), | ||
) | ||
|
||
_tests.append(_test_must_have_either_inbuild_or_system_interpreter) | ||
|
||
def _test_python_version_required(name): | ||
# Bazel 5.4 will entirely crash when python_version is missing. | ||
if br_util.is_bazel_6_or_higher(): | ||
py_runtime_kwargs = {} | ||
attr_values = {} | ||
else: | ||
py_runtime_kwargs = {"python_version": "PY3"} | ||
attr_values = _SKIP_TEST | ||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
interpreter_path = "/math/pi", | ||
**py_runtime_kwargs | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_python_version_required_impl, | ||
expect_failure = True, | ||
attr_values = attr_values, | ||
) | ||
|
||
def _test_python_version_required_impl(env, target): | ||
env.expect.that_target(target).failures().contains_predicate( | ||
matching.str_matches("must be set*PY2*PY3"), | ||
) | ||
|
||
_tests.append(_test_python_version_required) | ||
|
||
def _test_system_interpreter(name): | ||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
interpreter_path = "/system/python", | ||
python_version = "PY3", | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_system_interpreter_impl, | ||
) | ||
|
||
def _test_system_interpreter_impl(env, target): | ||
env.expect.that_target(target).provider( | ||
PyRuntimeInfo, | ||
factory = py_runtime_info_subject, | ||
).interpreter_path().equals("/system/python") | ||
|
||
_tests.append(_test_system_interpreter) | ||
|
||
def _test_system_interpreter_must_be_absolute(name): | ||
# Bazel 5.4 will entirely crash when an invalid interpreter_path | ||
# is given. | ||
if br_util.is_bazel_6_or_higher(): | ||
py_runtime_kwargs = {"interpreter_path": "relative/path"} | ||
attr_values = {} | ||
else: | ||
py_runtime_kwargs = {"interpreter_path": "/junk/value/for/bazel5.4"} | ||
attr_values = _SKIP_TEST | ||
rt_util.helper_target( | ||
py_runtime, | ||
name = name + "_subject", | ||
python_version = "PY3", | ||
**py_runtime_kwargs | ||
) | ||
analysis_test( | ||
name = name, | ||
target = name + "_subject", | ||
impl = _test_system_interpreter_must_be_absolute_impl, | ||
expect_failure = True, | ||
attr_values = attr_values, | ||
) | ||
|
||
def _test_system_interpreter_must_be_absolute_impl(env, target): | ||
env.expect.that_target(target).failures().contains_predicate( | ||
matching.str_matches("must be*absolute"), | ||
) | ||
|
||
_tests.append(_test_system_interpreter_must_be_absolute) | ||
|
||
def py_runtime_test_suite(name): | ||
test_suite( | ||
name = name, | ||
tests = _tests, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.