Skip to content

Commit 1fd9f81

Browse files
committed
test: add more tests about automatic fix
1 parent 41898ca commit 1fd9f81

File tree

2 files changed

+114
-9
lines changed

2 files changed

+114
-9
lines changed

R/find_missing_ns.R

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ is_ns <- function(text) {
55

66
#' @noRd
77
is_shiny_input_output_funmodule <- function(
8-
text,
9-
extend_input_output_funmodule = NA_character_) {
8+
text,
9+
extend_input_output_funmodule = NA_character_) {
1010
stopifnot(is.character(extend_input_output_funmodule))
1111

1212
input_output_knew <- c("Input|Output|actionButton|radioButtons")
@@ -55,16 +55,15 @@ fix_ns_in_data <- function(data) {
5555
substr(line_to_modify, col_end + 1, nchar(line_to_modify))
5656
)
5757
file_content[line_index] <- modified_line
58+
writeLines(file_content, file)
5859
}
59-
60-
writeLines(file_content, file)
6160
}
6261

6362
#' @noRd
6463
#' @importFrom utils getParseData
6564
check_namespace_in_file <- function(
66-
path,
67-
extend_input_output_funmodule = NA_character_) {
65+
path,
66+
extend_input_output_funmodule = NA_character_) {
6867
getParseData(
6968
parse(
7069
file = path,
@@ -116,9 +115,9 @@ check_namespace_in_file <- function(
116115
#'
117116
#' @export
118117
check_namespace_sanity <- function(
119-
pkg = golem::get_golem_wd(),
120-
extend_input_output_funmodule = NA_character_,
121-
auto_fix = TRUE) {
118+
pkg = golem::get_golem_wd(),
119+
extend_input_output_funmodule = NA_character_,
120+
auto_fix = TRUE) {
122121
check_desc_installed()
123122
check_cli_installed()
124123

tests/testthat/test-find_missing_ns.R

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,70 @@ test_that("Check check_namespace_in_file", {
271271
)
272272
})
273273

274+
test_that("Check fix_ns_in_data works", {
275+
path <- tempfile(fileext = ".R")
276+
277+
writeLines(
278+
c(
279+
"mod_test_module_ui <- function(id) {",
280+
" ns <- NS(id)",
281+
" tagList(",
282+
" selectInput(",
283+
" inputId = 'selectinput',", # Missing NS
284+
" label = 'Select input',",
285+
" choices = c(LETTERS[1:10])",
286+
" ),",
287+
" actionButton(",
288+
" inputId = 'actionbutton',", # Missing NS
289+
" label = 'Action button'",
290+
" )",
291+
" )",
292+
"}",
293+
"",
294+
"mod_test_module_server <- function(id) {",
295+
" observeEvent(input$actionbutton, {",
296+
" message(input$actionbutton)",
297+
" message(input$selectinput)",
298+
" })",
299+
"}"
300+
),
301+
con = path
302+
)
303+
304+
data <- check_namespace_in_file(
305+
path = path
306+
)
307+
308+
fix_ns_in_data(data)
309+
310+
expect_equal(
311+
readLines(path),
312+
c(
313+
"mod_test_module_ui <- function(id) {",
314+
" ns <- NS(id)",
315+
" tagList(",
316+
" selectInput(",
317+
" inputId = ns('selectinput'),", # NS present
318+
" label = 'Select input',",
319+
" choices = c(LETTERS[1:10])",
320+
" ),",
321+
" actionButton(",
322+
" inputId = ns('actionbutton'),", # NS present
323+
" label = 'Action button'",
324+
" )",
325+
" )",
326+
"}",
327+
"",
328+
"mod_test_module_server <- function(id) {",
329+
" observeEvent(input$actionbutton, {",
330+
" message(input$actionbutton)",
331+
" message(input$selectinput)",
332+
" })",
333+
"}"
334+
)
335+
)
336+
})
337+
274338
dummy_dir_check_ns <- tempfile(pattern = "dummy")
275339
dir.create(dummy_dir_check_ns)
276340

@@ -403,5 +467,47 @@ withr::with_dir(dummy_dir_check_ns, {
403467
),
404468
"NS check passed"
405469
)
470+
471+
expect_equal(
472+
readLines(file.path(dummy_golem_path, "R", "mod_test_module.R")),
473+
c(
474+
"#' first UI Function",
475+
"#'",
476+
"#' @description A shiny Module.",
477+
"#'",
478+
"#' @param id,input,output,session Internal parameters for {shiny}.",
479+
"#'",
480+
"#' @shinyModule A Golem module.",
481+
"#'",
482+
"#' @importFrom shiny NS tagList",
483+
"mod_test_module_ui <- function(id) {",
484+
" ns <- NS(id)",
485+
" tagList(",
486+
" selectInput(",
487+
" inputId = ns('selectinput'),",
488+
" label = 'Select input',",
489+
" choices = c(LETTERS[1:10])",
490+
" ),",
491+
" actionButton(",
492+
" inputId = ns('actionbutton'),",
493+
" label = 'Action button'",
494+
" )",
495+
" )",
496+
"}",
497+
"",
498+
"#' first Server Functions",
499+
"#'",
500+
"mod_test_module_server <- function(id) {",
501+
" moduleServer(id, function(input, output, session) {",
502+
" ns <- session$ns",
503+
"",
504+
" observeEvent(input$actionbutton, {",
505+
" message(input$actionbutton)",
506+
" message(input$selectinput)",
507+
" })",
508+
" })",
509+
"}"
510+
)
511+
)
406512
})
407513
})

0 commit comments

Comments
 (0)