11test_that(" aborts when path is not a package root" , {
2- tmp <- tempfile(" not_pkg" )
3- dir.create(tmp )
4- on.exit(unlink(tmp , recursive = TRUE ))
2+ tmp <- withr :: local_tempdir()
53
64 expect_error(
75 use_zzz(path = tmp ),
@@ -10,9 +8,12 @@ test_that("aborts when path is not a package root", {
108})
119
1210test_that(" aborts when zzz.R already exists and overwrite = FALSE" , {
13- tmp <- tempfile(" pkg" )
14- dir.create(tmp )
15- on.exit(unlink(tmp , recursive = TRUE ))
11+ tmp <- withr :: local_tempdir()
12+ local_mocked_bindings(
13+ proj_set = function (... ) NULL ,
14+ use_package = function (... ) NULL ,
15+ .package = " usethis"
16+ )
1617
1718 writeLines(
1819 c(
@@ -33,9 +34,12 @@ test_that("aborts when zzz.R already exists and overwrite = FALSE", {
3334})
3435
3536test_that(" creates zzz.R with PKG replaced in template" , {
36- tmp <- tempfile(" pkg" )
37- dir.create(tmp )
38- on.exit(unlink(tmp , recursive = TRUE ))
37+ tmp <- withr :: local_tempdir()
38+ local_mocked_bindings(
39+ proj_set = function (... ) NULL ,
40+ use_package = function (... ) NULL ,
41+ .package = " usethis"
42+ )
3943
4044 writeLines(
4145 c(
@@ -54,15 +58,18 @@ test_that("creates zzz.R with PKG replaced in template", {
5458 expect_true(file.exists(expected_path ))
5559
5660 lines <- readLines(expected_path , warn = FALSE )
57- expect_match(lines [1L ], " #' @title My Test Package" , fixed = TRUE )
58- expect_match(lines [9L ], " ^#' @name testpkg-package$" , perl = TRUE )
59- expect_match(lines [10L ], " ^#' @aliases testpkg$" , perl = TRUE )
61+ expect_match(lines [3L ], " #' @title My Test Package" , fixed = TRUE )
62+ expect_match(lines [11L ], " ^#' @name testpkg-package$" , perl = TRUE )
63+ expect_match(lines [12L ], " ^#' @aliases testpkg$" , perl = TRUE )
6064})
6165
6266test_that(" creates zzz.R with correct TITLE replacement" , {
63- tmp <- tempfile(" pkg" )
64- dir.create(tmp )
65- on.exit(unlink(tmp , recursive = TRUE ))
67+ tmp <- withr :: local_tempdir()
68+ local_mocked_bindings(
69+ proj_set = function (... ) NULL ,
70+ use_package = function (... ) NULL ,
71+ .package = " usethis"
72+ )
6673
6774 writeLines(
6875 c(
@@ -77,13 +84,16 @@ test_that("creates zzz.R with correct TITLE replacement", {
7784 use_zzz(path = tmp )
7885
7986 lines <- readLines(file.path(tmp , " R" , " testpkg-package.R" ), warn = FALSE )
80- expect_match(lines [1L ], " #' @title Custom Title Here" , fixed = TRUE )
87+ expect_match(lines [3L ], " #' @title Custom Title Here" , fixed = TRUE )
8188})
8289
8390test_that(" creates zzz.R with DESCRIPTION replacement (including multiline)" , {
84- tmp <- tempfile(" pkg" )
85- dir.create(tmp )
86- on.exit(unlink(tmp , recursive = TRUE ))
91+ tmp <- withr :: local_tempdir()
92+ local_mocked_bindings(
93+ proj_set = function (... ) NULL ,
94+ use_package = function (... ) NULL ,
95+ .package = " usethis"
96+ )
8797
8898 writeLines(
8999 c(
@@ -99,20 +109,21 @@ test_that("creates zzz.R with DESCRIPTION replacement (including multiline)", {
99109 use_zzz(path = tmp )
100110
101111 lines <- readLines(file.path(tmp , " R" , " testpkg-package.R" ), warn = FALSE )
102- # Description spans lines 3-4: "#' @description A package for testing."
103- # then "#' It has multiple lines."
104112 expect_match(
105- lines [3L ],
113+ lines [5L ],
106114 " #' @description A package for testing." ,
107115 fixed = TRUE
108116 )
109- expect_match(lines [4L ], " #' It has multiple lines." , fixed = TRUE )
117+ expect_match(lines [6L ], " #' It has multiple lines." , fixed = TRUE )
110118})
111119
112120test_that(" creates zzz.R with correct LICENSE replacement" , {
113- tmp <- tempfile(" pkg" )
114- dir.create(tmp )
115- on.exit(unlink(tmp , recursive = TRUE ))
121+ tmp <- withr :: local_tempdir()
122+ local_mocked_bindings(
123+ proj_set = function (... ) NULL ,
124+ use_package = function (... ) NULL ,
125+ .package = " usethis"
126+ )
116127
117128 writeLines(
118129 c(
@@ -127,13 +138,16 @@ test_that("creates zzz.R with correct LICENSE replacement", {
127138 use_zzz(path = tmp )
128139
129140 lines <- readLines(file.path(tmp , " R" , " testpkg-package.R" ), warn = FALSE )
130- expect_match(lines [6L ], " ^#' GPL-3$" , perl = TRUE )
141+ expect_match(lines [8L ], " ^#' GPL-3$" , perl = TRUE )
131142})
132143
133144test_that(" overwrite = TRUE replaces existing zzz.R file" , {
134- tmp <- tempfile(" pkg" )
135- dir.create(tmp )
136- on.exit(unlink(tmp , recursive = TRUE ))
145+ tmp <- withr :: local_tempdir()
146+ local_mocked_bindings(
147+ proj_set = function (... ) NULL ,
148+ use_package = function (... ) NULL ,
149+ .package = " usethis"
150+ )
137151
138152 writeLines(
139153 c(
@@ -151,13 +165,16 @@ test_that("overwrite = TRUE replaces existing zzz.R file", {
151165 result <- use_zzz(path = tmp , overwrite = TRUE )
152166
153167 lines <- readLines(result , warn = FALSE )
154- expect_match(lines [1L ], " #' @title My Package" , fixed = TRUE )
168+ expect_match(lines [3L ], " #' @title My Package" , fixed = TRUE )
155169})
156170
157171test_that(" custom file_name is respected" , {
158- tmp <- tempfile(" pkg" )
159- dir.create(tmp )
160- on.exit(unlink(tmp , recursive = TRUE ))
172+ tmp <- withr :: local_tempdir()
173+ local_mocked_bindings(
174+ proj_set = function (... ) NULL ,
175+ use_package = function (... ) NULL ,
176+ .package = " usethis"
177+ )
161178
162179 writeLines(
163180 c(
@@ -177,9 +194,12 @@ test_that("custom file_name is respected", {
177194})
178195
179196test_that(" returns invisible file path" , {
180- tmp <- tempfile(" pkg" )
181- dir.create(tmp )
182- on.exit(unlink(tmp , recursive = TRUE ))
197+ tmp <- withr :: local_tempdir()
198+ local_mocked_bindings(
199+ proj_set = function (... ) NULL ,
200+ use_package = function (... ) NULL ,
201+ .package = " usethis"
202+ )
183203
184204 writeLines(
185205 c(
@@ -199,9 +219,12 @@ test_that("returns invisible file path", {
199219})
200220
201221test_that(" creates R/ directory if missing" , {
202- tmp <- tempfile(" pkg" )
203- dir.create(tmp )
204- on.exit(unlink(tmp , recursive = TRUE ))
222+ tmp <- withr :: local_tempdir()
223+ local_mocked_bindings(
224+ proj_set = function (... ) NULL ,
225+ use_package = function (... ) NULL ,
226+ .package = " usethis"
227+ )
205228
206229 writeLines(
207230 c(
0 commit comments