Skip to content
This repository was archived by the owner on Jan 26, 2025. It is now read-only.

Commit f8ea86e

Browse files
tingerrrtingerrr
authored andcommitted
fix: don't ignore temporary directories before creation
Previously test creation would've tried to ignore the temporary directoties before the were created. Now the ignoring is simply done on creation of the directories. Additionally, ignoring now creates the directory to ignore.
1 parent c07786e commit f8ea86e

File tree

6 files changed

+51
-40
lines changed

6 files changed

+51
-40
lines changed

crates/typst-test-cli/src/cli/add.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,18 @@ pub fn run(ctx: &mut Context, args: &Args) -> eyre::Result<()> {
5050
}
5151

5252
let paths = project.paths();
53-
let vcs = project.vcs();
5453
let id = args.test.clone();
5554

5655
if let Some(template) = suite.template().filter(|_| !args.no_template) {
5756
if args.ephemeral {
5857
Test::create(
5958
paths,
60-
vcs,
6159
id,
6260
template,
6361
Some(Reference::Ephemeral(template.into())),
6462
)?;
6563
} else if args.compile_only {
66-
Test::create(paths, vcs, id, template, None)?;
64+
Test::create(paths, id, template, None)?;
6765
} else {
6866
let world = ctx.world(&args.compile)?;
6967

@@ -78,10 +76,10 @@ pub fn run(ctx: &mut Context, args: &Args) -> eyre::Result<()> {
7876
);
7977
let doc = output?;
8078

81-
Test::create(paths, vcs, id, template, Some(Reference::Persistent(doc)))?;
79+
Test::create(paths, id, template, Some(Reference::Persistent(doc)))?;
8280
};
8381
} else {
84-
Test::create_default(paths, vcs, id)?;
82+
Test::create_default(paths, id)?;
8583
}
8684

8785
let mut w = ctx.ui.stderr();

crates/typst-test-cli/src/cli/init.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub fn run(ctx: &mut Context, args: &Args) -> eyre::Result<()> {
3737
if !args.no_example {
3838
Test::create_default(
3939
project.paths(),
40-
project.vcs(),
4140
Id::new("example").expect("the id is valid and unique"),
4241
)?;
4342
}

crates/typst-test-cli/src/cli/util/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn run(ctx: &mut Context) -> eyre::Result<()> {
1414
let len = suite.matched().len();
1515

1616
for test in suite.matched().values() {
17-
test.create_temporary_directories(project.paths())?;
17+
test.create_temporary_directories(project.paths(), project.vcs())?;
1818
}
1919

2020
let mut w = ctx.ui.stderr();

crates/typst-test-cli/src/runner.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl TestRunner<'_, '_, '_> {
144144
// TODO(tinger): don't exit early if there are still exports possible
145145

146146
let paths = self.project_runner.project.paths();
147+
let vcs = self.project_runner.project.vcs();
147148

148149
match self.project_runner.config.action {
149150
Action::Run {
@@ -213,7 +214,7 @@ impl TestRunner<'_, '_, '_> {
213214
let output = self.compile_out_doc(output)?;
214215
let output = self.render_out_doc(output)?;
215216

216-
self.test.create_reference_documents(paths, &output)?;
217+
self.test.create_reference_documents(paths, vcs, &output)?;
217218

218219
if export {
219220
let reference = self.load_ref_doc()?;
@@ -249,8 +250,10 @@ impl TestRunner<'_, '_, '_> {
249250
pub fn prepare(&mut self) -> eyre::Result<()> {
250251
tracing::trace!(test = ?self.test.id(), "clearing temporary directories");
251252

252-
self.test
253-
.create_temporary_directories(self.project_runner.project.paths())?;
253+
self.test.create_temporary_directories(
254+
self.project_runner.project.paths(),
255+
self.project_runner.project.vcs(),
256+
)?;
254257

255258
Ok(())
256259
}

crates/typst-test-lib/src/project/vcs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt::{self, Debug, Display};
77
use std::path::{Path, PathBuf};
88
use std::{fs, io};
99

10+
use crate::stdx;
1011
use crate::stdx::result::ResultEx;
1112

1213
/// The name of the git ignore file.
@@ -85,10 +86,12 @@ impl Vcs {
8586
match self.kind {
8687
Kind::Git => {
8788
let gitignore = path.join(GITIGNORE_NAME);
89+
stdx::fs::create_dir(path, true)?;
8890
fs::write(gitignore, GITIGNORE_CONTENT)?;
8991
}
9092
Kind::Mercurial => {
9193
let hgignore = path.join(HGIGNORE_NAME);
94+
stdx::fs::create_dir(path, true)?;
9295
fs::write(hgignore, HGIGNORE_CONTENT)?;
9396
}
9497
}

crates/typst-test-lib/src/test/mod.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,9 @@ impl Test {
179179

180180
impl Test {
181181
/// Creates a new default test on disk.
182-
pub fn create_default(paths: &Paths, vcs: Option<&Vcs>, id: Id) -> Result<Test, CreateError> {
182+
pub fn create_default(paths: &Paths, id: Id) -> Result<Test, CreateError> {
183183
Self::create(
184184
paths,
185-
vcs,
186185
id,
187186
DEFAULT_TEST_INPUT,
188187
Some(Reference::Persistent(Document::new([Pixmap::decode_png(
@@ -195,7 +194,6 @@ impl Test {
195194
/// Creates a new test on disk.
196195
pub fn create(
197196
paths: &Paths,
198-
vcs: Option<&Vcs>,
199197
id: Id,
200198
source: &str,
201199
reference: Option<Reference>,
@@ -236,20 +234,16 @@ impl Test {
236234
this.create_reference_script(paths, reference.as_str())?;
237235
}
238236
Some(Reference::Persistent(reference)) => {
239-
this.create_reference_documents(paths, &reference)?;
237+
this.create_reference_documents(paths, None, &reference)?;
240238
}
241239
None => {}
242240
}
243241

244-
if let Some(vcs) = vcs {
245-
this.ignore_temporary_directories(paths, vcs)?;
246-
}
247-
248242
Ok(this)
249243
}
250244

251245
/// Creates this test's temporary directories, if they don't exist yet.
252-
pub fn create_temporary_directories(&self, paths: &Paths) -> io::Result<()> {
246+
pub fn create_temporary_directories(&self, paths: &Paths, vcs: Option<&Vcs>) -> io::Result<()> {
253247
self.delete_temporary_directories(paths)?;
254248

255249
if self.kind.is_ephemeral() {
@@ -258,6 +252,11 @@ impl Test {
258252

259253
stdx::fs::create_dir(paths.test_out_dir(&self.id), true)?;
260254
stdx::fs::create_dir(paths.test_diff_dir(&self.id), true)?;
255+
256+
if let Some(vcs) = vcs {
257+
self.ignore_temporary_directories(paths, vcs)?;
258+
}
259+
261260
Ok(())
262261
}
263262

@@ -279,6 +278,7 @@ impl Test {
279278
pub fn create_reference_documents(
280279
&self,
281280
paths: &Paths,
281+
vcs: Option<&Vcs>,
282282
reference: &Document,
283283
) -> Result<(), SaveError> {
284284
// NOTE(tinger): if there are already more pages than we want to create,
@@ -289,6 +289,13 @@ impl Test {
289289
let ref_dir = paths.test_ref_dir(&self.id);
290290
stdx::fs::create_dir(&ref_dir, true)?;
291291
reference.save(&ref_dir)?;
292+
293+
if self.kind().is_ephemeral() {
294+
if let Some(vcs) = vcs {
295+
self.ignore_reference_documents(paths, vcs)?;
296+
}
297+
}
298+
292299
Ok(())
293300
}
294301

@@ -381,7 +388,7 @@ impl Test {
381388
reference: &Document,
382389
) -> Result<(), SaveError> {
383390
self.delete_reference_script(paths)?;
384-
self.create_reference_documents(paths, reference)?;
391+
self.create_reference_documents(paths, vcs, reference)?;
385392
if let Some(vcs) = vcs {
386393
self.unignore_reference_documents(paths, vcs)?;
387394
}
@@ -422,23 +429,22 @@ impl Test {
422429
/// Loads the reference test script source of this test, if this test is
423430
/// ephemeral.
424431
pub fn load_reference_source(&self, paths: &Paths) -> io::Result<Option<Source>> {
425-
match self.kind {
426-
Kind::Ephemeral => {
427-
let ref_script = paths.test_ref_script(&self.id);
428-
Ok(Some(Source::new(
429-
FileId::new(
430-
None,
431-
VirtualPath::new(
432-
ref_script
433-
.strip_prefix(paths.project_root())
434-
.unwrap_or(&ref_script),
435-
),
436-
),
437-
std::fs::read_to_string(ref_script)?,
438-
)))
439-
}
440-
_ => Ok(None),
432+
if !self.kind().is_ephemeral() {
433+
return Ok(None);
441434
}
435+
436+
let ref_script = paths.test_ref_script(&self.id);
437+
Ok(Some(Source::new(
438+
FileId::new(
439+
None,
440+
VirtualPath::new(
441+
ref_script
442+
.strip_prefix(paths.project_root())
443+
.unwrap_or(&ref_script),
444+
),
445+
),
446+
std::fs::read_to_string(ref_script)?,
447+
)))
442448
}
443449

444450
/// Loads the persistent reference pages of this test, if they exist.
@@ -501,16 +507,15 @@ mod tests {
501507
}
502508

503509
#[test]
504-
fn test_create_new() {
510+
fn test_create() {
505511
_dev::fs::TempEnv::run(
506512
|root| root.setup_dir("tests"),
507513
|root| {
508514
let paths = Paths::new(root, None);
509-
Test::create(&paths, None, id("compile-only"), "Hello World", None).unwrap();
515+
Test::create(&paths, id("compile-only"), "Hello World", None).unwrap();
510516

511517
Test::create(
512518
&paths,
513-
None,
514519
id("ephemeral"),
515520
"Hello World",
516521
Some(Reference::Ephemeral("Hello\nWorld".into())),
@@ -519,18 +524,21 @@ mod tests {
519524

520525
Test::create(
521526
&paths,
522-
None,
523527
id("persistent"),
524528
"Hello World",
525529
Some(Reference::Persistent(Document::new(vec![]))),
526530
)
527531
.unwrap();
532+
533+
Test::create_default(&paths, id("default")).unwrap();
528534
},
529535
|root| {
530536
root.expect_file_content("tests/compile-only/test.typ", "Hello World")
531537
.expect_file_content("tests/ephemeral/test.typ", "Hello World")
532538
.expect_file_content("tests/ephemeral/ref.typ", "Hello\nWorld")
533539
.expect_file_content("tests/persistent/test.typ", "Hello World")
540+
.expect_file_content("tests/default/test.typ", DEFAULT_TEST_INPUT)
541+
.expect_file("tests/default/ref/1.png")
534542
.expect_dir("tests/persistent/ref")
535543
},
536544
);

0 commit comments

Comments
 (0)