From c085c26b510f5daa46071c0e299ef728035f0548 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Wed, 2 Jun 2021 13:36:33 +0100 Subject: [PATCH] Allow spaces in filenames Fixes #225 --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ R/odin_preprocess.R | 2 +- tests/testthat/test-interface.R | 15 +++++++++++++++ tests/testthat/test-preprocess.R | 19 +++++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 96faa3de..4e233ab7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: odin Title: ODE Generation and Integration -Version: 1.1.12 +Version: 1.1.13 Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"), email = "rich.fitzjohn@gmail.com"), person("Thibaut", "Jombart", role = "ctb"), diff --git a/NEWS.md b/NEWS.md index adb3839c..b1b179a5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# odin 1.1.13 + +* Allow spaces in filenames passed to `odin::odin()` (#225) + # odin 1.1.12 * New option `rewrite_constants` (via `odin::odin_options`) which attempts to rewrite all constants in the model code before generation. This can considerably reduce the number of variable lookups (mrc-2252) diff --git a/R/odin_preprocess.R b/R/odin_preprocess.R index ecf2dae9..2f6c679f 100644 --- a/R/odin_preprocess.R +++ b/R/odin_preprocess.R @@ -18,7 +18,7 @@ odin_preprocess <- function(x, type = NULL) { file <- x root <- normalizePath(dirname(x)) path <- c(root, normalizePath(getwd())) - base <- chartr("-", "_", tools::file_path_sans_ext(basename(file))) + base <- chartr("- ", "__", tools::file_path_sans_ext(basename(file))) } else { file <- NULL path <- getwd() diff --git a/tests/testthat/test-interface.R b/tests/testthat/test-interface.R index 68cb74cd..f6ad0a82 100644 --- a/tests/testthat/test-interface.R +++ b/tests/testthat/test-interface.R @@ -121,4 +121,19 @@ test_that("delay discrete models with defaults are prevented in C", { }) +test_that("Allow spaces in filenames", { + skip_on_cran() + path <- tempfile() + dir.create(path) + on.exit(unlink(path, recursive = TRUE)) + + filename <- file.path(path, "path with spaces.R") + writeLines(c("initial(x) <- 1", "deriv(x) <- 1"), filename) + + mod <- odin(filename, target = "c") + expect_equal(ir_deserialise(odin_ir(mod))$config$base, + "path_with_spaces") +}) + + unload_dlls() diff --git a/tests/testthat/test-preprocess.R b/tests/testthat/test-preprocess.R index 792321ea..c4c97f45 100644 --- a/tests/testthat/test-preprocess.R +++ b/tests/testthat/test-preprocess.R @@ -58,3 +58,22 @@ test_that("handle empty input", { ## Previously errored expect_equal(odin_preprocess_detect(character(0)), "text") }) + + +test_that("sanitise filenames", { + path <- tempfile() + dir.create(path) + on.exit(unlink(path, recursive = TRUE)) + + code <- c("initial(x) <- 1", "deriv(x) <- 1") + + path_hyphens <- file.path(path, "path-with-hyphens.R") + path_spaces <- file.path(path, "path with spaces.R") + writeLines(code, path_hyphens) + writeLines(code, path_spaces) + + expect_equal(odin_preprocess(path_hyphens)$base, + "path_with_hyphens") + expect_equal(odin_preprocess(path_spaces)$base, + "path_with_spaces") +})