-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test_warn macro to Base.Test for checking for warnings #19903
Conversation
""" | ||
@test_warn msg expr | ||
|
||
Test whether evaluating `expr` results in `stderr` output that contains |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stderr
-> STDERR
and link ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, maybe include an example?
""" | ||
@test_nowarn expr | ||
|
||
Test whether evaluating `expr` results in empty `stderr` output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here re stderr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! This is great!
Also added a commit to use |
vers = Pkg.installed("Example") | ||
branch = LibGit2.with(LibGit2.GitRepo, Pkg.dir("Example")) do repo | ||
LibGit2.branch(repo) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strange indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will fix, sorry
ret, out, err = @grab_outputs Pkg.pin("Example") | ||
@test ret === nothing && out == "" | ||
@test ismatch(r"INFO: Creating Example branch pinned\.[0-9a-f]{8}\.tmp", err) | ||
@test !contains(err, "INFO: No packages to install, update or remove") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't delete this test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't deleted, and in fact the test was strengthened: The new regex test has r"^...$"
so that it checks that exactly one INFO
message is printed.
ret, out, err = @grab_outputs Pkg.pin("Example", v"0.4.0") | ||
@test ret === nothing && out == "" | ||
@test contains(err, "INFO: Package Example is already pinned to the selected commit") | ||
@test !contains(err, "INFO: No packages to install, update or remove") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't delete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't deleted: I used r"^....$"
as above to make sure only one warning is printed
@test contains(err, "INFO: Package Example: skipping update (pinned)...\n") | ||
@test ismatch(r"INFO: Package Example was set to version 0\.4\.0, but a higher version \d+\.\d+\.\d+\S* exists.\n", err) | ||
@test contains(err, "The package is fixed. You can try using `Pkg.free(\"Example\")` to update it.") | ||
@test contains(err, nothingtodomsg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't delete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops, will re-add the nothingtodomsg
check
|
||
@test_warn (r"INFO: Upgrading ColorTypes: v0\.2\.2 => v\d+\.\d+\.\d+", | ||
r"INFO: Upgrading Compat: v0\.7\.18 => v\d+\.\d+\.\d+", | ||
r"INFO: Upgrading Color[^s]") Pkg.update("ColorTypes") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this won't verify that Colors doesn't get upgraded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, right, I need a "negative pattern" here ... is there any way to do this with just a regex?
Otherwise, I can modify @test_warn
so that it accepts a boolean function, and then we can test whatever we want.
@tkelman, I think I've addressed all of your comments. |
Hooray, 32-bit Travis is passing again! |
@test isa(Base._require_from_serialized(myid(), Foo_module, cachefile, #=broadcast-load=#false), Array{Any,1}) | ||
finally | ||
close(STDERR) | ||
redirect_stderr(olderr) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is now misleadingly ending a much-earlier try
misread, didn't see the begin
@@ -4822,6 +4822,17 @@ end | |||
@test f14893() == 14893 | |||
@test M14893.f14893() == 14893 | |||
|
|||
# issue #18725 | |||
@test_nowarn begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't tests run in a non-Main anonymous module now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, right: the warning is getting output (correctly), but for some reason it is not captured by redirect_stderr
.
For example, if I do the following in test/core.jl
:
let fname = tempname()
try
open(fname, "w") do f
redirect_stderr(f) do
begin
f18725x(x) = 1
f18725x(x) = 2
@test f18725x(0) == 2
end
end
end
println("GOT WARNING: \"", readstring(fname), '"')
finally
rm(fname, force=true)
end
end
it outputs
WARNING: Method definition f18725x(Any) in module TestMain_core at /Users/stevenj/Documents/Code/julia/test/core.jl:4830 overwritten at /Users/stevenj/Documents/Code/julia/test/core.jl:4831.
GOT WARNING: ""
(And yet, the other @test_warn
passes, indicating that the warning is sometimes being captured by redirect_stderr
)
Any idea why the warning would not be captured here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I change the begin...end
to
eval(current_module(), :(begin
f18725x(x) = 1
f18725x(x) = 2
@test f18725x(0) == 2
end))
end
then it correctly captures the warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow, it seems that the "Method definition overwritten" error is being printed at some other stage of code evaluation, so that it occurs before the redirect_stderr
is executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed a workaround, and verified that it is not printing any warning that fails to get captured. (The issue of when the method-override warning gets printed seems unrelated to this PR.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(My workaround now evals the expression in Main
, so it correctly gets the case that is not supposed to have a warning.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm if passes CI and squashed on merge (or before if you want to split it to a couple separate commits)
Is there a way to use if isdefined(Symbol("@test_warn"))
@test_warn msg expr
else
expr
end But it seems like Julia 0.5 tries to expand the macro even if |
someone could contribute this to either BaseTestNext or Compat probably need to specify |
Okay, thanks for the reply @tkelman. I'll wait with |
This adds a
@test_warn
macro to test for the presence of warnings, and@test_nowarn
to test for the absence of warnings.I updated the precompilation tests to use this macro, rather than the
@async
method they were previously using. I also used the macro to add a test for #19888 (similar to #18746, but much cleaner given the new macro).