Skip to content

Commit aa85d03

Browse files
committed
[AIENG-294] refactor tests of subset command
1 parent b9bfe6c commit aa85d03

File tree

2 files changed

+28
-105
lines changed

2 files changed

+28
-105
lines changed

smart_tests/commands/subset.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,17 +591,13 @@ def _should_skip_stdin(self) -> bool:
591591
return True
592592

593593
if self.subset_id is not None:
594-
if self._stdin_is_tty():
594+
if sys.stdin.isatty():
595595
warn_and_exit_if_fail_fast_mode(
596596
"Warning: --subset-id is set so stdin will be ignored."
597597
)
598598
return True
599599
return False
600600

601-
# Note(Konboi): Helper to be able to patch stdin easily in tests
602-
def _stdin_is_tty(self) -> bool:
603-
return sys.stdin.isatty()
604-
605601
def run(self):
606602
"""called after tests are scanned to compute the optimized order"""
607603

tests/commands/test_subset.py

Lines changed: 27 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -542,22 +542,10 @@ def test_subset_with_bin_option(self):
542542
status=200,
543543
)
544544

545-
result = self.cli(
546-
"subset",
547-
"file",
548-
"--session",
549-
self.session,
550-
"--target",
551-
"10%",
552-
"--bin",
553-
"1/4",
554-
"--subset-id",
555-
"222",
556-
mix_stderr=False,
557-
input=pipe,
558-
)
559-
self.assert_success(result)
545+
result = self.cli("subset", "file", "--session", self.session, "--target", "10%", "--bin", "1/4",
546+
"--subset-id", "222", mix_stderr=False, input=pipe)
560547

548+
self.assert_success(result)
561549
payload = self.decode_request_body(self.find_request('/subset').request.body)
562550
self.assertEqual(payload.get('subsettingId'), 222)
563551
self.assertEqual(
@@ -568,7 +556,25 @@ def test_subset_with_bin_option(self):
568556
@responses.activate
569557
@mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token})
570558
def test_subset_with_same_bin_file(self):
571-
pipe = "TestExample1\nok github.com/example/project 0.1s\n"
559+
# Test invalid case
560+
# --same-bin requires --bin options
561+
with tempfile.NamedTemporaryFile("w+", delete=False) as same_bin_file:
562+
same_bin_file.write("example.AddTest\n")
563+
same_bin_file.flush()
564+
result = self.cli(
565+
"subset",
566+
"go-test",
567+
"--session",
568+
self.session,
569+
"--subset-id",
570+
123,
571+
"--same-bin",
572+
same_bin_file.name,
573+
mix_stderr=False)
574+
self.assert_exit_code(result, 1)
575+
self.assertIn("--same-bin option requires --bin option", result.stderr)
576+
577+
# Test valid case
572578
mock_json_response = {
573579
"testPaths": [[
574580
{"type": "class", "name": "rocket-car-gotest"},
@@ -589,27 +595,12 @@ def test_subset_with_same_bin_file(self):
589595
status=200,
590596
)
591597

592-
same_bin_file = tempfile.NamedTemporaryFile(delete=False)
593-
try:
594-
same_bin_file.write(b"example.AddTest\nexample.DivTest\n")
595-
same_bin_file.close()
596-
597-
result = self.cli(
598-
"subset",
599-
"go-test",
600-
"--session",
601-
self.session,
602-
"--target",
603-
"20%",
604-
"--bin",
605-
"2/5",
606-
"--same-bin",
607-
same_bin_file.name,
608-
mix_stderr=False,
609-
input=pipe,
610-
)
598+
with tempfile.NamedTemporaryFile("w+", delete=False) as same_bin_file:
599+
same_bin_file.write("example.AddTest\nexample.DivTest\n")
600+
same_bin_file.flush()
601+
result = self.cli("subset", "go-test", "--session", self.session, "--target", "20%", "--subset-id", 123,
602+
"--bin", "2/5", "--same-bin", same_bin_file.name, mix_stderr=False)
611603
self.assert_success(result)
612-
613604
payload = self.decode_request_body(self.find_request('/subset').request.body)
614605
split_subset = payload.get('splitSubset')
615606
self.assertEqual(split_subset.get('sliceIndex'), 2)
@@ -627,68 +618,4 @@ def test_subset_with_same_bin_file(self):
627618
],
628619
]],
629620
)
630-
finally:
631-
os.unlink(same_bin_file.name)
632-
633-
@responses.activate
634-
@mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token})
635-
def test_same_bin_requires_bin(self):
636-
pipe = "TestExample\nok github.com/example/project 0.1s\n"
637-
same_bin_file = tempfile.NamedTemporaryFile(delete=False)
638-
try:
639-
same_bin_file.write(b"example.AddTest\n")
640-
same_bin_file.close()
641-
642-
result = self.cli(
643-
"subset",
644-
"go-test",
645-
"--session",
646-
self.session,
647-
"--same-bin",
648-
same_bin_file.name,
649-
mix_stderr=False,
650-
input=pipe,
651-
)
652-
self.assert_exit_code(result, 1)
653-
self.assertIn("--same-bin requires --bin", result.stderr)
654-
finally:
655621
os.unlink(same_bin_file.name)
656-
657-
@responses.activate
658-
@mock.patch.dict(os.environ, {"SMART_TESTS_TOKEN": CliTestCase.smart_tests_token})
659-
def test_subset_without_tests_but_with_subset_id(self):
660-
responses.replace(
661-
responses.POST,
662-
f"{get_base_url()}/intake/organizations/{self.organization}/workspaces/{self.workspace}/subset",
663-
json={
664-
"testPaths": [],
665-
"rest": [],
666-
"subsettingId": 321,
667-
"summary": {
668-
"subset": {"duration": 0, "candidates": 0, "rate": 0},
669-
"rest": {"duration": 0, "candidates": 0, "rate": 0},
670-
},
671-
"isObservation": False,
672-
},
673-
status=200,
674-
)
675-
676-
result = self.cli(
677-
"subset",
678-
"file",
679-
"--session",
680-
self.session,
681-
"--subset-id",
682-
"321",
683-
"--bin",
684-
"1/2",
685-
"--target",
686-
"10%",
687-
mix_stderr=False,
688-
)
689-
690-
self.assert_success(result)
691-
self.assertNotIn("Warning: this command reads from stdin", result.stderr)
692-
693-
payload = self.decode_request_body(self.find_request('/subset').request.body)
694-
self.assertEqual(payload.get('subsettingId'), 321)

0 commit comments

Comments
 (0)