From eb9bb3c42c80c2505b96be3f54b339ec01c5db09 Mon Sep 17 00:00:00 2001 From: Patrice Lecharpentier Date: Mon, 18 Dec 2023 15:08:36 +0100 Subject: [PATCH 1/2] fixing cores numbers if tests are done in Github CI. --- tests/testthat/test-tutorial.R | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test-tutorial.R b/tests/testthat/test-tutorial.R index 691b8d4..3e28a8b 100644 --- a/tests/testthat/test-tutorial.R +++ b/tests/testthat/test-tutorial.R @@ -1,23 +1,31 @@ #Download and transform Tutorial .RMD to .R tmpdir <- normalizePath(tempdir(), winslash = "/", mustWork = FALSE) -tutorial_rmd <-file.path(tmpdir,"SticsRpacks.Rmd") +tutorial_rmd <- file.path(tmpdir, "SticsRpacks.Rmd") download.file("https://raw.githubusercontent.com/SticsRPacks/SticsRPacks/main/inst/tutorials/SticsRpacks/SticsRpacks.Rmd", tutorial_rmd) -xfun::gsub_file(file=tutorial_rmd, - "eval=FALSE","eval=TRUE",fixed=TRUE) +xfun::gsub_file(file = tutorial_rmd, + "eval=FALSE","eval=TRUE", + fixed = TRUE) -xfun::gsub_file(file=tutorial_rmd, +xfun::gsub_file(file = tutorial_rmd, "optim_options = list(nb_rep = 3, out_dir = workspace_path)", - "optim_options = list(nb_rep = 3, out_dir = workspace_path, maxeval=3)",fixed=TRUE) + "optim_options = list(nb_rep = 3, out_dir = workspace_path, maxeval = 3)", + fixed = TRUE) +if (!Sys.getenv("CI") != "") { + xfun::gsub_file(file = tutorial_rmd, + "parallel = TRUE","parallel = TRUE, cores = 2", + fixed = TRUE) +} -tutorial_r <-file.path(tmpdir,"tutorial.R") -knitr::purl(input=tutorial_rmd, - output=tutorial_r, documentation = 2) +tutorial_r <-file.path(tmpdir, "tutorial.R") +knitr::purl(input = tutorial_rmd, + output = tutorial_r, + documentation = 2) -# Test Tutor=ial +# Test Tutorial test_tuto <- function() { source(tutorial_r) return (TRUE) From 6cd01a7aa24426da77f91d8dc173592d675cc0b4 Mon Sep 17 00:00:00 2001 From: Patrice Lecharpentier Date: Mon, 18 Dec 2023 16:05:07 +0100 Subject: [PATCH 2/2] fix: cores number in the context of Github CI --- tests/testthat/test-estim_param.R | 15 +++++++++++++++ tests/testthat/test-stics-wrapper.R | 19 ++++++++++++++----- tests/testthat/test-tutorial.R | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-estim_param.R b/tests/testthat/test-estim_param.R index 921a0b2..4ca3778 100644 --- a/tests/testthat/test-estim_param.R +++ b/tests/testthat/test-estim_param.R @@ -157,6 +157,14 @@ xfun::gsub_file(file=vignette_rmd, pattern="stics_version = \"V9.0\"", replacement=paste0("stics_version = \"",stics_version,"\""),fixed=TRUE) +## Fixing cores number for CI context on Github +if (Sys.getenv("CI") != "") { + xfun::gsub_file(file = vignette_rmd, + "parallel = TRUE","parallel = TRUE, cores = 2", + fixed = TRUE) +} + + ## generate the R script knitr::purl(input=vignette_rmd, output=file.path(tmpdir,"Parameter_estimation_Specific_and_Varietal.R"), documentation = 2) @@ -519,6 +527,13 @@ xfun::gsub_file(file=vignette_rmd, pattern="stics_version = \"V9.0\"", replacement=paste0("stics_version = \"",stics_version,"\""),fixed=TRUE) +## Fixing cores number for CI context on Github +if (Sys.getenv("CI") != "") { + xfun::gsub_file(file = vignette_rmd, + "parallel = TRUE, cores = 4","parallel = TRUE, cores = 2", + fixed = TRUE) +} + ## generate the R script knitr::purl(input=vignette_rmd, output=file.path(tmpdir,"AgMIP_Calibration_Phenology_protocol.R"), documentation = 2) diff --git a/tests/testthat/test-stics-wrapper.R b/tests/testthat/test-stics-wrapper.R index d730c49..e062e12 100644 --- a/tests/testthat/test-stics-wrapper.R +++ b/tests/testthat/test-stics-wrapper.R @@ -1,6 +1,15 @@ context("Stics Wrapper") stics_version <- "V10.0" + +# fixing the number of cores in the context of Github CI +if (Sys.getenv("CI") != "") { + cores <- 2 +} else { + cores <- NA +} + + # Define path to JavaStics and download data path_to_JavaStics=system.file("stics", package = "SticsRTests") javastics_path=file.path(path_to_JavaStics,stics_version) @@ -14,7 +23,7 @@ SticsRFiles::gen_usms_xml2txt(javastics = javastics_path, workspace = javastics_ out_dir = stics_inputs_path, verbose = TRUE) # Set options for Stics wrapper -model_options=SticsOnR::stics_wrapper_options(javastics=javastics_path,workspace = stics_inputs_path, parallel=FALSE) +model_options=SticsOnR::stics_wrapper_options(javastics=javastics_path,workspace = stics_inputs_path, parallel = FALSE) # Standard wrapper tests param_names=c("dlaimax","durvieF") @@ -33,7 +42,7 @@ test_that("Stics Wrapper succeed test_wrapper tests", { # Test forcing of parameter per situation works param_values <- data.frame(situation="bo96iN+",dlaimax=0.0005,durvieF=50) -model_options=SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, parallel=FALSE) +model_options=SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, parallel = FALSE) var_name="lai_n" sim_without_forcing <- SticsOnR::stics_wrapper(model_options = model_options, var="lai_n", situation = c("bo96iN+", "bou99t1")) @@ -139,13 +148,13 @@ SticsRFiles::gen_usms_xml2txt(javastics = javastics_path, workspace = javastics_ out_dir = stics_inputs_path, usm = c("demo_BareSoil2","demo_Wheat1","banana","demo_maize3"), verbose = TRUE) -model_options= SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, parallel=TRUE) +model_options= SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, parallel = TRUE, cores = cores) sim_without_successive=SticsOnR::stics_wrapper(model_options=model_options) -model_options= SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, successive = list(c("demo_Wheat1","demo_BareSoil2","demo_maize3")), parallel=TRUE) +model_options= SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, successive = list(c("demo_Wheat1","demo_BareSoil2","demo_maize3")), parallel = TRUE, cores = cores) sim_with_successive=SticsOnR::stics_wrapper(model_options=model_options) -model_options= SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, successive = list(c("demo_Wheat1","demo_BareSoil2","demo_maize3")), parallel=TRUE) +model_options= SticsOnR::stics_wrapper_options(javastics=javastics_path, workspace = stics_inputs_path, successive = list(c("demo_Wheat1","demo_BareSoil2","demo_maize3")), parallel = TRUE, cores = cores) sim_with_successive_restricted_results=SticsOnR::stics_wrapper(model_options=model_options, situation=c("banana","demo_maize3")) maize_succ_res <- file(file.path(stics_inputs_path,"demo_maize3","mod_bdemo_maize3.sti"), "rb") diff --git a/tests/testthat/test-tutorial.R b/tests/testthat/test-tutorial.R index 3e28a8b..b5bbb1a 100644 --- a/tests/testthat/test-tutorial.R +++ b/tests/testthat/test-tutorial.R @@ -13,7 +13,7 @@ xfun::gsub_file(file = tutorial_rmd, "optim_options = list(nb_rep = 3, out_dir = workspace_path, maxeval = 3)", fixed = TRUE) -if (!Sys.getenv("CI") != "") { +if (Sys.getenv("CI") != "") { xfun::gsub_file(file = tutorial_rmd, "parallel = TRUE","parallel = TRUE, cores = 2", fixed = TRUE)