|
| 1 | +# SPDX-FileCopyrightText: 2023 Ivan Grokhotkov <[email protected]> |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +import pathlib |
| 4 | + |
| 5 | +import pytest |
| 6 | +from test_files_iter import chdir_ctx |
| 7 | + |
| 8 | +from astyle_py import __version__ |
| 9 | +from astyle_py.__main__ import astyle_py_main |
| 10 | + |
| 11 | +# tmp_path: pathlib.Path |
| 12 | + |
| 13 | + |
| 14 | +def test_main_version(capfd: pytest.CaptureFixture[str]): |
| 15 | + with pytest.raises(SystemExit) as e: |
| 16 | + astyle_py_main(['--version']) |
| 17 | + assert e.value.code == 0 |
| 18 | + out, err = capfd.readouterr() |
| 19 | + assert out == f'astyle-py {__version__} with Astyle v3.1\n' |
| 20 | + assert err == '' |
| 21 | + |
| 22 | + |
| 23 | +def test_main_specific_version(capfd: pytest.CaptureFixture[str]): |
| 24 | + ver = '3.4.7' |
| 25 | + with pytest.raises(SystemExit) as e: |
| 26 | + astyle_py_main([f'--astyle-version={ver}', '--version']) |
| 27 | + assert e.value.code == 0 |
| 28 | + out, err = capfd.readouterr() |
| 29 | + assert out == f'astyle-py {__version__} with Astyle v{ver}\n' |
| 30 | + assert err == '' |
| 31 | + |
| 32 | + |
| 33 | +def test_main_invalid_version(capfd: pytest.CaptureFixture[str]): |
| 34 | + ver = '1.2.3' |
| 35 | + with pytest.raises(SystemExit) as e: |
| 36 | + astyle_py_main([f'--astyle-version={ver}']) |
| 37 | + assert e.value.code == 1 |
| 38 | + out, err = capfd.readouterr() |
| 39 | + assert out == '' |
| 40 | + assert f'Unsupported astyle version: {ver}. Available versions:' in err |
| 41 | + |
| 42 | + |
| 43 | +def test_main_option_requires_value(capfd: pytest.CaptureFixture[str]): |
| 44 | + with pytest.raises(SystemExit) as e: |
| 45 | + astyle_py_main(['--exclude']) |
| 46 | + assert e.value.code == 1 |
| 47 | + out, err = capfd.readouterr() |
| 48 | + assert out == '' |
| 49 | + assert 'Option --exclude requires a value' in err |
| 50 | + |
| 51 | + |
| 52 | +def test_main_no_files(capfd: pytest.CaptureFixture[str]): |
| 53 | + with pytest.raises(SystemExit) as e: |
| 54 | + astyle_py_main([]) |
| 55 | + assert e.value.code == 0 |
| 56 | + out, err = capfd.readouterr() |
| 57 | + assert out == '' |
| 58 | + assert err == 'No files specified\n' |
| 59 | + |
| 60 | + |
| 61 | +def test_main_formatting_error_invalid_arg( |
| 62 | + capfd: pytest.CaptureFixture[str], tmp_path: pathlib.Path |
| 63 | +): |
| 64 | + base = str(tmp_path.absolute()) |
| 65 | + (tmp_path / 'file_a.c').touch() |
| 66 | + |
| 67 | + with chdir_ctx(base): |
| 68 | + with pytest.raises(SystemExit) as e: |
| 69 | + astyle_py_main(['--invalid-arg', 'file_a.c']) |
| 70 | + assert e.value.code == 1 |
| 71 | + out, err = capfd.readouterr() |
| 72 | + assert out == '' |
| 73 | + assert ( |
| 74 | + 'Error formatting file_a.c: error: Invalid Artistic Style options:\n\tinvalid-arg\n' |
| 75 | + in err |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def test_formatted_quiet(capfd: pytest.CaptureFixture[str], tmp_path: pathlib.Path): |
| 80 | + # Check in formatting mode (no --dry-run) |
| 81 | + base = str(tmp_path.absolute()) |
| 82 | + (tmp_path / 'file_a.c').write_text('int main() { foo(); }\n') |
| 83 | + (tmp_path / 'file_b.c').write_text('int main()\n{\n foo();\n}\n') |
| 84 | + |
| 85 | + with chdir_ctx(base): |
| 86 | + with pytest.raises(SystemExit) as e: |
| 87 | + astyle_py_main(['--style=otbs', '--quiet', 'file_a.c', 'file_b.c']) |
| 88 | + assert e.value.code == 0 |
| 89 | + out, err = capfd.readouterr() |
| 90 | + assert out == '' |
| 91 | + assert err == '' |
| 92 | + |
| 93 | + # first file formatted, 2nd unchanged |
| 94 | + assert (tmp_path / 'file_a.c').read_text() == 'int main()\n{\n foo();\n}\n' |
| 95 | + assert (tmp_path / 'file_b.c').read_text() == 'int main()\n{\n foo();\n}\n' |
| 96 | + |
| 97 | + # Now check in --dry-run mode (no formatting) |
| 98 | + (tmp_path / 'file_a.c').write_text('int main() { foo(); }\n') |
| 99 | + (tmp_path / 'file_b.c').write_text('int main()\n{\n foo();\n}\n') |
| 100 | + |
| 101 | + with chdir_ctx(base): |
| 102 | + with pytest.raises(SystemExit) as e: |
| 103 | + astyle_py_main( |
| 104 | + ['--style=otbs', '--quiet', '--dry-run', 'file_a.c', 'file_b.c'] |
| 105 | + ) |
| 106 | + assert e.value.code == 1 |
| 107 | + out, err = capfd.readouterr() |
| 108 | + assert out == '' |
| 109 | + assert err == '' |
| 110 | + |
| 111 | + # Files should not be modified in dry-run mode |
| 112 | + assert (tmp_path / 'file_a.c').read_text() == 'int main() { foo(); }\n' |
| 113 | + assert (tmp_path / 'file_b.c').read_text() == 'int main()\n{\n foo();\n}\n' |
| 114 | + |
| 115 | + |
| 116 | +def test_formatted_verbose(capfd: pytest.CaptureFixture[str], tmp_path: pathlib.Path): |
| 117 | + base = str(tmp_path.absolute()) |
| 118 | + (tmp_path / 'file_a.c').write_text('int main() { foo(); }\n') |
| 119 | + (tmp_path / 'file_b.c').write_text('int main()\n{\n foo();\n}\n') |
| 120 | + |
| 121 | + with chdir_ctx(base): |
| 122 | + with pytest.raises(SystemExit) as e: |
| 123 | + astyle_py_main(['--style=otbs', 'file_a.c', 'file_b.c']) |
| 124 | + assert e.value.code == 0 |
| 125 | + out, err = capfd.readouterr() |
| 126 | + assert out == '' |
| 127 | + assert 'Formatting file_a.c' in err |
| 128 | + assert 'Formatted 1 files' in err |
| 129 | + |
| 130 | + assert (tmp_path / 'file_a.c').read_text() == 'int main()\n{\n foo();\n}\n' |
| 131 | + assert (tmp_path / 'file_b.c').read_text() == 'int main()\n{\n foo();\n}\n' |
| 132 | + |
| 133 | + |
| 134 | +def test_formatted_verbose_dry_run( |
| 135 | + capfd: pytest.CaptureFixture[str], tmp_path: pathlib.Path |
| 136 | +): |
| 137 | + base = str(tmp_path.absolute()) |
| 138 | + (tmp_path / 'file_a.c').write_text('int main() { foo(); }\n') |
| 139 | + (tmp_path / 'file_b.c').write_text('int main()\n{\n foo();\n}\n') |
| 140 | + |
| 141 | + with chdir_ctx(base): |
| 142 | + with pytest.raises(SystemExit) as e: |
| 143 | + astyle_py_main(['--style=otbs', '--dry-run', 'file_a.c', 'file_b.c']) |
| 144 | + assert e.value.code == 1 |
| 145 | + out, err = capfd.readouterr() |
| 146 | + assert out == '' |
| 147 | + assert 'Formatting error in file_a.c' in err |
| 148 | + assert 'Formatting errors found in 1 files' in err |
| 149 | + |
| 150 | + assert (tmp_path / 'file_a.c').read_text() == 'int main() { foo(); }\n' |
| 151 | + assert (tmp_path / 'file_b.c').read_text() == 'int main()\n{\n foo();\n}\n' |
| 152 | + |
| 153 | + |
| 154 | +def test_all_files_excluded_warning( |
| 155 | + capfd: pytest.CaptureFixture[str], tmp_path: pathlib.Path |
| 156 | +): |
| 157 | + base = str(tmp_path.absolute()) |
| 158 | + (tmp_path / 'file_a.c').touch() |
| 159 | + |
| 160 | + with chdir_ctx(base): |
| 161 | + with pytest.raises(SystemExit) as e: |
| 162 | + astyle_py_main(['--exclude=*.c', 'file_a.c']) |
| 163 | + assert e.value.code == 0 |
| 164 | + out, err = capfd.readouterr() |
| 165 | + assert out == '' |
| 166 | + assert 'No files checked, excluded by --exclude/--exclude-list option' in err |
0 commit comments