|
1 | 1 | import os
|
2 | 2 |
|
3 | 3 | from .tasktest import *
|
| 4 | +from .tempdir import * |
4 | 5 | from wpiformat.includeguard import IncludeGuard
|
5 | 6 | from wpiformat.task import Task
|
6 | 7 |
|
7 | 8 |
|
8 | 9 | def test_includeguard():
|
9 | 10 | test = TaskTest(IncludeGuard())
|
10 | 11 |
|
11 |
| - repo_root = os.path.basename(Task.get_repo_root()).upper() |
| 12 | + with OpenTemporaryDirectory(): |
| 13 | + with open(".styleguide", "w") as file: |
| 14 | + # Roots are ordered this way to ensure tests find longest match |
| 15 | + file.write(r"""includeGuardRoots { |
| 16 | + wpiformat/ |
| 17 | + wpiformat/Test/ |
| 18 | +} |
| 19 | +""") |
| 20 | + repo_root = os.path.basename(Task.get_repo_root()).upper() |
12 | 21 |
|
13 |
| - # Fix incorrect include guard |
14 |
| - test.add_input("./Test.h", |
15 |
| - "#ifndef WRONG_H" + os.linesep + \ |
16 |
| - "#define WRONG_C" + os.linesep + \ |
17 |
| - os.linesep + \ |
18 |
| - "#endif" + os.linesep) |
19 |
| - test.add_output( |
20 |
| - "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
21 |
| - "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
22 |
| - os.linesep + \ |
23 |
| - "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
| 22 | + # Fix incorrect include guard |
| 23 | + test.add_input("./Test.h", |
| 24 | + "#ifndef WRONG_H" + os.linesep + \ |
| 25 | + "#define WRONG_C" + os.linesep + \ |
| 26 | + os.linesep + \ |
| 27 | + "#endif" + os.linesep) |
| 28 | + test.add_output( |
| 29 | + "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
| 30 | + "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
| 31 | + os.linesep + \ |
| 32 | + "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
24 | 33 |
|
25 |
| - # Ensure nested preprocessor statements are handled properly for incorrect |
26 |
| - # include guard |
27 |
| - test.add_input("./Test.h", |
28 |
| - "#ifndef WRONG_H" + os.linesep + \ |
29 |
| - "#define WRONG_C" + os.linesep + \ |
30 |
| - os.linesep + \ |
31 |
| - "#if SOMETHING" + os.linesep + \ |
32 |
| - "// do something" + os.linesep + \ |
33 |
| - "#endif" + os.linesep + \ |
34 |
| - "#endif" + os.linesep) |
35 |
| - test.add_output( |
36 |
| - "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
37 |
| - "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
38 |
| - os.linesep + \ |
39 |
| - "#if SOMETHING" + os.linesep + \ |
40 |
| - "// do something" + os.linesep + \ |
41 |
| - "#endif" + os.linesep + \ |
42 |
| - "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
| 34 | + # Ensure nested preprocessor statements are handled properly for incorrect |
| 35 | + # include guard |
| 36 | + test.add_input("./Test.h", |
| 37 | + "#ifndef WRONG_H" + os.linesep + \ |
| 38 | + "#define WRONG_C" + os.linesep + \ |
| 39 | + os.linesep + \ |
| 40 | + "#if SOMETHING" + os.linesep + \ |
| 41 | + "// do something" + os.linesep + \ |
| 42 | + "#endif" + os.linesep + \ |
| 43 | + "#endif" + os.linesep) |
| 44 | + test.add_output( |
| 45 | + "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
| 46 | + "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
| 47 | + os.linesep + \ |
| 48 | + "#if SOMETHING" + os.linesep + \ |
| 49 | + "// do something" + os.linesep + \ |
| 50 | + "#endif" + os.linesep + \ |
| 51 | + "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
43 | 52 |
|
44 |
| - # Don't touch correct include guard |
45 |
| - test.add_input("./Test.h", |
46 |
| - "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
47 |
| - "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
48 |
| - os.linesep + \ |
49 |
| - "#endif // " + repo_root + "_TEST_H_" + os.linesep) |
50 |
| - test.add_latest_input_as_output(True) |
| 53 | + # Don't touch correct include guard |
| 54 | + test.add_input("./Test.h", |
| 55 | + "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
| 56 | + "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
| 57 | + os.linesep + \ |
| 58 | + "#endif // " + repo_root + "_TEST_H_" + os.linesep) |
| 59 | + test.add_latest_input_as_output(True) |
51 | 60 |
|
52 |
| - # Fail on missing include guard |
53 |
| - test.add_input("./Test.h", "// Empty file" + os.linesep) |
54 |
| - test.add_latest_input_as_output(False) |
| 61 | + # Fail on missing include guard |
| 62 | + test.add_input("./Test.h", "// Empty file" + os.linesep) |
| 63 | + test.add_latest_input_as_output(False) |
55 | 64 |
|
56 |
| - # Verify pragma once counts as include guard |
57 |
| - test.add_input("./Test.h", "#pragma once" + os.linesep) |
58 |
| - test.add_latest_input_as_output(True) |
| 65 | + # Verify pragma once counts as include guard |
| 66 | + test.add_input("./Test.h", "#pragma once" + os.linesep) |
| 67 | + test.add_latest_input_as_output(True) |
59 | 68 |
|
60 |
| - # Ensure include guard roots are processed correctly |
61 |
| - test.add_input("./Test.h", |
62 |
| - "#ifndef " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \ |
63 |
| - "#define " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \ |
64 |
| - os.linesep + \ |
65 |
| - "#endif // " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep) |
66 |
| - test.add_output( |
67 |
| - "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
68 |
| - "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
69 |
| - os.linesep + \ |
70 |
| - "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
| 69 | + # Ensure include guard roots are processed correctly |
| 70 | + test.add_input("./Test.h", |
| 71 | + "#ifndef " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \ |
| 72 | + "#define " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \ |
| 73 | + os.linesep + \ |
| 74 | + "#endif // " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep) |
| 75 | + test.add_output( |
| 76 | + "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
| 77 | + "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
| 78 | + os.linesep + \ |
| 79 | + "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
71 | 80 |
|
72 |
| - # Ensure leading underscores are removed (this occurs if the user doesn't |
73 |
| - # include a trailing "/" in the include guard root) |
74 |
| - test.add_input("./Test/Test.h", |
75 |
| - "#ifndef " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \ |
76 |
| - "#define " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \ |
77 |
| - os.linesep + \ |
78 |
| - "#endif // " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep) |
79 |
| - test.add_output( |
80 |
| - "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
81 |
| - "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
82 |
| - os.linesep + \ |
83 |
| - "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
| 81 | + # Ensure leading underscores are removed (this occurs if the user doesn't |
| 82 | + # include a trailing "/" in the include guard root) |
| 83 | + test.add_input("./Test/Test.h", |
| 84 | + "#ifndef " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \ |
| 85 | + "#define " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \ |
| 86 | + os.linesep + \ |
| 87 | + "#endif // " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep) |
| 88 | + test.add_output( |
| 89 | + "#ifndef " + repo_root + "_TEST_H_" + os.linesep + \ |
| 90 | + "#define " + repo_root + "_TEST_H_" + os.linesep + \ |
| 91 | + os.linesep + \ |
| 92 | + "#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True) |
84 | 93 |
|
85 |
| - test.run(OutputType.FILE) |
| 94 | + test.run(OutputType.FILE) |
0 commit comments