|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +from typing import Generator |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from py_app_dev.core.compile_commands import CompilationDatabase, CompilationOptionsManager, CompileCommand |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def temp_compilation_database(tmp_path: Path) -> Generator[Path, None, None]: |
| 12 | + db_content = [ |
| 13 | + {"directory": tmp_path.as_posix(), "file": "test1.c", "arguments": ["gcc", "-I/usr/include", "-DDEBUG", "test1.c"]}, |
| 14 | + {"directory": tmp_path.as_posix(), "file": "test2.c", "command": "gcc -c -O2 test2.c"}, |
| 15 | + ] |
| 16 | + db_file = tmp_path / "compile_commands.json" |
| 17 | + db_file.write_text(json.dumps(db_content)) |
| 18 | + yield db_file |
| 19 | + |
| 20 | + |
| 21 | +def test_compilation_options_manager_without_database() -> None: |
| 22 | + manager = CompilationOptionsManager() |
| 23 | + assert manager.get_compile_options(Path("any_file.c")) == ["-std=c11"] |
| 24 | + |
| 25 | + |
| 26 | +def test_compilation_options_manager_with_database(temp_compilation_database: Path) -> None: |
| 27 | + manager = CompilationOptionsManager(temp_compilation_database) |
| 28 | + |
| 29 | + # Test for file in the database |
| 30 | + test1_path = Path(temp_compilation_database.parent) / "test1.c" |
| 31 | + options = manager.get_compile_options(test1_path) |
| 32 | + assert options == ["-I/usr/include", "-DDEBUG"] |
| 33 | + |
| 34 | + # Test for file in the database with command string |
| 35 | + test2_path = Path(temp_compilation_database.parent) / "test2.c" |
| 36 | + options = manager.get_compile_options(test2_path) |
| 37 | + assert options == ["-O2"] |
| 38 | + |
| 39 | + # Test for file not in the database |
| 40 | + not_in_db_path = Path(temp_compilation_database.parent) / "not_in_db.c" |
| 41 | + options = manager.get_compile_options(not_in_db_path) |
| 42 | + assert options == ["-std=c11"] |
| 43 | + |
| 44 | + |
| 45 | +def test_compilation_options_manager_no_default() -> None: |
| 46 | + manager = CompilationOptionsManager(no_default=True) |
| 47 | + options = manager.get_compile_options(Path("any_file.c")) |
| 48 | + assert options == [] |
| 49 | + |
| 50 | + |
| 51 | +def test_set_default_options() -> None: |
| 52 | + manager = CompilationOptionsManager() |
| 53 | + new_defaults = ["-std=c99", "-Wall"] |
| 54 | + manager.set_default_options(new_defaults) |
| 55 | + assert manager.get_compile_options(Path("any_file.c")) == new_defaults |
| 56 | + |
| 57 | + |
| 58 | +def test_compilation_database_from_json() -> None: |
| 59 | + json_data = """ |
| 60 | + [ |
| 61 | + { |
| 62 | + "directory": "/home/user/project", |
| 63 | + "file": "main.c", |
| 64 | + "arguments": ["gcc", "-c", "-I/usr/include", "main.c"] |
| 65 | + } |
| 66 | + ] |
| 67 | + """ |
| 68 | + tmp_file = Path("temp_compile_commands.json") |
| 69 | + tmp_file.write_text(json_data) |
| 70 | + |
| 71 | + try: |
| 72 | + db = CompilationDatabase.from_json_file(tmp_file) |
| 73 | + assert len(db.commands) == 1 |
| 74 | + assert db.commands[0].directory == Path("/home/user/project") |
| 75 | + assert db.commands[0].file == Path("main.c") |
| 76 | + assert db.commands[0].arguments == ["gcc", "-c", "-I/usr/include", "main.c"] |
| 77 | + finally: |
| 78 | + tmp_file.unlink() |
| 79 | + |
| 80 | + |
| 81 | +def test_get_compile_commands() -> None: |
| 82 | + json_data = """ |
| 83 | + [ |
| 84 | + { |
| 85 | + "directory": "/home/user/project", |
| 86 | + "file": "main.c", |
| 87 | + "arguments": ["gcc", "-c", "-I/usr/include", "main.c"] |
| 88 | + }, |
| 89 | + { |
| 90 | + "directory": "/home/user/project", |
| 91 | + "file": "helper.c", |
| 92 | + "command": "gcc -c -O2 helper.c" |
| 93 | + } |
| 94 | + ] |
| 95 | + """ |
| 96 | + tmp_file = Path("temp_compile_commands.json") |
| 97 | + tmp_file.write_text(json_data) |
| 98 | + |
| 99 | + try: |
| 100 | + db = CompilationDatabase.from_json_file(tmp_file) |
| 101 | + commands = db.getCompileCommands(Path("/home/user/project/main.c")) |
| 102 | + assert len(commands) == 1 |
| 103 | + assert commands[0].file == Path("main.c") |
| 104 | + assert commands[0].arguments == ["gcc", "-c", "-I/usr/include", "main.c"] |
| 105 | + |
| 106 | + commands = db.getCompileCommands(Path("/home/user/project/helper.c")) |
| 107 | + assert len(commands) == 1 |
| 108 | + assert commands[0].file == Path("helper.c") |
| 109 | + assert commands[0].command == "gcc -c -O2 helper.c" |
| 110 | + |
| 111 | + commands = db.getCompileCommands(Path("/home/user/project/nonexistent.c")) |
| 112 | + assert len(commands) == 0 |
| 113 | + finally: |
| 114 | + tmp_file.unlink() |
| 115 | + |
| 116 | + |
| 117 | +@pytest.fixture |
| 118 | +def compile_command(): |
| 119 | + return CompileCommand(directory=Path("/home/user/project"), file=Path("/home/user/project/input.c"), output=Path("/home/user/project/output.o")) |
| 120 | + |
| 121 | + |
| 122 | +def test_clean_up_arguments_basic(compile_command): |
| 123 | + arguments = ["gcc", "-DStuff", "-ISome/Path", "-o", "/home/user/project/output.o", "/home/user/project/input.c"] |
| 124 | + expected = ["-DStuff", "-ISome/Path"] |
| 125 | + assert compile_command.clean_up_arguments(arguments) == expected |
| 126 | + |
| 127 | + |
| 128 | +def test_clean_up_arguments_with_c_option(compile_command): |
| 129 | + arguments = ["gcc", "-DStuff", "-ISome/Path", "-c", "-o", "/home/user/project/output.o", "/home/user/project/input.c"] |
| 130 | + expected = ["-DStuff", "-ISome/Path"] |
| 131 | + assert compile_command.clean_up_arguments(arguments) == expected |
| 132 | + |
| 133 | + |
| 134 | +def test_clean_up_arguments_with_combined_options(compile_command): |
| 135 | + arguments = ["gcc", "-DStuff", "-ISome/Path", "-c", "-o/home/user/project/output.o", "/home/user/project/input.c"] |
| 136 | + expected = ["-DStuff", "-ISome/Path"] |
| 137 | + assert compile_command.clean_up_arguments(arguments) == expected |
| 138 | + |
| 139 | + |
| 140 | +def test_clean_up_arguments_with_multiple_input_files(compile_command): |
| 141 | + arguments = ["gcc", "-DStuff", "-ISome/Path", "-c", "/home/user/project/input.c", "/home/user/project/helper.c", "-o", "/home/user/project/output.o"] |
| 142 | + expected = ["-DStuff", "-ISome/Path", "/home/user/project/helper.c"] |
| 143 | + assert compile_command.clean_up_arguments(arguments) == expected |
| 144 | + |
| 145 | + |
| 146 | +def test_clean_up_arguments_with_complex_options(compile_command): |
| 147 | + arguments = ["gcc", "-DStuff", "-ISome/Path", "-Werror", "-Wall", "-std=c11", '-DVERSION="1.0"', "-c", "/home/user/project/input.c", "-o", "/home/user/project/output.o"] |
| 148 | + expected = ["-DStuff", "-ISome/Path", "-Werror", "-Wall", "-std=c11", '-DVERSION="1.0"'] |
| 149 | + assert compile_command.clean_up_arguments(arguments) == expected |
| 150 | + |
| 151 | + |
| 152 | +def test_clean_up_arguments_with_partial_paths(compile_command): |
| 153 | + arguments = ["gcc", "-DStuff", "-I/home/user/project", "-Werror", "-Wall", "-c", "project/input.c", "-o", "project/output.o"] |
| 154 | + expected = ["-DStuff", "-I/home/user/project", "-Werror", "-Wall"] |
| 155 | + assert compile_command.clean_up_arguments(arguments) == expected |
0 commit comments