Skip to content

Commit d9abf83

Browse files
committed
Refactor temporary test directory creation
Now the includeorder, includeguard, and config tests use a temporary directory with their own .styleguide files instead of using the wpiformat .styleguide file. This allowed massively simplifying wpiformat's .styleguide file.
1 parent 3372907 commit d9abf83

8 files changed

+179
-132
lines changed

.styleguide

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
1-
cppHeaderFileInclude {
2-
\.h$
3-
\.hpp$
4-
\.inc$
5-
}
6-
7-
cppSrcFileInclude {
8-
\.cpp$
9-
}
10-
11-
# Extra "/" used by unit tests
121
generatedFileExclude {
13-
/cpplint\.py$
2+
cpplint\.py$
143
}
154

165
modifiableFileExclude {
176
\.png$
187
}
19-
20-
licenseUpdateExclude {
21-
Excluded\.h$
22-
}
23-
24-
# Ordered this way to ensure tests find longest match
25-
includeGuardRoots {
26-
wpiformat/
27-
wpiformat/Test/
28-
}
29-
30-
# Used by unit tests
31-
includeProject {
32-
^ctre/
33-
}

.styleguide-license

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/*{padding}Company Name{padding}*/
2-
/* Copyright (c) {year} Company Name. All Rights Reserved.{padding}*/
1+
// Copyright (c) {year} FIRST

wpiformat/wpiformat/test/tempdir.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import tempfile
3+
4+
5+
class OpenTemporaryDirectory():
6+
7+
def __init__(self):
8+
self.prev_dir = os.getcwd()
9+
10+
def __enter__(self):
11+
self.temp_dir = tempfile.TemporaryDirectory()
12+
os.chdir(self.temp_dir.name)
13+
return self.temp_dir
14+
15+
def __exit__(self, type, value, traceback):
16+
os.chdir(self.prev_dir)

wpiformat/wpiformat/test/test_cidentlist.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
from .tasktest import *
4+
from .tempdir import *
45
from wpiformat.cidentlist import CIdentList
56

67

@@ -299,4 +300,16 @@ def test_cidentlist():
299300
"}" + os.linesep)
300301
test.add_latest_input_as_output(True)
301302

302-
test.run(OutputType.FILE)
303+
with OpenTemporaryDirectory():
304+
with open(".styleguide", "w") as file:
305+
file.write(r"""cppHeaderFileInclude {
306+
\.h$
307+
\.hpp$
308+
\.inc$
309+
}
310+
311+
includeProject {
312+
^ctre/
313+
}
314+
""")
315+
test.run(OutputType.FILE)
+34-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
import os
2-
1+
from .tempdir import *
32
from wpiformat.config import Config
43

54

65
def test_config():
7-
config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
8-
assert config_file.is_modifiable_file("." + os.sep + "wpiformat" + os.sep +
9-
"javaguidelink.png")
10-
assert config_file.is_generated_file("." + os.sep + "wpiformat" + os.sep +
11-
"wpiformat" + os.sep + "cpplint.py")
6+
with OpenTemporaryDirectory():
7+
with open(".styleguide", "w") as file:
8+
file.write(r"""cppHeaderFileInclude {
9+
\.h$
10+
\.inc$
11+
}
12+
13+
cppSrcFileInclude {
14+
\.cpp$
15+
}
16+
17+
# Extra "/" used by unit tests
18+
generatedFileExclude {
19+
/cpplint\.py$
20+
}
21+
22+
modifiableFileExclude {
23+
\.png$
24+
}
25+
""")
26+
27+
config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
28+
assert config_file.is_modifiable_file("." + os.sep + "wpiformat" +
29+
os.sep + "javaguidelink.png")
30+
assert config_file.is_generated_file("." + os.sep + "wpiformat" +
31+
os.sep + "wpiformat" + os.sep +
32+
"cpplint.py")
1233

13-
assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
14-
os.sep + "diff_cpplint.py")
15-
assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
16-
os.sep + "update_cpplint.py")
17-
assert not config_file.is_modifiable_file("." + os.sep + "wpiformat" +
18-
os.sep + "license.txt")
34+
assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
35+
os.sep + "diff_cpplint.py")
36+
assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
37+
os.sep + "update_cpplint.py")
38+
assert not config_file.is_modifiable_file("." + os.sep + "wpiformat" +
39+
os.sep + "license.txt")
+76-67
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,94 @@
11
import os
22

33
from .tasktest import *
4+
from .tempdir import *
45
from wpiformat.includeguard import IncludeGuard
56
from wpiformat.task import Task
67

78

89
def test_includeguard():
910
test = TaskTest(IncludeGuard())
1011

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()
1221

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)
2433

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)
4352

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)
5160

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)
5564

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)
5968

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)
7180

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)
8493

85-
test.run(OutputType.FILE)
94+
test.run(OutputType.FILE)

wpiformat/wpiformat/test/test_includeorder.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
from .tasktest import *
4+
from .tempdir import *
45
from wpiformat.includeorder import IncludeOrder
56

67

@@ -546,4 +547,16 @@ def test_includeorder():
546547
"#endif" + os.linesep)
547548
test.add_latest_input_as_output(True)
548549

549-
test.run(OutputType.FILE)
550+
with OpenTemporaryDirectory():
551+
with open(".styleguide", "w") as file:
552+
file.write(r"""cppHeaderFileInclude {
553+
\.h$
554+
\.hpp$
555+
\.inc$
556+
}
557+
558+
includeProject {
559+
^ctre/
560+
}
561+
""")
562+
test.run(OutputType.FILE)

0 commit comments

Comments
 (0)