Skip to content

Move the check on misuse of data.frame() to tests #3758

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

Conversation

yutannihilation
Copy link
Member

@yutannihilation yutannihilation commented Jan 23, 2020

#3526 introduced the static analysis of codes here.

get_n_stop <- function(f) {
d <- getParseData(parse(f, keep.source = TRUE))
sum(d$token == "SYMBOL_FUNCTION_CALL" & d$text == "stop")
}
get_n_warning <- function(f) {
d <- getParseData(parse(f, keep.source = TRUE))
sum(d$token == "SYMBOL_FUNCTION_CALL" & d$text == "warning")
}
test_that("do not use stop()", {
stops <- vapply(list.files("../../R", full.names = TRUE), get_n_stop, integer(1))
expect_equal(sum(stops), 0)
})
test_that("do not use warning()", {
warnings <- vapply(list.files("../../R", full.names = TRUE), get_n_warning, integer(1))
expect_equal(sum(warnings), 0)
})

This approach seems very clean and extensive because this catches the misuse of the prohibited function even if it's on the rare code path. I think the check on data.frame() should be done in the same way instead of overwriting the function. One bonus of this is that we'll be less likely to face such mysterious issues as #3745.

@clauswilke
Copy link
Member

I like this approach, but I'd like to suggest that we should also test the test (i.e., include a true positive). Add a file to the testthat directory that contains the prohibited functions and check that the counts of prohibited functions for that file are correct.

@yutannihilation
Copy link
Member Author

Thanks, I added some simple tests.

@clauswilke clauswilke added this to the ggplot2 3.4.0 milestone Feb 7, 2020
@thomasp85
Copy link
Member

@yutannihilation can we merge this in in its current form?

@yutannihilation
Copy link
Member Author

Yes, since this is just about testing, I think it's safe to merge this.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

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

LGTM

@yutannihilation
Copy link
Member Author

Thanks

@yutannihilation yutannihilation merged commit 241b7c5 into tidyverse:master Mar 26, 2021
@yutannihilation yutannihilation deleted the refactor/move-data.frame-warning-to-test branch March 26, 2021 15:06
sthagen added a commit to sthagen/tidyverse-ggplot2 that referenced this pull request Mar 27, 2021
Move the check on misuse of data.frame() to tests (tidyverse#3758)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants