Skip to content

Commit 7b38b52

Browse files
only rewrite files in mplusModel if they have changed
1 parent 2e128fe commit 7b38b52

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

R/mplusModel.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,8 @@ mplusModel_r6 <- R6::R6Class(
423423
# check whether the input file has changed
424424
write_inp <- TRUE
425425
if (file.exists(self$inp_file)) {
426-
new_md5 <- digest::digest(self$syntax, algo = "md5", serialize = FALSE)
427-
ext_md5 <- digest::digest(readLines(self$inp_file), algo = "md5", serialize = FALSE)
426+
new_md5 <- digest::digest(self$syntax, algo = "md5", serialize = TRUE)
427+
ext_md5 <- digest::digest(readLines(self$inp_file), algo = "md5", serialize = TRUE)
428428
if (isTRUE(new_md5 == ext_md5)) write_inp <- FALSE
429429
}
430430
if (write_inp) self$write_inp(quiet = TRUE)

man/mplusModel_r6.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-mplusModel.R

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,82 @@ test_that("mplusModel exposes readModels sections", {
6565
expect_true(length(m$errors) == 0L)
6666
})
6767

68+
69+
test_that("mplusModel only rewrites input and data when changed", {
70+
syn <- "
71+
TITLE: this is an example of a simple linear
72+
regression for a continuous observed
73+
dependent variable with two covariates
74+
DATA: FILE IS ex3.1.dat;
75+
VARIABLE: NAMES ARE y1 x1 x3;
76+
MODEL: y1 ON x1 x3;
77+
"
78+
dat <- as.data.frame(data.table::fread(
79+
testthat::test_path("submitModels", "ex3.1.dat"),
80+
data.table = FALSE
81+
))
82+
names(dat) <- c("y1", "x1", "x3")
83+
tmp <- tempfile()
84+
dir.create(tmp)
85+
mplus_fake <- tempfile()
86+
file.create(mplus_fake)
87+
m <- mplusModel(
88+
syntax = syn,
89+
data = dat,
90+
inp_file = file.path(tmp, "ex3.1.inp"),
91+
Mplus_command = mplus_fake
92+
)
93+
94+
95+
fake_runModels <- function(target, ...) {
96+
file.create(sub("\\.inp$", ".out", target))
97+
invisible(NULL)
98+
}
99+
fake_readModels <- function(...) {
100+
list()
101+
}
102+
103+
run_stub <- function() {
104+
testthat::with_mocked_bindings(
105+
m$run(replaceOutfile = "always"),
106+
runModels = fake_runModels,
107+
readModels = fake_readModels
108+
)
109+
}
110+
111+
# run things the first time, get modified times
112+
run_stub()
113+
mtime_inp1 <- file.info(m$inp_file)$mtime
114+
mtime_dat1 <- file.info(m$dat_file)$mtime
115+
116+
# run again with no changes -- should not write files again
117+
Sys.sleep(1)
118+
run_stub()
119+
mtime_inp2 <- file.info(m$inp_file)$mtime
120+
mtime_dat2 <- file.info(m$dat_file)$mtime
121+
122+
expect_equal(mtime_inp2, mtime_inp1)
123+
expect_equal(mtime_dat2, mtime_dat1)
124+
125+
# run again with new syntax -- inp changes, dat does not
126+
Sys.sleep(1)
127+
m$syntax <- c(m$syntax, "! new comment")
128+
run_stub()
129+
mtime_inp3 <- file.info(m$inp_file)$mtime
130+
mtime_dat3 <- file.info(m$dat_file)$mtime
131+
132+
expect_gt(mtime_inp3, mtime_inp2)
133+
expect_equal(mtime_dat3, mtime_dat2)
134+
135+
# run again with new data -- inp stays the same, dat changes
136+
Sys.sleep(1)
137+
dat2 <- m$data
138+
dat2$y1[1] <- dat2$y1[1] + 1
139+
m$data <- dat2
140+
run_stub()
141+
mtime_inp4 <- file.info(m$inp_file)$mtime
142+
mtime_dat4 <- file.info(m$dat_file)$mtime
143+
144+
expect_equal(mtime_inp4, mtime_inp3)
145+
expect_gt(mtime_dat4, mtime_dat3)
146+
})

0 commit comments

Comments
 (0)