Skip to content

Commit 8899bea

Browse files
authored
refactor: ♻️ change use_targets_template() to use dir as input (#193)
# Description Following this comment: #181 (comment) Closes #190 Needs a quick review. ## Checklist - [X] Ran `just run-all`
1 parent 197a34f commit 8899bea

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

R/use-targets.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
#' Use a targets pipeline template for converting SAS registers to Parquet
22
#'
3-
#' Copies a template to the given path.
3+
#' Copies a `_targets.R` template to the given directory.
44
#'
5-
#' @param path Path to the file to create. Defaults to `_targets.R`.
5+
#' @param path Path to the directory where `_targets.R` will be created.
6+
#' Defaults to the current directory.
67
#' @param open Whether to open the file for editing.
78
#'
8-
#' @returns The `path`, invisibly.
9+
#' @returns The path to the created file, invisibly.
910
#'
1011
#' @export
1112
use_targets_template <- function(
12-
path = "_targets.R",
13+
path = ".",
1314
open = rlang::is_interactive()
1415
) {
15-
if (fs::path_file(path) != "_targets.R") {
16-
cli::cli_abort("File name must be {.file _targets.R}.")
17-
}
16+
checkmate::assert_directory_exists(path)
17+
target_file <- fs::path(path, "_targets.R")
1818

1919
template_path <- fs::path_package("fastreg", "template-targets.R")
2020

21-
if (fs::file_exists(path)) {
21+
if (fs::file_exists(target_file)) {
2222
cli::cli_abort(c(
23-
"{.file {path}} already exists.",
24-
"i" = "Delete it first or choose a different path."
23+
"{.file {target_file}} already exists.",
24+
"i" = "Delete it first or choose a different directory."
2525
))
2626
}
2727

28-
fs::file_copy(path = template_path, new_path = path)
28+
fs::file_copy(path = template_path, new_path = target_file)
2929

30-
if (fs::file_exists(path)) {
31-
cli::cli_alert_success("Created {.file {path}}")
30+
if (fs::file_exists(target_file)) {
31+
cli::cli_alert_success("Created {.file {target_file}}")
3232
cli::cli_alert_info("Edit the {.code config} section to set your paths.")
3333
}
3434

3535
if (open) {
36-
utils::file.edit(path)
36+
utils::file.edit(target_file)
3737
}
3838

39-
invisible(path)
39+
invisible(target_file)
4040
}

man/use_targets_template.Rd

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-use-targets.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Setup ------------------------------------------------------------------------
2-
output_path <- fs::path_temp("_targets.R")
3-
use_targets_template(output_path, open = FALSE)
2+
output_path <- fs::path_temp()
3+
file_path <- use_targets_template(output_path, open = FALSE)
44
template_path <- fs::path_package("fastreg", "template-targets.R")
55
template_content <- readLines(template_path)
66

@@ -16,7 +16,7 @@ test_that("use_targets_template() errors when file already exists", {
1616

1717
test_that("use_targets_template() creates file matching template content", {
1818
expect_equal(
19-
readLines(output_path),
19+
readLines(file_path),
2020
readLines(template_path)
2121
)
2222
})
@@ -25,10 +25,10 @@ test_that("use_targets_template() creates valid R code", {
2525
expect_no_error(parse(file = template_path))
2626
})
2727

28-
test_that("use_targets_template() errors with incorrect `path`", {
28+
test_that("use_targets_template() errors with non-existing `path`", {
2929
expect_error(
30-
use_targets_template(fs::path_temp("not-targets.R")),
31-
regexp = "_targets.R"
30+
use_targets_template(fs::path_temp("non-existing/dir")),
31+
regexp = "does not exist"
3232
)
3333
})
3434

vignettes/fastreg.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ but that can be adjusted in the pipeline to fit the available resources.
192192
pipeline_dir <- fs::path_temp("pipeline-dir")
193193
fs::dir_create(pipeline_dir)
194194
195-
use_targets_template(path = fs::path(pipeline_dir, "_targets.R"))
195+
use_targets_template(path = pipeline_dir)
196196
```
197197

198198
### Set input and output paths

0 commit comments

Comments
 (0)