Skip to content

Commit fa847b5

Browse files
jonthegeekjennybc
andauthored
Dots in rename (r-lib#2038)
* Check uncommitted before renaming files. Fixes r-lib#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]>
1 parent 792cdd0 commit fa847b5

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

R/rename-files.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
#' This is a potentially dangerous operation, so you must be using Git in
1313
#' order to use this function.
1414
#'
15-
#' @param old,new Old and new file names (with or without extensions).
15+
#' @param old,new Old and new file names (with or without `.R` extensions).
1616
#' @export
1717
rename_files <- function(old, new) {
1818
check_uses_git()
19+
challenge_uncommitted_changes(
20+
msg = "
21+
There are uncommitted changes and we're about to bulk-rename files. It is \\
22+
highly recommended to get into a clean Git state before bulk-editing files",
23+
untracked = TRUE
24+
)
1925

20-
old <- path_ext_remove(old)
21-
new <- path_ext_remove(new)
26+
old <- sub("\\.R$", "", old)
27+
new <- sub("\\.R$", "", new)
2228

2329
# R/ ------------------------------------------------------------------------
2430
r_old_path <- proj_path("R", old, ext = "R")

man/rename_files.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-rename-files.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
test_that("checks uncommitted files", {
2+
create_local_package()
3+
expect_error(rename_files("foo", "bar"), class = "usethis_error")
4+
5+
git_init()
6+
use_r("foo", open = FALSE)
7+
expect_error(
8+
rename_files("foo", "bar"),
9+
"uncommitted changes",
10+
class = "usethis_error"
11+
)
12+
})
13+
114
test_that("renames R and test and snapshot files", {
215
create_local_package()
16+
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
317
git_init()
418

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

1933
test_that("renames src/ files", {
2034
create_local_package()
35+
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
2136
git_init()
2237

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

3651
test_that("strips context from test file", {
3752
create_local_package()
53+
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
3854
git_init()
3955

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

5571
test_that("rename paths in test file", {
5672
create_local_package()
73+
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
5774
git_init()
5875

5976
use_testthat()
@@ -65,3 +82,13 @@ test_that("rename paths in test file", {
6582
lines <- read_utf8(proj_path("tests", "testthat", "test-bar.R"))
6683
expect_equal(lines, "test-bar.txt")
6784
})
85+
86+
test_that("does not remove non-R dots in filename", {
87+
create_local_package()
88+
local_mocked_bindings(challenge_uncommitted_changes = function(...) invisible())
89+
git_init()
90+
91+
file_create(proj_path("R/foo.bar.R"))
92+
rename_files("foo.bar", "baz.qux")
93+
expect_proj_file("R/baz.qux.R")
94+
})

0 commit comments

Comments
 (0)