Skip to content
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

Merged
merged 15 commits into from
Jan 7, 2017

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Jan 6, 2017

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).

@stevengj stevengj added the testsystem The unit testing framework and Test stdlib label Jan 6, 2017
"""
@test_warn msg expr

Test whether evaluating `expr` results in `stderr` output that contains
Copy link
Contributor

@kshyatt kshyatt Jan 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stderr -> STDERR and link ?

Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here re stderr

Copy link
Contributor

@kshyatt kshyatt left a 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!

@stevengj
Copy link
Member Author

stevengj commented Jan 6, 2017

Also added a commit to use @test_warn in the Pkg tests rather than a custom output-grabbing macro. The PR is now a net deletion of code.

vers = Pkg.installed("Example")
branch = LibGit2.with(LibGit2.GitRepo, Pkg.dir("Example")) do repo
LibGit2.branch(repo)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange indent

Copy link
Member Author

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")
Copy link
Contributor

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

Copy link
Member Author

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't delete

Copy link
Member Author

@stevengj stevengj Jan 6, 2017

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't delete

Copy link
Member Author

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")
Copy link
Contributor

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

Copy link
Member Author

@stevengj stevengj Jan 6, 2017

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.

@stevengj
Copy link
Member Author

stevengj commented Jan 6, 2017

@tkelman, I think I've addressed all of your comments.

@stevengj
Copy link
Member Author

stevengj commented Jan 7, 2017

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
Copy link
Contributor

@tkelman tkelman Jan 7, 2017

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
Copy link
Contributor

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?

Copy link
Member Author

@stevengj stevengj Jan 7, 2017

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member Author

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.)

Copy link
Member Author

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.)

Copy link
Contributor

@tkelman tkelman left a 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)

@stevengj stevengj merged commit 28a11ff into JuliaLang:master Jan 7, 2017
@stevengj stevengj deleted the test_warn branch January 7, 2017 17:42
@anriseth
Copy link

anriseth commented May 9, 2017

Is there a way to use @test_warn in 0.5?
For compatibility between 0.5 and 0.6, I have tried to do:

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 isdefined(Symbol("@test_warn")) == false

@tkelman
Copy link
Contributor

tkelman commented May 9, 2017

someone could contribute this to either BaseTestNext or Compat

probably need to specify isdefined(Base.Test, Symbol("@test_warn")) instead of asking about Main.

@anriseth
Copy link

anriseth commented May 9, 2017

Okay, thanks for the reply @tkelman. I'll wait with @test_warn until the package stops supporting 0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testsystem The unit testing framework and Test stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants