Skip to content

Refactor get_active_file() #1730

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

Merged
merged 5 commits into from
Jan 18, 2023
Merged

Refactor get_active_file() #1730

merged 5 commits into from
Jan 18, 2023

Conversation

hadley
Copy link
Member

@hadley hadley commented Jan 17, 2023

  • Make it much broader so it now works with any file in R/, src/ or tests/testthat. Fixes Teach use_test()/test_active_file() about src #1566
  • This new foundation made it possible to make use_c() and use_rcpp() (and friends) to work the same way as use_r()/use_test()`.
  • Since I was in here I fixed use_test() creates wrong file if period in file name #1690.
  • I also refactored the code to make it easier to test.
  • I switched the errors I touch to use cli::cli_abort() so the messaging can point to the correct user facing function.
  • I generally refactored the error handling so it happens in one central location.

* Make it much broader so it now works with any file in `R/`, `src/` or `tests/testthat`. Fixes #1566
* This new foundation made it possible to make `use_c()` and `use_rcpp()` (and friends) to work the same way as `use_r()`/use_test()`.
* Since I was in here I fixed #1690.
* I also refactored the code to make it easier to test.
* I switched the errors I touch to use `cli::cli_abort()` so the messaging can point to the correct user facing function.
* I generally refactored the error handling so it happens in one central location.
@hadley hadley force-pushed the refactor-active-file branch from 990cbad to 047cb88 Compare January 17, 2023 23:23
#' @inheritParams edit_file
#' @seealso The [testing](https://r-pkgs.org/tests.html) and
#' [R code](https://r-pkgs.org/r.html) chapters of
#' [R Packages](https://r-pkgs.org).
#' @export
use_r <- function(name = NULL, open = rlang::is_interactive()) {
check_not_empty_file_name(name)
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 inlined check_not_empty_filename() into check_file_name() and moved that into compute_name().


test_path <- proj_path("tests", "testthat", paste0("test-", name, ".R"))
if (!file_exists(test_path)) {
ui_todo("Call {ui_code('use_test()')} to create a matching test file")
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 removed this because no other helper includes similar text, but I can restore it if you think it's important.

#'
#' @details
#'
#' When using compiled code, please note that there must be at least one file
Copy link
Member Author

Choose a reason for hiding this comment

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

This weird wrinkle (implemented in use_src_example_script()) is no longer needed because we always create a file.

@hadley hadley requested a review from jennybc January 17, 2023 23:28
Copy link
Member

@jennybc jennybc left a comment

Choose a reason for hiding this comment

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

I didn't do any manual testing, but I assume you have.

R/r.R Outdated
valid_file_name <- function(x) {
grepl("^[a-zA-Z0-9._-]+$", x)
}
rel_path <- path_dir(proj_rel_path(path))
Copy link
Member

Choose a reason for hiding this comment

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

rel_path feels like a confusing name for the immediate parent directory within the project, but I guess it's not terribly important.

@hadley hadley merged commit 3c03f5c into main Jan 18, 2023
@hadley hadley deleted the refactor-active-file branch January 18, 2023 15:50
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.

use_test() creates wrong file if period in file name Teach use_test()/test_active_file() about src
2 participants