|
1 |
| -import os |
| 1 | +from pathlib import Path |
2 | 2 | import pytest
|
3 |
| -from junitparser.cli import verify |
| 3 | +from junitparser import cli |
| 4 | +from junitparser import version |
| 5 | + |
| 6 | +DATA_DIR = Path(__file__).parent / "data" |
4 | 7 |
|
5 | 8 |
|
6 | 9 | @pytest.mark.parametrize(
|
7 | 10 | "file, expected_exitcode",
|
8 |
| - [("data/jenkins.xml", 1), ("data/no_fails.xml", 0), ("data/normal.xml", 1)], |
| 11 | + [("jenkins.xml", 1), ("no_fails.xml", 0), ("normal.xml", 1)], |
9 | 12 | )
|
10 |
| -def test_verify(file, expected_exitcode): |
11 |
| - path = os.path.join(os.path.dirname(__file__), file) |
12 |
| - assert verify([path]) == expected_exitcode |
| 13 | +def test_verify(file: str, expected_exitcode: int): |
| 14 | + path = DATA_DIR / file |
| 15 | + assert cli.verify([path]) == expected_exitcode |
| 16 | + |
| 17 | + |
| 18 | +def test_merge(tmp_path: Path): |
| 19 | + files = [DATA_DIR / "jenkins.xml", DATA_DIR / "pytest_success.xml"] |
| 20 | + suites = ["JUnitXmlReporter", "JUnitXmlReporter.constructor", "pytest"] |
| 21 | + outfile = tmp_path / "merged.xml" |
| 22 | + cli.merge(files, str(outfile)) |
| 23 | + xml = outfile.read_text() |
| 24 | + for s in suites: |
| 25 | + assert f'name="{s}"' in xml |
| 26 | + |
| 27 | + |
| 28 | +def test_merge_output_to_terminal(capsys: pytest.CaptureFixture): |
| 29 | + ret = cli.main(["merge", str(DATA_DIR / "normal.xml"), "-"]) |
| 30 | + assert ret == 0 |
| 31 | + captured = capsys.readouterr() |
| 32 | + assert captured.out.startswith("<?xml version='1.0'") |
| 33 | + |
| 34 | + |
| 35 | +def test_verify_with_glob(): |
| 36 | + ret = cli.main(["verify", "--glob", str(DATA_DIR / "pytest_*.xml")]) |
| 37 | + # we expect failure, as one of the files has errors |
| 38 | + assert ret == 1 |
| 39 | + |
| 40 | + |
| 41 | +class Test_CommandlineOptions: |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def setup_class(cls): |
| 45 | + cls.parser = cli._parser("junitparser") |
| 46 | + |
| 47 | + @pytest.mark.parametrize("arg", ["-v", "--version"]) |
| 48 | + def test_version(self, arg, capsys): |
| 49 | + with pytest.raises(SystemExit) as e: |
| 50 | + self.parser.parse_args([arg]) |
| 51 | + captured = capsys.readouterr() |
| 52 | + assert e.value.code == 0 |
| 53 | + assert captured.out == f"junitparser {version}\n" |
| 54 | + |
| 55 | + def test_help_shows_subcommands(self, capsys): |
| 56 | + with pytest.raises(SystemExit) as e: |
| 57 | + self.parser.parse_args(["--help"]) |
| 58 | + captured = capsys.readouterr() |
| 59 | + assert "{merge,verify} ...\n" in captured.out |
| 60 | + assert e.value.code == 0 |
| 61 | + |
| 62 | + @pytest.mark.parametrize("command", ["merge", "verify"]) |
| 63 | + def test_subcommand_help(self, command): |
| 64 | + with pytest.raises(SystemExit) as e: |
| 65 | + self.parser.parse_args([command, "--help"]) |
| 66 | + assert e.value.code == 0 |
| 67 | + |
| 68 | + @pytest.mark.parametrize("command", ["merge", "verify"]) |
| 69 | + def test_subcommands_help_general_options(self, command, capsys): |
| 70 | + with pytest.raises(SystemExit): |
| 71 | + self.parser.parse_args([command, "--help"]) |
| 72 | + captured = capsys.readouterr() |
| 73 | + assert "[--glob]" in captured.out |
| 74 | + assert "paths [paths ...]" in captured.out |
| 75 | + |
| 76 | + def test_merge_help_options(self, capsys): |
| 77 | + with pytest.raises(SystemExit): |
| 78 | + self.parser.parse_args(["merge", "--help"]) |
| 79 | + captured = capsys.readouterr() |
| 80 | + assert "[--suite-name SUITE_NAME]" in captured.out |
| 81 | + assert "output\n" in captured.out |
| 82 | + |
| 83 | + @pytest.mark.parametrize("command", ["merge", "verify"]) |
| 84 | + def test_option_glob( |
| 85 | + self, |
| 86 | + command, |
| 87 | + ): |
| 88 | + args = self.parser.parse_args([command, "--glob", "pytest_*.xml", "-"]) |
| 89 | + assert args.paths_are_globs |
| 90 | + |
| 91 | + def test_verify_argument_path(self): |
| 92 | + files = ["foo", "bar"] |
| 93 | + args = self.parser.parse_args(["verify", *files]) |
| 94 | + assert args.paths == files |
| 95 | + |
| 96 | + def test_merge_argument_path(self): |
| 97 | + files = ["foo", "bar"] |
| 98 | + args = self.parser.parse_args(["merge", *files, "-"]) |
| 99 | + assert args.paths == files |
| 100 | + |
| 101 | + def test_merge_option_suite_name(self): |
| 102 | + args = self.parser.parse_args(["merge", "--suite-name", "foo", "_", "-"]) |
| 103 | + assert args.suite_name == "foo" |
| 104 | + |
| 105 | + def test_merge_argument_output(self): |
| 106 | + args = self.parser.parse_args(["merge", "foo", "bar"]) |
| 107 | + assert args.output == "bar" |
0 commit comments