Skip to content

Commit

Permalink
update tests to use rules_tests; implement commented out case; update…
Browse files Browse the repository at this point in the history
… real examples to use runfiles-root relative paths
  • Loading branch information
rickeylev committed Nov 25, 2024
1 parent d09ad2c commit 39e7e5a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 57 deletions.
2 changes: 1 addition & 1 deletion tests/bootstrap_impls/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ sh_py_run_test(
target_compatible_with = _SUPPORTS_BOOTSTRAP_SCRIPT,
)

relative_path_test_suite(name = "relative_path_test")
relative_path_test_suite(name = "relative_path_tests")
95 changes: 39 additions & 56 deletions tests/bootstrap_impls/venv_relative_path_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,110 +12,93 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"Unit tests for yaml.bzl"
"Unit tests for relative_path computation"

load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python/private:py_executable_bazel.bzl", "relative_path") # buildifier: disable=bzl-visibility

def _relative_path_test_impl(ctx):
env = unittest.begin(ctx)
_tests = []

def _relative_path_test(env):
# Basic test cases

asserts.equals(
env,
"../../c/d",
env.expect.that_str(
relative_path(
from_ = "a/b",
to = "c/d",
),
)
).equals("../../c/d")

asserts.equals(
env,
"../../c/d",
env.expect.that_str(
relative_path(
from_ = "../a/b",
to = "../c/d",
),
)
).equals("../../c/d")

asserts.equals(
env,
"../../../c/d",
env.expect.that_str(
relative_path(
from_ = "../a/b",
to = "../../c/d",
),
)
).equals("../../../c/d")

asserts.equals(
env,
"../../d",
env.expect.that_str(
relative_path(
from_ = "a/b/c",
to = "a/d",
),
)

asserts.equals(
env,
"d/e",
).equals("../../d")
env.expect.that_str(
relative_path(
from_ = "a/b/c",
to = "a/b/c/d/e",
),
)
).equals("d/e")

# Real examples

# external py_binary uses external python runtime
asserts.equals(
env,
"../../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
env.expect.that_str(
relative_path(
from_ = "../rules_python~/python/private/_py_console_script_gen_py.venv/bin",
to = "../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
from_ = "other_repo~/python/private/_py_console_script_gen_py.venv/bin",
to = "rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
),
).equals(
"../../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
)

# internal py_binary uses external python runtime
asserts.equals(
env,
"../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
env.expect.that_str(
relative_path(
from_ = "test/version_default.venv/bin",
to = "../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
from_ = "_main/test/version_default.venv/bin",
to = "rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
),
).equals(
"../../../../rules_python~~python~python_3_9_x86_64-unknown-linux-gnu/bin/python3",
)

# external py_binary uses internal python runtime
# asserts.equals(
# env,
# "???",
# relative_path(
# from_ = "../rules_python~/python/private/_py_console_script_gen_py.venv/bin",
# to = "python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
# ),
#)
# ^ TODO: Technically we can infer ".." to be the workspace name?
env.expect.that_str(
relative_path(
from_ = "other_repo~/python/private/_py_console_script_gen_py.venv/bin",
to = "_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
),
).equals(
"../../../../../_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
)

# internal py_binary uses internal python runtime
asserts.equals(
env,
"../../../python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
env.expect.that_str(
relative_path(
from_ = "scratch/main.venv/bin",
to = "python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
from_ = "_main/scratch/main.venv/bin",
to = "_main/python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
),
).equals(
"../../../python/python_3_9_x86_64-unknown-linux-gnu/bin/python3",
)

return unittest.end(env)

relative_path_test = unittest.make(
_relative_path_test_impl,
attrs = {},
)
_tests.append(_relative_path_test)

def relative_path_test_suite(*, name):
unittest.suite(name, relative_path_test)
test_suite(name = name, basic_tests = _tests)

0 comments on commit 39e7e5a

Please sign in to comment.