Skip to content

Commit

Permalink
Dots in rename (#2038)
Browse files Browse the repository at this point in the history
* Check uncommitted before renaming files.

Fixes #1969.

After digging into this a little, I think making sure everything is committed is enough. use_r() won't create a file with a `.` in it, so, unless you want to let this snowball, I think the check is enough.

* Only remove `.R` extensions from old/new.

* Don't use shorthand for function definition.

* Use older anonymous function syntax in more places

---------

Co-authored-by: Jenny Bryan <[email protected]>
  • Loading branch information
jonthegeek and jennybc authored Aug 15, 2024
1 parent 792cdd0 commit fa847b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
12 changes: 9 additions & 3 deletions R/rename-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
#' This is a potentially dangerous operation, so you must be using Git in
#' order to use this function.
#'
#' @param old,new Old and new file names (with or without extensions).
#' @param old,new Old and new file names (with or without `.R` extensions).
#' @export
rename_files <- function(old, new) {
check_uses_git()
challenge_uncommitted_changes(
msg = "
There are uncommitted changes and we're about to bulk-rename files. It is \\
highly recommended to get into a clean Git state before bulk-editing files",
untracked = TRUE
)

old <- path_ext_remove(old)
new <- path_ext_remove(new)
old <- sub("\\.R$", "", old)
new <- sub("\\.R$", "", new)

# R/ ------------------------------------------------------------------------
r_old_path <- proj_path("R", old, ext = "R")
Expand Down
2 changes: 1 addition & 1 deletion man/rename_files.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/testthat/test-rename-files.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
test_that("checks uncommitted files", {
create_local_package()
expect_error(rename_files("foo", "bar"), class = "usethis_error")

git_init()
use_r("foo", open = FALSE)
expect_error(
rename_files("foo", "bar"),
"uncommitted changes",
class = "usethis_error"
)
})

test_that("renames R and test and snapshot files", {
create_local_package()
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
git_init()

use_r("foo", open = FALSE)
Expand All @@ -18,6 +32,7 @@ test_that("renames R and test and snapshot files", {

test_that("renames src/ files", {
create_local_package()
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
git_init()

use_src()
Expand All @@ -35,6 +50,7 @@ test_that("renames src/ files", {

test_that("strips context from test file", {
create_local_package()
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
git_init()

use_testthat()
Expand All @@ -54,6 +70,7 @@ test_that("strips context from test file", {

test_that("rename paths in test file", {
create_local_package()
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
git_init()

use_testthat()
Expand All @@ -65,3 +82,13 @@ test_that("rename paths in test file", {
lines <- read_utf8(proj_path("tests", "testthat", "test-bar.R"))
expect_equal(lines, "test-bar.txt")
})

test_that("does not remove non-R dots in filename", {
create_local_package()
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
git_init()

file_create(proj_path("R/foo.bar.R"))
rename_files("foo.bar", "baz.qux")
expect_proj_file("R/baz.qux.R")
})

0 comments on commit fa847b5

Please sign in to comment.