Skip to content

Commit

Permalink
Always build with coverage to simplify the test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jpnock committed Feb 21, 2024
1 parent 2c42dd8 commit 02d07f4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Based on https://stackoverflow.com/a/52036564 which is well worth reading!

CXXFLAGS += -std=c++20 -W -Wall -g -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -fsanitize=address -static-libasan -O0 -rdynamic -I include
CXXFLAGS += -std=c++20 -W -Wall -g -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -fsanitize=address -static-libasan -O0 -rdynamic --coverage -I include

SOURCES := $(wildcard src/*.cpp)
DEPENDENCIES := $(patsubst src/%.cpp,build/%.d,$(SOURCES))
Expand Down Expand Up @@ -30,12 +30,10 @@ build/lexer.yy.cpp: src/lexer.flex build/parser.tab.hpp
@mkdir -p build
flex -o build/lexer.yy.cpp src/lexer.flex

with_coverage : CXXFLAGS += --coverage
with_coverage : bin/c_compiler

coverage : coverage/index.html

coverage/index.html :
coverage:
@rm -rf coverage/
@mkdir -p coverage
# somehow lexer and parser coverage info are available but not accurate. To exclude them use:
# lcov -c --no-external --exclude "`pwd`/src/lexer.*" --exclude "`pwd`/src/parser.*" -d . -o coverage/cov.info
Expand Down
25 changes: 8 additions & 17 deletions scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
This script will also generate a JUnit XML file, which can be used to integrate
with CI/CD pipelines.
Usage: test.py [-h] [-m] [-s] [--version] [--no_clean | --coverage] [dir]
Usage: test.py [-h] [-m] [-s] [--version] [--no_clean] [--coverage] [dir]
Example usage: scripts/test.py compiler_tests/_example
Expand Down Expand Up @@ -322,23 +322,16 @@ def clean() -> bool:
return False
return True

def make(with_coverage: bool, silent: bool) -> bool:
def make(silent: bool) -> bool:
"""
Wrapper for make bin/c_compiler.
Return True if successful, False otherwise
"""
print(GREEN + "Running make..." + RESET)

cmd = ["make", "-C", PROJECT_LOCATION, "bin/c_compiler"]
if with_coverage:
# Run coverage if needed
print(GREEN + "Making with coverage..." + RESET)
shutil.rmtree(COVERAGE_FOLDER, ignore_errors=True)
cmd = ["make", "-C", PROJECT_LOCATION, "with_coverage"]

return_code, error_msg, _ = run_subprocess(cmd=cmd, timeout=BUILD_TIMEOUT_SECONDS, silent=silent)

return_code, error_msg, _ = run_subprocess(
cmd=["make", "-C", PROJECT_LOCATION, "bin/c_compiler"], timeout=BUILD_TIMEOUT_SECONDS, silent=silent
)
if return_code != 0:
print(RED + "Error when making:", error_msg + RESET)
return False
Expand Down Expand Up @@ -465,17 +458,15 @@ def parse_args():
action="version",
version=f"BetterTesting {__version__}"
)
# Coverage cannot be perfomed without rebuilding the compiler
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument(
parser.add_argument(
"--no-clean",
'--no_clean',
action="store_true",
default=False,
help="Don't clean the repository before testing. This will make it "
"faster but it can be safer to clean if you have any compilation issues."
)
group.add_argument(
parser.add_argument(
"--coverage",
action="store_true",
default=False,
Expand All @@ -494,7 +485,7 @@ def main():
# Clean the repo if required and exit if this fails.
exit(2)

if not make(with_coverage=args.coverage, silent=args.short):
if not make(silent=args.short):
exit(3)

with JUnitXMLFile(J_UNIT_OUTPUT_FILE) as xml_file:
Expand Down
13 changes: 3 additions & 10 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ if [ "${DONT_CLEAN:-}" != "1" ]; then
make clean
fi

if [ "${COVERAGE:-}" == "1" ]; then
rm -rf coverage
set -e
make with_coverage
set +e
else
set -e
make bin/c_compiler
set +e
fi
set -e
make bin/c_compiler
set +e

mkdir -p bin
mkdir -p bin/output
Expand Down

0 comments on commit 02d07f4

Please sign in to comment.