diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 3df775505..91de2e3a2 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -100,7 +100,7 @@ jobs: - name: Upload coverage reports to Codecov uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0 # 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.outcome == 'success' || steps.tests.outcome == 'failure') && hashFiles('coverage.xml') != '') }} with: slug: AcademySoftwareFoundation/rez files: 'coverage.xml' diff --git a/src/rez/resolved_context.py b/src/rez/resolved_context.py index 7e1586b41..c9c9ab70a 100644 --- a/src/rez/resolved_context.py +++ b/src/rez/resolved_context.py @@ -154,8 +154,12 @@ 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(self.start_time) + print(time.time()) + print(secs) if secs > self.time_limit: return SolverCallbackReturn.abort, "time limit exceeded" if self.callback: diff --git a/src/rez/tests/test_context.py b/src/rez/tests/test_context.py index 65cefffbd..2c1993f71 100644 --- a/src/rez/tests/test_context.py +++ b/src/rez/tests/test_context.py @@ -5,14 +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 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.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 @@ -250,6 +256,75 @@ 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) + 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 + + 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()