def test_transitive_params(transitive_params_rule_runner: RuleRunner) -> None: # Test that C can be provided and implicitly converted into a B with transitive_b_c() to satisfy # the selectors of consumes_a_and_b(). a, c = A(), C() result_str = transitive_params_rule_runner.request(str, [a, c]) assert remove_locations_from_traceback( result_str) == remove_locations_from_traceback( consumes_a_and_b(a, transitive_b_c(c))) # Test that an inner Get in transitive_coroutine_rule() is able to resolve B from C due to # the existence of transitive_b_c(). transitive_params_rule_runner.request(D, [c])
def test_transitive_params(self): # Test that C can be provided and implicitly converted into a B with transitive_b_c() to satisfy # the selectors of consumes_a_and_b(). a, c = A(), C() result_str = self.request_product(str, [a, c]) self.assertEqual( remove_locations_from_traceback(result_str), remove_locations_from_traceback(consumes_a_and_b(a, transitive_b_c(c))), ) # Test that an inner Get in transitive_coroutine_rule() is able to resolve B from C due to # the existence of transitive_b_c(). self.request_product(D, [c])
def test_trace_includes_rule_exception_traceback(self): # Execute a request that will trigger the nested raise, and then directly inspect its trace. request = self.scheduler.execution_request([A], [B()]) _, throws = self.scheduler.execute(request) with self.assertRaises(ExecutionError) as cm: self.scheduler._raise_on_error([t for _, t in throws]) trace = remove_locations_from_traceback(str(cm.exception)) assert_equal_with_printing( self, dedent( """\ 1 Exception encountered: Engine traceback: in select in Nested raise Traceback (most recent call last): File LOCATION-INFO, in nested_raise fn_raises(b) File LOCATION-INFO, in fn_raises raise Exception(f"An exception for {type(x).__name__}") Exception: An exception for B """ ), trace, )
def test_trace_includes_rule_exception_traceback() -> None: rule_runner = RuleRunner(rules=[nested_raise, QueryRule(A, [])]) with pytest.raises(ExecutionError) as exc: rule_runner.request(A, []) normalized_traceback = remove_locations_from_traceback(str(exc.value)) assert normalized_traceback == dedent(f"""\ 1 Exception encountered: Engine traceback: in select in {__name__}.{nested_raise.__name__} Traceback (most recent call last): File LOCATION-INFO, in nested_raise fn_raises() File LOCATION-INFO, in fn_raises raise Exception("An exception!") Exception: An exception! """)
def test_include_trace_error_raises_error_with_trace(self): rules = [nested_raise, QueryRule(A, (B, ))] scheduler = self.scheduler(rules, include_trace_on_error=True) with self.assertRaises(ExecutionError) as cm: list(scheduler.product_request(A, subjects=[(B())])) self.assert_equal_with_printing( dedent(""" 1 Exception encountered: Engine traceback: in select in pants.engine.internals.engine_test.nested_raise Traceback (most recent call last): File LOCATION-INFO, in nested_raise fn_raises(x) File LOCATION-INFO, in fn_raises raise Exception(f"An exception for {type(x).__name__}") Exception: An exception for B """).lstrip(), remove_locations_from_traceback(str(cm.exception)), )
def assert_execution_error(test_case, expected_msg): with test_case.assertRaises(ExecutionError) as cm: yield test_case.assertIn(expected_msg, remove_locations_from_traceback(str(cm.exception)))