-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
target_types_test.py
79 lines (64 loc) · 3.29 KB
/
target_types_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from typing import Optional
import pytest
from pants.backend.python.subsystems.pytest import PyTest
from pants.backend.python.target_types import PythonBinarySources, PythonTestsTimeout
from pants.engine.addresses import Address
from pants.engine.rules import SubsystemRule
from pants.engine.target import InvalidFieldException
from pants.testutil.option.util import create_options_bootstrapper
from pants.testutil.test_base import TestBase
class TestTimeout(TestBase):
@classmethod
def rules(cls):
return [*super().rules(), SubsystemRule(PyTest)]
def test_timeout_validation(self) -> None:
with pytest.raises(InvalidFieldException):
PythonTestsTimeout(-100, address=Address.parse(":tests"))
with pytest.raises(InvalidFieldException):
PythonTestsTimeout(0, address=Address.parse(":tests"))
assert PythonTestsTimeout(5, address=Address.parse(":tests")).value == 5
def assert_timeout_calculated(
self,
*,
field_value: Optional[int],
expected: Optional[int],
global_default: Optional[int] = None,
global_max: Optional[int] = None,
timeouts_enabled: bool = True,
) -> None:
args = ["--backend-packages=pants.backend.python", f"--pytest-timeouts={timeouts_enabled}"]
if global_default is not None:
args.append(f"--pytest-timeout-default={global_default}")
if global_max is not None:
args.append(f"--pytest-timeout-maximum={global_max}")
pytest = self.request_single_product(PyTest, create_options_bootstrapper(args=args))
field = PythonTestsTimeout(field_value, address=Address.parse(":tests"))
assert field.calculate_from_global_options(pytest) == expected
def test_valid_field_timeout(self) -> None:
self.assert_timeout_calculated(field_value=10, expected=10)
def test_field_timeout_greater_than_max(self) -> None:
self.assert_timeout_calculated(field_value=20, global_max=10, expected=10)
def test_no_field_timeout_uses_default(self) -> None:
self.assert_timeout_calculated(field_value=None, global_default=20, expected=20)
def test_no_field_timeout_and_no_default(self) -> None:
self.assert_timeout_calculated(field_value=None, expected=None)
def test_no_field_timeout_and_default_greater_than_max(self) -> None:
self.assert_timeout_calculated(
field_value=None, global_default=20, global_max=10, expected=10
)
def test_timeouts_disabled(self) -> None:
self.assert_timeout_calculated(field_value=10, timeouts_enabled=False, expected=None)
def test_translate_source_file_to_entry_point() -> None:
assert (
PythonBinarySources.translate_source_file_to_entry_point(["example/app.py"])
== "example.app"
)
# NB: the onus is on the call site to strip the source roots before calling this method.
assert (
PythonBinarySources.translate_source_file_to_entry_point(["src/python/app.py"])
== "src.python.app"
)
assert PythonBinarySources.translate_source_file_to_entry_point([]) is None
assert PythonBinarySources.translate_source_file_to_entry_point(["f1.py", "f2.py"]) is None