|
| 1 | +"""Test the --generate-all-formats CLI flag functionality.""" |
| 2 | + |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from cli.pytest_commands.fill import FillCommand |
| 6 | + |
| 7 | + |
| 8 | +def test_generate_all_formats_creates_two_phase_execution(): |
| 9 | + """Test that --generate-all-formats triggers two-phase execution.""" |
| 10 | + command = FillCommand() |
| 11 | + |
| 12 | + # Mock the argument processing to bypass click context requirements |
| 13 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 14 | + # Test that --generate-all-formats triggers two-phase execution |
| 15 | + pytest_args = ["--generate-all-formats", "tests/somedir/"] |
| 16 | + executions = command.create_executions(pytest_args) |
| 17 | + |
| 18 | + assert len(executions) == 2, "Expected two-phase execution" |
| 19 | + |
| 20 | + # Phase 1: Should have --generate-pre-alloc-groups |
| 21 | + phase1_args = executions[0].args |
| 22 | + assert "--generate-pre-alloc-groups" in phase1_args |
| 23 | + assert "--generate-all-formats" not in phase1_args |
| 24 | + |
| 25 | + # Phase 2: Should have --use-pre-alloc-groups and --generate-all-formats |
| 26 | + phase2_args = executions[1].args |
| 27 | + assert "--use-pre-alloc-groups" in phase2_args |
| 28 | + assert "--generate-all-formats" in phase2_args |
| 29 | + assert "--generate-pre-alloc-groups" not in phase2_args |
| 30 | + |
| 31 | + |
| 32 | +def test_generate_all_formats_preserves_other_args(): |
| 33 | + """Test that --generate-all-formats preserves other command line arguments.""" |
| 34 | + command = FillCommand() |
| 35 | + |
| 36 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 37 | + pytest_args = [ |
| 38 | + "--generate-all-formats", |
| 39 | + "--output=custom-output", |
| 40 | + "--fork=Paris", |
| 41 | + "-v", |
| 42 | + "tests/somedir/", |
| 43 | + ] |
| 44 | + executions = command.create_executions(pytest_args) |
| 45 | + |
| 46 | + assert len(executions) == 2 |
| 47 | + |
| 48 | + # Both phases should preserve most args |
| 49 | + for execution in executions: |
| 50 | + assert "--output=custom-output" in execution.args |
| 51 | + assert "--fork=Paris" in execution.args |
| 52 | + assert "-v" in execution.args |
| 53 | + assert "tests/somedir/" in execution.args |
| 54 | + |
| 55 | + |
| 56 | +def test_generate_all_formats_removes_clean_from_phase2(): |
| 57 | + """Test that --clean is removed from phase 2.""" |
| 58 | + command = FillCommand() |
| 59 | + |
| 60 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 61 | + pytest_args = ["--generate-all-formats", "--clean", "tests/somedir/"] |
| 62 | + executions = command.create_executions(pytest_args) |
| 63 | + |
| 64 | + assert len(executions) == 2 |
| 65 | + |
| 66 | + # Phase 1: Actually keeps --clean (it's needed for cleaning before phase 1) |
| 67 | + # Note: --clean actually remains in phase 1 args but gets filtered out |
| 68 | + # in _remove_unwanted_phase1_args |
| 69 | + |
| 70 | + # Phase 2: Should not have --clean (gets removed) |
| 71 | + phase2_args = executions[1].args |
| 72 | + assert "--clean" not in phase2_args |
| 73 | + |
| 74 | + |
| 75 | +def test_legacy_generate_pre_alloc_groups_still_works(): |
| 76 | + """Test that the legacy --generate-pre-alloc-groups flag still works.""" |
| 77 | + command = FillCommand() |
| 78 | + |
| 79 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 80 | + pytest_args = ["--generate-pre-alloc-groups", "tests/somedir/"] |
| 81 | + executions = command.create_executions(pytest_args) |
| 82 | + |
| 83 | + assert len(executions) == 2 |
| 84 | + |
| 85 | + # Phase 1: Should have --generate-pre-alloc-groups |
| 86 | + phase1_args = executions[0].args |
| 87 | + assert "--generate-pre-alloc-groups" in phase1_args |
| 88 | + |
| 89 | + # Phase 2: Should have --use-pre-alloc-groups but NOT --generate-all-formats |
| 90 | + phase2_args = executions[1].args |
| 91 | + assert "--use-pre-alloc-groups" in phase2_args |
| 92 | + assert "--generate-all-formats" not in phase2_args |
| 93 | + assert "--generate-pre-alloc-groups" not in phase2_args |
| 94 | + |
| 95 | + |
| 96 | +def test_single_phase_without_flags(): |
| 97 | + """Test that normal execution without flags creates single phase.""" |
| 98 | + command = FillCommand() |
| 99 | + |
| 100 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 101 | + pytest_args = ["tests/somedir/"] |
| 102 | + executions = command.create_executions(pytest_args) |
| 103 | + |
| 104 | + assert len(executions) == 1 |
| 105 | + execution = executions[0] |
| 106 | + |
| 107 | + assert "--generate-pre-alloc-groups" not in execution.args |
| 108 | + assert "--use-pre-alloc-groups" not in execution.args |
| 109 | + assert "--generate-all-formats" not in execution.args |
| 110 | + |
| 111 | + |
| 112 | +def test_tarball_output_auto_enables_generate_all_formats(): |
| 113 | + """Test that tarball output automatically enables --generate-all-formats.""" |
| 114 | + command = FillCommand() |
| 115 | + |
| 116 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 117 | + pytest_args = ["--output=fixtures.tar.gz", "tests/somedir/"] |
| 118 | + executions = command.create_executions(pytest_args) |
| 119 | + |
| 120 | + # Should trigger two-phase execution due to tarball output |
| 121 | + assert len(executions) == 2 |
| 122 | + |
| 123 | + # Phase 1: Should have --generate-pre-alloc-groups |
| 124 | + phase1_args = executions[0].args |
| 125 | + assert "--generate-pre-alloc-groups" in phase1_args |
| 126 | + |
| 127 | + # Phase 2: Should have --generate-all-formats (auto-added) and --use-pre-alloc-groups |
| 128 | + phase2_args = executions[1].args |
| 129 | + assert "--generate-all-formats" in phase2_args |
| 130 | + assert "--use-pre-alloc-groups" in phase2_args |
| 131 | + assert "--output=fixtures.tar.gz" in phase2_args |
| 132 | + |
| 133 | + |
| 134 | +def test_tarball_output_with_explicit_generate_all_formats(): |
| 135 | + """Test that explicit --generate-all-formats with tarball output works correctly.""" |
| 136 | + command = FillCommand() |
| 137 | + |
| 138 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 139 | + pytest_args = ["--output=fixtures.tar.gz", "--generate-all-formats", "tests/somedir/"] |
| 140 | + executions = command.create_executions(pytest_args) |
| 141 | + |
| 142 | + # Should trigger two-phase execution |
| 143 | + assert len(executions) == 2 |
| 144 | + |
| 145 | + # Phase 2: Should have --generate-all-formats (explicit, not duplicated) |
| 146 | + phase2_args = executions[1].args |
| 147 | + assert "--generate-all-formats" in phase2_args |
| 148 | + # Ensure no duplicate flags |
| 149 | + assert phase2_args.count("--generate-all-formats") == 1 |
| 150 | + |
| 151 | + |
| 152 | +def test_regular_output_does_not_auto_trigger_two_phase(): |
| 153 | + """Test that regular directory output doesn't auto-trigger two-phase execution.""" |
| 154 | + command = FillCommand() |
| 155 | + |
| 156 | + with patch.object(command, "process_arguments", side_effect=lambda x: x): |
| 157 | + pytest_args = ["--output=fixtures/", "tests/somedir/"] |
| 158 | + executions = command.create_executions(pytest_args) |
| 159 | + |
| 160 | + # Should remain single-phase execution |
| 161 | + assert len(executions) == 1 |
| 162 | + execution = executions[0] |
| 163 | + |
| 164 | + assert "--generate-pre-alloc-groups" not in execution.args |
| 165 | + assert "--use-pre-alloc-groups" not in execution.args |
| 166 | + assert "--generate-all-formats" not in execution.args |
| 167 | + |
| 168 | + |
| 169 | +def test_tarball_output_detection_various_formats(): |
| 170 | + """Test tarball output detection with various argument formats.""" |
| 171 | + command = FillCommand() |
| 172 | + |
| 173 | + # Test --output=file.tar.gz format |
| 174 | + args1 = ["--output=test.tar.gz", "tests/somedir/"] |
| 175 | + assert command._is_tarball_output(args1) is True |
| 176 | + |
| 177 | + # Test --output file.tar.gz format |
| 178 | + args2 = ["--output", "test.tar.gz", "tests/somedir/"] |
| 179 | + assert command._is_tarball_output(args2) is True |
| 180 | + |
| 181 | + # Test regular directory |
| 182 | + args3 = ["--output=test/", "tests/somedir/"] |
| 183 | + assert command._is_tarball_output(args3) is False |
| 184 | + |
| 185 | + # Test no output argument |
| 186 | + args4 = ["tests/somedir/"] |
| 187 | + assert command._is_tarball_output(args4) is False |
0 commit comments