@@ -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