Skip to content

Commit

Permalink
Refactor temporary test directory creation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
calcmogul committed Dec 4, 2020
1 parent 3372907 commit d9abf83
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 132 deletions.
28 changes: 1 addition & 27 deletions .styleguide
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
cppHeaderFileInclude {
\.h$
\.hpp$
\.inc$
}

cppSrcFileInclude {
\.cpp$
}

# Extra "/" used by unit tests
generatedFileExclude {
/cpplint\.py$
cpplint\.py$
}

modifiableFileExclude {
\.png$
}

licenseUpdateExclude {
Excluded\.h$
}

# Ordered this way to ensure tests find longest match
includeGuardRoots {
wpiformat/
wpiformat/Test/
}

# Used by unit tests
includeProject {
^ctre/
}
3 changes: 1 addition & 2 deletions .styleguide-license
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/*{padding}Company Name{padding}*/
/* Copyright (c) {year} Company Name. All Rights Reserved.{padding}*/
// Copyright (c) {year} FIRST
16 changes: 16 additions & 0 deletions wpiformat/wpiformat/test/tempdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import tempfile


class OpenTemporaryDirectory():

def __init__(self):
self.prev_dir = os.getcwd()

def __enter__(self):
self.temp_dir = tempfile.TemporaryDirectory()
os.chdir(self.temp_dir.name)
return self.temp_dir

def __exit__(self, type, value, traceback):
os.chdir(self.prev_dir)
15 changes: 14 additions & 1 deletion wpiformat/wpiformat/test/test_cidentlist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from .tasktest import *
from .tempdir import *
from wpiformat.cidentlist import CIdentList


Expand Down Expand Up @@ -299,4 +300,16 @@ def test_cidentlist():
"}" + os.linesep)
test.add_latest_input_as_output(True)

test.run(OutputType.FILE)
with OpenTemporaryDirectory():
with open(".styleguide", "w") as file:
file.write(r"""cppHeaderFileInclude {
\.h$
\.hpp$
\.inc$
}
includeProject {
^ctre/
}
""")
test.run(OutputType.FILE)
47 changes: 34 additions & 13 deletions wpiformat/wpiformat/test/test_config.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
import os

from .tempdir import *
from wpiformat.config import Config


def test_config():
config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
assert config_file.is_modifiable_file("." + os.sep + "wpiformat" + os.sep +
"javaguidelink.png")
assert config_file.is_generated_file("." + os.sep + "wpiformat" + os.sep +
"wpiformat" + os.sep + "cpplint.py")
with OpenTemporaryDirectory():
with open(".styleguide", "w") as file:
file.write(r"""cppHeaderFileInclude {
\.h$
\.inc$
}
cppSrcFileInclude {
\.cpp$
}
# Extra "/" used by unit tests
generatedFileExclude {
/cpplint\.py$
}
modifiableFileExclude {
\.png$
}
""")

config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
assert config_file.is_modifiable_file("." + os.sep + "wpiformat" +
os.sep + "javaguidelink.png")
assert config_file.is_generated_file("." + os.sep + "wpiformat" +
os.sep + "wpiformat" + os.sep +
"cpplint.py")

assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
os.sep + "diff_cpplint.py")
assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
os.sep + "update_cpplint.py")
assert not config_file.is_modifiable_file("." + os.sep + "wpiformat" +
os.sep + "license.txt")
assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
os.sep + "diff_cpplint.py")
assert not config_file.is_generated_file("." + os.sep + "wpiformat" +
os.sep + "update_cpplint.py")
assert not config_file.is_modifiable_file("." + os.sep + "wpiformat" +
os.sep + "license.txt")
143 changes: 76 additions & 67 deletions wpiformat/wpiformat/test/test_includeguard.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,94 @@
import os

from .tasktest import *
from .tempdir import *
from wpiformat.includeguard import IncludeGuard
from wpiformat.task import Task


def test_includeguard():
test = TaskTest(IncludeGuard())

repo_root = os.path.basename(Task.get_repo_root()).upper()
with OpenTemporaryDirectory():
with open(".styleguide", "w") as file:
# Roots are ordered this way to ensure tests find longest match
file.write(r"""includeGuardRoots {
wpiformat/
wpiformat/Test/
}
""")
repo_root = os.path.basename(Task.get_repo_root()).upper()

# Fix incorrect include guard
test.add_input("./Test.h",
"#ifndef WRONG_H" + os.linesep + \
"#define WRONG_C" + os.linesep + \
os.linesep + \
"#endif" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)
# Fix incorrect include guard
test.add_input("./Test.h",
"#ifndef WRONG_H" + os.linesep + \
"#define WRONG_C" + os.linesep + \
os.linesep + \
"#endif" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)

# Ensure nested preprocessor statements are handled properly for incorrect
# include guard
test.add_input("./Test.h",
"#ifndef WRONG_H" + os.linesep + \
"#define WRONG_C" + os.linesep + \
os.linesep + \
"#if SOMETHING" + os.linesep + \
"// do something" + os.linesep + \
"#endif" + os.linesep + \
"#endif" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#if SOMETHING" + os.linesep + \
"// do something" + os.linesep + \
"#endif" + os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)
# Ensure nested preprocessor statements are handled properly for incorrect
# include guard
test.add_input("./Test.h",
"#ifndef WRONG_H" + os.linesep + \
"#define WRONG_C" + os.linesep + \
os.linesep + \
"#if SOMETHING" + os.linesep + \
"// do something" + os.linesep + \
"#endif" + os.linesep + \
"#endif" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#if SOMETHING" + os.linesep + \
"// do something" + os.linesep + \
"#endif" + os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)

# Don't touch correct include guard
test.add_input("./Test.h",
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep)
test.add_latest_input_as_output(True)
# Don't touch correct include guard
test.add_input("./Test.h",
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep)
test.add_latest_input_as_output(True)

# Fail on missing include guard
test.add_input("./Test.h", "// Empty file" + os.linesep)
test.add_latest_input_as_output(False)
# Fail on missing include guard
test.add_input("./Test.h", "// Empty file" + os.linesep)
test.add_latest_input_as_output(False)

# Verify pragma once counts as include guard
test.add_input("./Test.h", "#pragma once" + os.linesep)
test.add_latest_input_as_output(True)
# Verify pragma once counts as include guard
test.add_input("./Test.h", "#pragma once" + os.linesep)
test.add_latest_input_as_output(True)

# Ensure include guard roots are processed correctly
test.add_input("./Test.h",
"#ifndef " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \
"#define " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)
# Ensure include guard roots are processed correctly
test.add_input("./Test.h",
"#ifndef " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \
"#define " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_WPIFORMAT_TEST_H_" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)

# Ensure leading underscores are removed (this occurs if the user doesn't
# include a trailing "/" in the include guard root)
test.add_input("./Test/Test.h",
"#ifndef " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \
"#define " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)
# Ensure leading underscores are removed (this occurs if the user doesn't
# include a trailing "/" in the include guard root)
test.add_input("./Test/Test.h",
"#ifndef " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \
"#define " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_WPIFORMAT_TEST_TEST_H_" + os.linesep)
test.add_output(
"#ifndef " + repo_root + "_TEST_H_" + os.linesep + \
"#define " + repo_root + "_TEST_H_" + os.linesep + \
os.linesep + \
"#endif // " + repo_root + "_TEST_H_" + os.linesep, True, True)

test.run(OutputType.FILE)
test.run(OutputType.FILE)
15 changes: 14 additions & 1 deletion wpiformat/wpiformat/test/test_includeorder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from .tasktest import *
from .tempdir import *
from wpiformat.includeorder import IncludeOrder


Expand Down Expand Up @@ -546,4 +547,16 @@ def test_includeorder():
"#endif" + os.linesep)
test.add_latest_input_as_output(True)

test.run(OutputType.FILE)
with OpenTemporaryDirectory():
with open(".styleguide", "w") as file:
file.write(r"""cppHeaderFileInclude {
\.h$
\.hpp$
\.inc$
}
includeProject {
^ctre/
}
""")
test.run(OutputType.FILE)
Loading

0 comments on commit d9abf83

Please sign in to comment.