|
8 | 8 | import subprocess |
9 | 9 | import sys |
10 | 10 | from pathlib import Path |
| 11 | +from types import SimpleNamespace |
11 | 12 | from unittest.mock import MagicMock, patch |
12 | 13 |
|
13 | 14 | import pytest |
@@ -1489,6 +1490,53 @@ def test_main_default_outputpath(git_repo_minimal, temp_dir): |
1489 | 1490 | assert os.path.isdir("gitstats-report") |
1490 | 1491 |
|
1491 | 1492 |
|
| 1493 | +@pytest.mark.skipif( |
| 1494 | + sys.version_info < (3, 12), |
| 1495 | + reason="argparse before 3.12 rejects a positional that follows an option", |
| 1496 | +) |
| 1497 | +def test_main_outputpath_after_an_option(git_repo_minimal, temp_dir): |
| 1498 | + """`gitstats repo -f json out`: argparse itself hands the last path to |
| 1499 | + outputpath, so main() has nothing to move out of gitpath.""" |
| 1500 | + import gitstats |
| 1501 | + import gitstats.main |
| 1502 | + |
| 1503 | + cfg = dict(gitstats.DEFAULT_CONFIG, ai_enabled=False) |
| 1504 | + gitstats._config = cfg |
| 1505 | + gitstats.main.conf = cfg |
| 1506 | + |
| 1507 | + output = os.path.join(temp_dir, "report") |
| 1508 | + |
| 1509 | + with patch.object(sys, "argv", ["gitstats", git_repo_minimal, "-f", "json", output]): |
| 1510 | + ret = main() |
| 1511 | + assert ret == 0 |
| 1512 | + assert os.path.isfile(os.path.join(output, "index.html")) |
| 1513 | + assert os.path.isfile(f"{output}.json") # the JSON goes next to the report |
| 1514 | + |
| 1515 | + |
| 1516 | +def test_main_keeps_an_outputpath_argparse_filled(git_repo_minimal, temp_dir, monkeypatch): |
| 1517 | + """Same contract on every Python: when the parser already filled outputpath, |
| 1518 | + main() leaves gitpath alone instead of moving its last entry.""" |
| 1519 | + import gitstats |
| 1520 | + import gitstats.main |
| 1521 | + |
| 1522 | + cfg = dict(gitstats.DEFAULT_CONFIG, ai_enabled=False) |
| 1523 | + gitstats._config = cfg |
| 1524 | + gitstats.main.conf = cfg |
| 1525 | + |
| 1526 | + output = os.path.join(temp_dir, "report") |
| 1527 | + args = gitstats.main.get_parser().parse_args([git_repo_minimal, "-f", "json"]) |
| 1528 | + args.outputpath = output |
| 1529 | + monkeypatch.setattr( |
| 1530 | + gitstats.main, "get_parser", lambda: SimpleNamespace(parse_args=lambda: args) |
| 1531 | + ) |
| 1532 | + |
| 1533 | + ret = main() |
| 1534 | + assert ret == 0 |
| 1535 | + assert args.gitpath == [git_repo_minimal] |
| 1536 | + assert os.path.isfile(os.path.join(output, "index.html")) |
| 1537 | + assert os.path.isfile(f"{output}.json") |
| 1538 | + |
| 1539 | + |
1492 | 1540 | def test_main_with_config_override(git_repo_minimal, temp_dir): |
1493 | 1541 | """Test main() with -c overrides.""" |
1494 | 1542 | import gitstats |
|
0 commit comments