Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix coupled tests #1998

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions tests/testthat/test_20-coupled.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ if (! dir.exists(magpie_folder)) magpie_folder <- paste0("../", magpie_folder)
config <- readCheckScenarioConfig(file.path("..", "..", coupledConfig), remindPath <- file.path("..", ".."))
max_iterations <- if ("max_iterations" %in% names(config)) max(config$max_iterations) else 5
# for a fresh run, delete all left-overs from previous test
del_iterations <- max(max_iterations, 9)
deleteallfiles <- NULL
deleteallfiles <- paste0("C_TESTTHAT_startlog_", seq(6), ".txt")
Rprofile <- ".Rprofile"
for (scen in rownames(config)) {
deleteallfiles <- c(deleteallfiles,
paste0("../../output/C_", scen, "-rem-", seq(del_iterations)),
paste0("../../output/C_", scen, "-", seq(del_iterations), ".pdf"),
paste0("../../output/C_", scen, "-rem-", seq(max_iterations)),
paste0("../../output/C_", scen, ".mif"),
paste0("../../C_", scen, "-rem-", seq(del_iterations), ".RData"),
paste0("../../C_", scen, "-rem-", seq(max_iterations), ".RData"),
paste0("../../output/gamscompile/main_", scen, ".gms"),
paste0("../../output/gamscompile/main_", scen, ".lst"),
file.path(magpie_folder, paste0("output/C_", scen, "-mag-", seq(del_iterations - 1))),
paste0("C_TESTTHAT_startlog_", seq(10), ".txt")
file.path(magpie_folder, paste0("output/C_", scen, "-mag-", seq(max_iterations - 1)))
)
}
expect_true(0 == unlink(deleteallfiles, recursive = TRUE))
expect_true(0 == unlink(deleteallfiles, recursive = TRUE, force = TRUE))

test_that("environment is suitable for coupled tests", {
skipIfPreviousFailed()
Expand Down Expand Up @@ -170,7 +167,7 @@ test_that("delete last REMIND run to simulate re-starting aborted run", {
paste0("../../output/C_", scen, "-", max_iterations, ".pdf"),
paste0("../../output/C_", scen, ".mif")
)
expect_true(0 == unlink(filestodelete, recursive = TRUE))
expect_true(0 == unlink(filestodelete, recursive = TRUE, force = TRUE))
output <- localSystem2("Rscript", c("start_bundle_coupled.R", coupledConfig, "startgroup=1"),
env = paste0("R_PROFILE_USER=", Rprofile))
writeLines(output, "C_TESTTHAT_startlog_2.txt")
Expand All @@ -181,7 +178,7 @@ test_that("delete last REMIND run to simulate re-starting aborted run", {

# delete the last MAgPIE, but not the last REMIND scenario and expect fail
filestodelete <- file.path(magpie_folder, "output", paste0("C_", scen, "-mag-", (max_iterations - 1)))
expect_true(0 == unlink(filestodelete, recursive = TRUE))
expect_true(0 == unlink(filestodelete, recursive = TRUE, force = TRUE))
output <- localSystem2("Rscript", c("start_bundle_coupled.R", coupledConfig, "startgroup=1"),
env = paste0("R_PROFILE_USER=", Rprofile))
writeLines(output, "C_TESTTHAT_startlog_3.txt")
Expand All @@ -195,7 +192,7 @@ test_that("delete last REMIND run to simulate re-starting aborted run", {
paste0("../../output/C_", scen, "-", max_iterations, ".pdf"),
paste0("../../output/C_", scen, ".mif")
)
expect_true(0 == unlink(filestodelete, recursive = TRUE))
expect_true(0 == unlink(filestodelete, recursive = TRUE, force = TRUE))
output <- localSystem2("Rscript", c("start_bundle_coupled.R", coupledConfig, "startgroup=1"),
env = paste0("R_PROFILE_USER=", Rprofile))
printIfFailed(output)
Expand All @@ -206,8 +203,9 @@ test_that("delete last REMIND run to simulate re-starting aborted run", {
expect_true(any(grepl(paste0("Starting MAgPIE run C_", scen, "-mag-", (max_iterations - 1)), output)))

# delete all REMIND, but not MAgPIE, and expect fail
# when adjusting this, be careful because it is used at the end to determine files that need to exist
filestodelete <- paste0("../../output/C_", scen, "-rem-", seq(max_iterations))
expect_true(0 == unlink(filestodelete, recursive = TRUE))
expect_true(0 == unlink(filestodelete, recursive = TRUE, force = TRUE))
output <- localSystem2("Rscript", c("start_bundle_coupled.R", coupledConfig, "startgroup=1"),
env = paste0("R_PROFILE_USER=", Rprofile))
writeLines(output, "C_TESTTHAT_startlog_5.txt")
Expand All @@ -218,6 +216,10 @@ test_that("delete last REMIND run to simulate re-starting aborted run", {
test_that("Check path_mif_ghgprice_land with file", {
# note: needs Base-rem-1 and -2 still present
skipIfPreviousFailed()
output <- localSystem2("Rscript", c("start_bundle_coupled.R --gamscompile", coupledConfig, "startgroup=2"),
env = paste0("R_PROFILE_USER=", Rprofile))
printIfFailed(output)
expectSuccessStatus(output)
output <- localSystem2("Rscript", c("start_bundle_coupled.R", coupledConfig, "startgroup=2"),
env = paste0("R_PROFILE_USER=", Rprofile))
writeLines(output, "C_TESTTHAT_startlog_6.txt")
Expand Down Expand Up @@ -261,17 +263,21 @@ for (csvfile in csvfiles) {
getLine = function() stop("getLine should not called."),
.package = "gms"
)
unlink("../../*TESTTHAT.RData")
unlink("../../*TESTTHAT.RData", force = TRUE)
expect_no_warning(config <- readCheckScenarioConfig(csvfile, remindPath <- file.path("..", "..")))
})
}

test_that("delete files to leave clean state", {
# leave clean state
skipIfPreviousFailed()
if (! all(file.exists(deleteallfiles))) {
# Exclude NDC folders since they were deleted earlier to see if everything fails correctly if mag iteration is bigger than rem iteration.
missingfiles <- setdiff(deleteallfiles[! file.exists(deleteallfiles)],
paste0("../../output/C_TESTTHAT-SSP2-NDC-rem-", seq(2)))
if (length(missingfiles) > 0) {
warning("files do not exist: ", paste(gsub("../.", "", missingfiles, fixed = TRUE), collapse = ", "))
skip("Not cleaning up coupled tests as not all required files exist.")
} else {
expect_true(0 == unlink(deleteallfiles, recursive = TRUE))
expect_true(0 == unlink(deleteallfiles, recursive = TRUE, force = TRUE))
}
})