Import("env")


testenv = env.Clone()
test_flags = {"LIBS": ["gtest", "gmock", "upp", "pthread"]}
testenv.MergeFlags(test_flags)
test = testenv.Program(
    "testbin", ["main.cpp",
                "cli/parse_simple.cpp",
                "async/job.cpp",
                "util/defer.cpp",
                "timer/oneshot.cpp",
                "timer/interval.cpp",
                "sync/rwwrap.cpp",
                "function/function.cpp",
                "strutil/strutil.cpp",
		"traits/traits.cpp",
		"property_tree/property_tree.cpp",
		"parser/factory.cpp",
		"parser/regex.cpp",
		"parser/literal.cpp",
		"parser/joined.cpp",
		"parser/or.cpp",
		"parser/optional.cpp",
		"parser/kleenestar.cpp",
		"parser/kleeneplus.cpp",
		"parser/complex.cpp",
		"dualbuf/dualbuf.cpp",
                ])

Help("    testbin           Only build tests\n")

test_exec = testenv.Alias('test', [test], test[0].path)
Help("    test              Build and run tests.\n")

# same as Makefile .PHONY
testenv.AlwaysBuild(test_exec)

curdir = Dir('.').get_abspath()
rootdir = Dir('#').get_abspath()

# Clean and recreate coverage directory
coverage_dir = testenv.Command(
    f"{rootdir}/cov", "", f"rm -rf {rootdir}/cov && mkdir -p {rootdir}/cov"
)

# Create coverage report. NOTE: coverage files are created by running tests with coverage flags
coverage = testenv.Command(
    'upp.html', "", f"cd {curdir} && gcovr . --root {rootdir} --html --html-details -o {rootdir}/cov/upp.html")

coverage_report = testenv.Alias("coverage-report", [coverage_dir, coverage])

Help(
    "    coverage-report   Populate coverate report from gcov output.\n"
    "                      This requires manual 'scons --build-debug --build-cov test'\n"
    "                      run to output a valid report.\n")
