Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a python rez package on the fly and use it as a dependency on …
Browse files Browse the repository at this point in the history
…our test packages.

This will hopefully fix issues when there is no system python available.

Signed-off-by: Jean-Christophe Morin <jean_christophe_morin@hotmail.com>
JeanChristopheMorinPerso committed Jan 22, 2024
1 parent 227ae8e commit 8e52a3c
Showing 15 changed files with 82 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -18,4 +18,5 @@ __pycache__
.vscode/
.venv/
docs/source/api/
docs/source/commands/
docs/source/commands/
__tests_pkg_repo/
58 changes: 48 additions & 10 deletions src/rez/cli/selftest.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
import sys
import inspect
import argparse
import shutil
from pkgutil import iter_modules

try:
@@ -38,6 +39,9 @@ def setup_parser(parser, completions=False):
"flag shadowed pytest '–capture=no' shorthand '-s', so the long "
"name must be used for disabling stdout/err capturing in pytest."
)
parser.add_argument(
"--keep-tmpdirs", action="store_true", help="Keep temporary directories."
)

# make an Action that will append the appropriate test to the "--test" arg
class AddTestModuleAction(argparse.Action):
@@ -74,22 +78,34 @@ def command(opts, parser, extra_arg_groups=None):
if opts.only_shell:
os.environ["__REZ_SELFTEST_SHELL"] = opts.only_shell

if opts.keep_tmpdirs:
os.environ["REZ_KEEP_TMPDIRS"] = "1"

if not opts.module_tests and not opts.tests:
module_tests = all_module_tests
else:
module_tests = opts.module_tests

if use_pytest:
cwd = os.getcwd()
os.chdir(tests_dir)
try:
run_pytest(module_tests, opts.tests, opts.verbose,
extra_arg_groups)
finally:
os.chdir(cwd)
repo = os.path.join(os.getcwd(), "__tests_pkg_repo")
os.makedirs(repo, exist_ok=True)
create_python_package(os.path.join(os.getcwd(), "__tests_pkg_repo"))

else:
run_unittest(module_tests, opts.tests, opts.verbose)
os.environ["__REZ_SELFTEST_PYTHON_REPO"] = repo

try:
if use_pytest:
cwd = os.getcwd()
os.chdir(tests_dir)
try:
run_pytest(module_tests, opts.tests, opts.verbose,
extra_arg_groups)
finally:
os.chdir(cwd)

else:
run_unittest(module_tests, opts.tests, opts.verbose)
finally:
shutil.rmtree(repo)


def run_unittest(module_tests, tests, verbosity):
@@ -133,3 +149,25 @@ def run_pytest(module_tests, tests, verbosity, extra_arg_groups):

exitcode = main(args=argv)
sys.exit(exitcode)


def create_python_package(repo):
from rez.package_maker import make_package
from rez.utils.lint_helper import env, system
import venv

print("Creating python package in {0!r}".format(repo))

def make_root(variant, root):
venv.create(root)

def commands():
if system.platform == "windows":
env.PATH.prepend("{this.root}/Scripts")
else:
env.PATH.prepend("{this.root}/bin")

with make_package("python", repo, make_root=make_root, warn_on_skip=False) as pkg:
pkg.version = ".".join(map(str, sys.version_info[:3]))
pkg.tools = ["python"]
pkg.commands = commands
2 changes: 1 addition & 1 deletion src/rez/data/tests/builds/packages/anti/1.0.0/package.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
description = "package with anti package"


private_build_requires = ["build_util"]
private_build_requires = ["build_util", "python"]
requires = ["floob", "!loco"]

def commands():
2 changes: 1 addition & 1 deletion src/rez/data/tests/builds/packages/bah/2.1/package.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
uuid = "3c027ce6593244af947e305fc48eec96"
description = "bah humbug"

private_build_requires = ["build_util"]
private_build_requires = ["build_util", "python"]

variants = [
["foo-1.0"],
2 changes: 2 additions & 0 deletions src/rez/data/tests/builds/packages/build_util/1/package.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
uuid = "9982b60993af4a4d89e8372472a49d02"
description = "build utilities"

private_build_requires = ["python"]

def commands():
env.PYTHONPATH.append('{root}/python')

2 changes: 1 addition & 1 deletion src/rez/data/tests/builds/packages/floob/package.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
uuid = "156730d7122441e3a5745cc81361f49a"
description = "floobtasticator"

private_build_requires = ["build_util"]
private_build_requires = ["build_util", "python"]

def commands():
env.PYTHONPATH.append('{root}/python')
2 changes: 1 addition & 1 deletion src/rez/data/tests/builds/packages/foo/1.0.0/package.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

build_requires = ["floob"]

private_build_requires = ["build_util"]
private_build_requires = ["build_util", "python"]

def pre_build_commands():
env.FOO_TEST_VAR = "hello"
2 changes: 1 addition & 1 deletion src/rez/data/tests/builds/packages/foo/1.1.0/package.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

build_requires = ["floob"]

private_build_requires = ["build_util"]
private_build_requires = ["build_util", "python"]

@include("late_utils")
def commands():
2 changes: 2 additions & 0 deletions src/rez/data/tests/release/package.yaml
Original file line number Diff line number Diff line change
@@ -5,4 +5,6 @@ authors:
uuid: '9982b60993af4a4d89e8372472a49d02'
description: 'foo type thing'

private_build_requires: ["python"]

build_command: "python {root}/build.py {install}"
2 changes: 2 additions & 0 deletions src/rez/data/tests/release/variants/package.yaml
Original file line number Diff line number Diff line change
@@ -7,4 +7,6 @@ variants:
- ['spangle-1.0']
- ['spangle-1.1']

private_build_requires: ["python"]

build_command: "python {root}/build.py {install}"
2 changes: 2 additions & 0 deletions src/rez/tests/test_build.py
Original file line number Diff line number Diff line change
@@ -152,6 +152,7 @@ def test_builds(self, shell):
"""Test an interdependent set of builds.
"""
config.override("default_shell", shell)
self.inject_python_repo()

self._test_build_build_util()
self._test_build_floob()
@@ -165,6 +166,7 @@ def test_builds_anti(self, shell):
"""Test we can build packages that contain anti packages
"""
config.override("default_shell", shell)
self.inject_python_repo()

self._test_build_build_util()
self._test_build_floob()
14 changes: 9 additions & 5 deletions src/rez/tests/test_context.py
Original file line number Diff line number Diff line change
@@ -73,7 +73,8 @@ def test_execute_command(self):

def test_execute_command_environ(self):
"""Test that execute_command properly sets environ dict."""
r = ResolvedContext(["hello_world"])
self.inject_python_repo()
r = ResolvedContext(["hello_world", "python"])
self._test_execute_command_environ(r)

def _test_execute_command_environ(self, r):
@@ -110,14 +111,15 @@ def test_serialize(self):

def test_retarget(self):
"""Test that a retargeted context behaves identically."""
self.inject_python_repo()

# make a copy of the pkg repo
packages_path2 = os.path.join(self.root, "packages2")
shutil.copytree(self.packages_path, packages_path2)

# create a context, retarget to pkg repo copy
r = ResolvedContext(["hello_world"])
r2 = r.retargeted(package_paths=[packages_path2])
r = ResolvedContext(["hello_world", "python"])
r2 = r.retargeted(package_paths=[packages_path2, os.environ["__REZ_SELFTEST_PYTHON_REPO"]])

# check the pkg we contain is in the copied pkg repo
variant = r2.resolved_packages[0]
@@ -128,6 +130,8 @@ def test_retarget(self):
def test_bundled(self):
"""Test that a bundled context behaves identically."""

self.inject_python_repo()

def _test_bundle(path):
# load the bundled context
r2 = ResolvedContext.load(os.path.join(path, "context.rxt"))
@@ -145,7 +149,7 @@ def _test_bundle(path):
bundle_path = os.path.join(self.root, "bundle")

# create context and bundle it
r = ResolvedContext(["hello_world"])
r = ResolvedContext(["hello_world", "python"])
bundle_context(
context=r,
dest_dir=bundle_path,
@@ -172,7 +176,7 @@ def _test_bundle(path):
os.mkdir(hard_path)
os.symlink(hard_path, bundles_path)

r = ResolvedContext(["hello_world"])
r = ResolvedContext(["hello_world", "python"])
bundle_context(
context=r,
dest_dir=bundle_path3,
1 change: 1 addition & 0 deletions src/rez/tests/test_copy_package.py
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ def tearDownClass(cls):

def setup_once(self):
# build packages used by this test
self.inject_python_repo()
self._build_package("build_util", "1")
self._build_package("floob")
self._build_package("foo", "1.0.0")
2 changes: 2 additions & 0 deletions src/rez/tests/test_release.py
Original file line number Diff line number Diff line change
@@ -107,6 +107,7 @@ def _standardize_variants(variants):
@install_dependent()
def test_1(self, shell):
"""Basic release."""
self.inject_python_repo()
config.override("default_shell", shell)

# release should fail because release path does not exist
@@ -171,6 +172,7 @@ def test_1(self, shell):
def test_2_variant_add(self, shell):
"""Test variant installation on release
"""
self.inject_python_repo()
config.override("default_shell", shell)

orig_src_path = self.src_path
7 changes: 7 additions & 0 deletions src/rez/tests/util.py
Original file line number Diff line number Diff line change
@@ -131,6 +131,13 @@ def get_settings_env(self):
for k, v in self.settings.items()
)

def inject_python_repo(self):
self.update_settings(
{
"packages_path": config.packages_path + [os.environ["__REZ_SELFTEST_PYTHON_REPO"]],
}
)


class TempdirMixin(object):
"""Mixin that adds tmpdir create/delete."""

0 comments on commit 8e52a3c

Please sign in to comment.