From 51a045c7b95785c34b421253c2f3dc522a510dc9 Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Fri, 10 May 2024 06:54:14 -0500 Subject: [PATCH 01/12] Add unit tests Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index 65cefffbd..dc1b4a254 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -7,11 +7,13 @@ """ from rez.tests.util import restore_os_environ, restore_sys_path, TempdirMixin, \ TestBase -from rez.resolved_context import ResolvedContext +from rez.resolved_context import get_lock_request, PatchLock, ResolvedContext from rez.bundle_context import bundle_context from rez.bind import hello_world from rez.utils.platform_ import platform_ from rez.utils.filesystem import is_subdirectory +from rez.utils.formatting import PackageRequest +from rez.version import Version import unittest import subprocess import platform @@ -250,6 +252,42 @@ def test_orderer_package_argument(self): resolved = [x.qualified_package_name for x in r.resolved_packages] self.assertEqual(resolved, ['python-2.7.0']) + def test_get_lock_request_1(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.lock_3 + expected_lock_request = PackageRequest('~foo-1.2') + + lock_request = get_lock_request(pkg, version, patch_lock) + self.assertEqual(lock_request, expected_lock_request) + + def test_get_lock_request_2(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.lock_3 + expected_lock_request = PackageRequest('foo-1.2') + + lock_request = get_lock_request(pkg, version, patch_lock, weak=False) + self.assertEqual(lock_request, expected_lock_request) + + def test_get_lock_request_3(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.lock + expected_lock_request = PackageRequest('foo==1.2.1') + + lock_request = get_lock_request(pkg, version, patch_lock, weak=False) + self.assertEqual(lock_request, expected_lock_request) + + def test_get_lock_request_4(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.no_lock + expected_lock_request = None + + lock_request = get_lock_request(pkg, version, patch_lock) + self.assertEqual(lock_request, expected_lock_request) + if __name__ == '__main__': unittest.main() From 1d9911ae69cfc1f52149f8b723433cdf78f3c753 Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Fri, 10 May 2024 08:02:41 -0500 Subject: [PATCH 02/12] Add tests for Callback class inside ResolvedContext Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index dc1b4a254..225f08e46 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -5,16 +5,20 @@ """ test resolved contexts """ +import sys + from rez.tests.util import restore_os_environ, restore_sys_path, TempdirMixin, \ TestBase from rez.resolved_context import get_lock_request, PatchLock, ResolvedContext from rez.bundle_context import bundle_context from rez.bind import hello_world +from rez.solver import SolverCallbackReturn, SolverState from rez.utils.platform_ import platform_ from rez.utils.filesystem import is_subdirectory from rez.utils.formatting import PackageRequest from rez.version import Version import unittest +import unittest.mock import subprocess import platform import shutil @@ -252,6 +256,39 @@ def test_orderer_package_argument(self): resolved = [x.qualified_package_name for x in r.resolved_packages] self.assertEqual(resolved, ['python-2.7.0']) + def test_callback_1(self): + def solver_callback(solver_state: SolverState): + if solver_state.num_fails > 999: + solver_callback_return = SolverCallbackReturn.fail + abort_reason = 'Too many fails' + else: + solver_callback_return = SolverCallbackReturn.keep_going + abort_reason = 'No reason' + return solver_callback_return, abort_reason + + callback = ResolvedContext.Callback(max_fails=1, time_limit=2, callback=solver_callback, buf=sys.stdout) + solve_state = SolverState(3, 1, None) + callback_result = callback(solve_state) + assert callback_result[0] == SolverCallbackReturn.fail + + def test_callback_2(self): + def solver_callback(solver_state: SolverState): + if solver_state.num_fails > 999: + solver_callback_return = SolverCallbackReturn.fail + abort_reason = 'Too many fails' + else: + solver_callback_return = SolverCallbackReturn.keep_going + abort_reason = 'No reason' + return solver_callback_return, abort_reason + + callback = ResolvedContext.Callback(max_fails=999, time_limit=0, callback=solver_callback, buf=sys.stdout) + solve_state = SolverState(3, 1, None) + # Mock start time to be Jan 1, 1970. + with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback.start_time', + new_callable=unittest.mock.PropertyMock, return_value=0): + callback_result = callback(solve_state) + assert callback_result[0] == SolverCallbackReturn.abort + def test_get_lock_request_1(self): pkg = 'foo' version = Version('1.2.1') From 77d156dde9dd9e54fb9636c60cb62c4b837944cd Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Fri, 10 May 2024 08:03:04 -0500 Subject: [PATCH 03/12] Add TODO Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index 225f08e46..fcd778879 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -283,6 +283,7 @@ def solver_callback(solver_state: SolverState): callback = ResolvedContext.Callback(max_fails=999, time_limit=0, callback=solver_callback, buf=sys.stdout) solve_state = SolverState(3, 1, None) + # TODO: Can't seem to get this mock to work for some reason. # Mock start time to be Jan 1, 1970. with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback.start_time', new_callable=unittest.mock.PropertyMock, return_value=0): From d62dc876454f29c626b07dd3d0b942569b9d2362 Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Fri, 10 May 2024 06:54:14 -0500 Subject: [PATCH 04/12] Add unit tests Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index 65cefffbd..dc1b4a254 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -7,11 +7,13 @@ """ from rez.tests.util import restore_os_environ, restore_sys_path, TempdirMixin, \ TestBase -from rez.resolved_context import ResolvedContext +from rez.resolved_context import get_lock_request, PatchLock, ResolvedContext from rez.bundle_context import bundle_context from rez.bind import hello_world from rez.utils.platform_ import platform_ from rez.utils.filesystem import is_subdirectory +from rez.utils.formatting import PackageRequest +from rez.version import Version import unittest import subprocess import platform @@ -250,6 +252,42 @@ def test_orderer_package_argument(self): resolved = [x.qualified_package_name for x in r.resolved_packages] self.assertEqual(resolved, ['python-2.7.0']) + def test_get_lock_request_1(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.lock_3 + expected_lock_request = PackageRequest('~foo-1.2') + + lock_request = get_lock_request(pkg, version, patch_lock) + self.assertEqual(lock_request, expected_lock_request) + + def test_get_lock_request_2(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.lock_3 + expected_lock_request = PackageRequest('foo-1.2') + + lock_request = get_lock_request(pkg, version, patch_lock, weak=False) + self.assertEqual(lock_request, expected_lock_request) + + def test_get_lock_request_3(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.lock + expected_lock_request = PackageRequest('foo==1.2.1') + + lock_request = get_lock_request(pkg, version, patch_lock, weak=False) + self.assertEqual(lock_request, expected_lock_request) + + def test_get_lock_request_4(self): + pkg = 'foo' + version = Version('1.2.1') + patch_lock = PatchLock.no_lock + expected_lock_request = None + + lock_request = get_lock_request(pkg, version, patch_lock) + self.assertEqual(lock_request, expected_lock_request) + if __name__ == '__main__': unittest.main() From 78179b5a37b0daf09dd72d47d416c0d494b0bfb5 Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Fri, 10 May 2024 08:02:41 -0500 Subject: [PATCH 05/12] Add tests for Callback class inside ResolvedContext Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index dc1b4a254..225f08e46 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -5,16 +5,20 @@ """ test resolved contexts """ +import sys + from rez.tests.util import restore_os_environ, restore_sys_path, TempdirMixin, \ TestBase from rez.resolved_context import get_lock_request, PatchLock, ResolvedContext from rez.bundle_context import bundle_context from rez.bind import hello_world +from rez.solver import SolverCallbackReturn, SolverState from rez.utils.platform_ import platform_ from rez.utils.filesystem import is_subdirectory from rez.utils.formatting import PackageRequest from rez.version import Version import unittest +import unittest.mock import subprocess import platform import shutil @@ -252,6 +256,39 @@ def test_orderer_package_argument(self): resolved = [x.qualified_package_name for x in r.resolved_packages] self.assertEqual(resolved, ['python-2.7.0']) + def test_callback_1(self): + def solver_callback(solver_state: SolverState): + if solver_state.num_fails > 999: + solver_callback_return = SolverCallbackReturn.fail + abort_reason = 'Too many fails' + else: + solver_callback_return = SolverCallbackReturn.keep_going + abort_reason = 'No reason' + return solver_callback_return, abort_reason + + callback = ResolvedContext.Callback(max_fails=1, time_limit=2, callback=solver_callback, buf=sys.stdout) + solve_state = SolverState(3, 1, None) + callback_result = callback(solve_state) + assert callback_result[0] == SolverCallbackReturn.fail + + def test_callback_2(self): + def solver_callback(solver_state: SolverState): + if solver_state.num_fails > 999: + solver_callback_return = SolverCallbackReturn.fail + abort_reason = 'Too many fails' + else: + solver_callback_return = SolverCallbackReturn.keep_going + abort_reason = 'No reason' + return solver_callback_return, abort_reason + + callback = ResolvedContext.Callback(max_fails=999, time_limit=0, callback=solver_callback, buf=sys.stdout) + solve_state = SolverState(3, 1, None) + # Mock start time to be Jan 1, 1970. + with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback.start_time', + new_callable=unittest.mock.PropertyMock, return_value=0): + callback_result = callback(solve_state) + assert callback_result[0] == SolverCallbackReturn.abort + def test_get_lock_request_1(self): pkg = 'foo' version = Version('1.2.1') From eaed6ed3cf1622aa4837b79c7f09c0b9b330df25 Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Fri, 10 May 2024 08:03:04 -0500 Subject: [PATCH 06/12] Add TODO Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index 225f08e46..fcd778879 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -283,6 +283,7 @@ def solver_callback(solver_state: SolverState): callback = ResolvedContext.Callback(max_fails=999, time_limit=0, callback=solver_callback, buf=sys.stdout) solve_state = SolverState(3, 1, None) + # TODO: Can't seem to get this mock to work for some reason. # Mock start time to be Jan 1, 1970. with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback.start_time', new_callable=unittest.mock.PropertyMock, return_value=0): From e35fba00c6e107ead0e353b5d50cfb9eb872a4e2 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Morin Date: Sun, 12 May 2024 16:59:18 -0400 Subject: [PATCH 07/12] Try another if expression Signed-off-by: Jean-Christophe Morin --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index dc19ae3b6..868b1d91a 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -97,7 +97,7 @@ jobs: - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4 # Run on both success and failure, but only if coverage.xml exists. - if: ${{ hashFiles('coverage.xml') != '' && (steps.tests.outcome == 'success' || steps.tests.outcome == 'failure') }} + if: ${{ (success() || failure()) && ((steps.tests.conclusion == 'success' || steps.tests.outcome == 'failure') && hashFiles('coverage.xml') != '') }} with: slug: AcademySoftwareFoundation/rez files: 'coverage.xml' From 234e0eaf34525c9f02810adf8ca10ef8e234be73 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Morin Date: Sun, 12 May 2024 17:01:54 -0400 Subject: [PATCH 08/12] Use outcome instead of conclusion Signed-off-by: Jean-Christophe Morin --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 868b1d91a..c37c0326e 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -97,7 +97,7 @@ jobs: - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4 # Run on both success and failure, but only if coverage.xml exists. - if: ${{ (success() || failure()) && ((steps.tests.conclusion == 'success' || steps.tests.outcome == 'failure') && hashFiles('coverage.xml') != '') }} + if: ${{ (success() || failure()) && ((steps.tests.outcome == 'success' || steps.tests.outcome == 'failure') && hashFiles('coverage.xml') != '') }} with: slug: AcademySoftwareFoundation/rez files: 'coverage.xml' From 829873ceb094aa985180ed4cdaa5c7b7fc07d953 Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Sun, 14 Jul 2024 11:52:13 -0500 Subject: [PATCH 09/12] Fix linting error Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index fcd778879..f2bbff7ad 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -286,7 +286,7 @@ def solver_callback(solver_state: SolverState): # TODO: Can't seem to get this mock to work for some reason. # Mock start time to be Jan 1, 1970. with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback.start_time', - new_callable=unittest.mock.PropertyMock, return_value=0): + new_callable=unittest.mock.PropertyMock, return_value=0): callback_result = callback(solve_state) assert callback_result[0] == SolverCallbackReturn.abort From 42402fa3dc07e679fecab6be7f3d86de94bdee9f Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Sun, 14 Jul 2024 12:11:38 -0500 Subject: [PATCH 10/12] Fix broken test Signed-off-by: BryceGattis --- src/rez/tests/test_context.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index f2bbff7ad..2c1993f71 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -283,10 +283,9 @@ def solver_callback(solver_state: SolverState): callback = ResolvedContext.Callback(max_fails=999, time_limit=0, callback=solver_callback, buf=sys.stdout) solve_state = SolverState(3, 1, None) - # TODO: Can't seem to get this mock to work for some reason. - # Mock start time to be Jan 1, 1970. - with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback.start_time', - new_callable=unittest.mock.PropertyMock, return_value=0): + with unittest.mock.patch('rez.resolved_context.ResolvedContext.Callback') as mock_callback: + # Mock start time to be Jan 1, 1970. + mock_callback.start_time.return_value = 0 callback_result = callback(solve_state) assert callback_result[0] == SolverCallbackReturn.abort From 277c2e8de611440c40cb68c644328921999f205a Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Sun, 14 Jul 2024 12:25:14 -0500 Subject: [PATCH 11/12] Add some prints for debugging github actions Signed-off-by: BryceGattis --- src/rez/resolved_context.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rez/resolved_context.py b/src/rez/resolved_context.py index 7e1586b41..1af6bb635 100644 --- a/src/rez/resolved_context.py +++ b/src/rez/resolved_context.py @@ -154,8 +154,11 @@ def __call__(self, state): reason = ("fail limit reached: aborted after %d failures" % state.num_fails) return SolverCallbackReturn.fail, reason + print(self.time_limit) if self.time_limit != -1: secs = time.time() - self.start_time + print(time.time()) + print(secs) if secs > self.time_limit: return SolverCallbackReturn.abort, "time limit exceeded" if self.callback: From 496a499c9b5ffcd39a5a2629bf9c368685b2816a Mon Sep 17 00:00:00 2001 From: BryceGattis Date: Sun, 14 Jul 2024 12:33:01 -0500 Subject: [PATCH 12/12] Add one other print for debugging Signed-off-by: BryceGattis --- src/rez/resolved_context.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rez/resolved_context.py b/src/rez/resolved_context.py index 1af6bb635..c9c9ab70a 100644 --- a/src/rez/resolved_context.py +++ b/src/rez/resolved_context.py @@ -157,6 +157,7 @@ def __call__(self, state): print(self.time_limit) if self.time_limit != -1: secs = time.time() - self.start_time + print(self.start_time) print(time.time()) print(secs) if secs > self.time_limit: