Skip to content

Commit b601cfd

Browse files
authored
Add --test flag for app (#166)
* add `--test` flag to app * add test
1 parent 2bcd29f commit b601cfd

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ OPTIONS
270270
Show this message
271271
--check
272272
Run checks instead of printing. If --checklist is not specified, all checks are run
273+
--test
274+
Run Test.jl-style checks instead of printing
273275
--checklist <check1,check2>,...
274276
Run checks specified by <check1>,<check2>,...
275277
This will imply --check.

src/main.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ function print_help()
135135
println(io, " --check")
136136
println(io,
137137
" Run checks instead of printing. If --checklist is not specified, all checks are run")
138+
println(io, " --test")
139+
println(io, " Run Test.jl-style checks instead of printing")
138140
println(io, " --checklist <check1,check2>,...")
139141
println(io,
140142
""" Run checks specified by <check1>,<check2>,...
@@ -160,6 +162,7 @@ function main(args)
160162
valid_check_values = [CHECKS; "all"; EXCLUDE_PREFIX .* CHECKS]
161163
selected_checks = copy(CHECKS)
162164
should_run_checks = false
165+
should_run_tests = false
163166
should_print = false
164167
path = "."
165168

@@ -172,6 +175,8 @@ function main(args)
172175
return 0
173176
elseif x == "--check"
174177
should_run_checks = true
178+
elseif x == "--test"
179+
should_run_tests = true
175180
elseif x == "--print"
176181
should_print = true
177182
elseif x == "--checklist"
@@ -216,8 +221,12 @@ function main(args)
216221
end
217222
end
218223

224+
if should_run_tests && should_run_checks
225+
return err("Arguments `--test` and `--check`/`--checklist` are mutually exclusive.")
226+
end
227+
219228
# Print by default
220-
if !should_run_checks && !should_print
229+
if !should_run_checks && !should_run_tests && !should_print
221230
should_print = true
222231
end
223232

@@ -237,6 +246,15 @@ function main(args)
237246
return 1
238247
end
239248
end
249+
if should_run_tests
250+
try
251+
@eval Main $ExplicitImports.test_explicit_imports($package)
252+
catch e
253+
printstyled(stderr, "ERROR: "; bold=true, color=:red)
254+
Base.showerror(stderr, e)
255+
return 1
256+
end
257+
end
240258
if should_run_checks
241259
if length(selected_checks) == 0
242260
return err("The passed combination of checks $values made the selection empty.")

test/main.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if isdefined(Base, Symbol("@main")) && VERSION >= v"1.12.0-DEV.102"
77
r"\s+" => " ")
88
@test contains(help, "SYNOPSIS")
99
@test contains(help, "Path to the root directory")
10+
@test contains(help, "--test")
1011
run1 = replace(readchomp(`$cmd --project=$(dir) -m ExplicitImports $dir`),
1112
r"\s+" => " ")
1213
@test contains(run1, "is not relying on any implicit imports.")
@@ -30,6 +31,7 @@ end
3031
r"\s+" => " ")
3132
@test contains(help, "SYNOPSIS")
3233
@test contains(help, "Path to the root directory")
34+
@test contains(help, "--test")
3335
run1 = replace(readchomp(`$cmd --project=$(dir) -e "using ExplicitImports: main; exit(main([\"$(dir)\"]))"`),
3436
r"\s+" => " ")
3537
@test contains(run1, "is not relying on any implicit imports.")
@@ -45,6 +47,26 @@ end
4547
"is not a supported flag, directory, or file. See the output of `--help` for usage details")
4648
end
4749

50+
@testset "Test --test flag" begin
51+
mktempdir() do tmp
52+
proj_path = joinpath(tmp, "Project.toml")
53+
src_dir = joinpath(tmp, "src")
54+
mkpath(src_dir)
55+
write(proj_path,
56+
"name = \"TmpPkg\"\n" *
57+
"uuid = \"00000000-0000-0000-0000-000000000001\"\n" *
58+
"version = \"0.1.0\"\n")
59+
write(joinpath(src_dir, "TmpPkg.jl"),
60+
"module TmpPkg\n\n" *
61+
"greet() = \"hello\"\n\n" *
62+
"end # module\n")
63+
@test ExplicitImports.main([tmp, "--test"]) == 0
64+
@test ExplicitImports.main([tmp, "--test", "--print"]) == 0
65+
@test ExplicitImports.main([tmp, "--test", "--check"]) == 1
66+
@test ExplicitImports.main([tmp, "--test", "--checklist", "all"]) == 1
67+
end
68+
end
69+
4870
@testset "Test checks" begin
4971
# Expected failure on no_implicit_imports due to DataFramesExt
5072
dir = joinpath(@__DIR__, "TestPkg")

0 commit comments

Comments
 (0)