foch 0.0.2

EU4 mod analysis and merge toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Syntax fixes of mod script files outside a merge.
//!
//! A merge only repairs Foch's own copy of a source file. `foch check --fix`
//! writes the same repairs out, like a linter's fixes: into a mod directory
//! an author works on, or, for a playset or a Workshop mod, as a patch mod
//! that holds only the repaired files and loads after the mods it repairs,
//! or on explicit request into the source files themselves. A file fixed in
//! place is always backed up first, so the fix can be undone.
//!
//! Safe fixes are the repairs a merge makes by itself, and the reviewed
//! `[[repairs]]` of `foch.toml`. Unsafe fixes, only on request, settle what a
//! merge holds for review: an isolated statement is left out, as `--force`
//! reads it, and a definition left out whole takes its likeliest proposal.

use crate::game::eu4::base::snapshot::data_root;
use crate::game::eu4::script::parse_cache::parse_clausewitz_for_path;
use crate::game::eu4::script::parser::repair_text_edits;
use crate::game::eu4::text::{TextEdit, decode_paradox_bytes, reencode_paradox_bytes};
use crate::input::request::InputRequest;
use crate::merge::transform::input::{repair_text, reviewed_definition_lines};
use crate::model::{GamePath, GamePathBuf};
use crate::project::{Project, render_repairs_toml};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::BTreeMap;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

/// One fix of a parse diagnostic, as an edit to the text it was parsed
/// from.
#[derive(Clone, Debug)]
pub struct TextFix {
	pub code: crate::game::eu4::script::parser::ParseDiagnosticCode,
	/// Where the diagnostic is, as byte offsets in the text.
	pub start: usize,
	pub end: usize,
	pub title: String,
	/// A safe fix is a repair the merge makes by itself; an unsafe one
	/// settles what the merge holds for review.
	pub safe: bool,
	pub edit: TextEdit,
}

/// The fixes of `diagnostics`, parsed from `text`: a repair for each
/// repaired diagnostic, and the unsafe fix of each isolated definition.
pub fn text_fixes(
	text: &str,
	diagnostics: &[crate::game::eu4::script::parser::ParseDiagnostic],
) -> Vec<TextFix> {
	let mut fixes = Vec::new();
	for diagnostic in diagnostics {
		let fix = if let Some(repair) = diagnostic.repair {
			crate::game::eu4::script::parser::repair_text_edit(
				text,
				repair.edit,
				diagnostic.span.start.offset,
			)
			.map(|edit| (edit, repair.description(), true))
		} else if let Some(isolation) = &diagnostic.isolation {
			unsafe_fix(text, isolation).map(|(edit, title)| (edit, title, false))
		} else {
			None
		};
		if let Some((edit, title, safe)) = fix {
			fixes.push(TextFix {
				code: diagnostic.code,
				start: diagnostic.span.start.offset,
				end: diagnostic.span.end.offset,
				title,
				safe,
				edit,
			});
		}
	}
	fixes
}

/// Which fixes a plan makes.
#[derive(Clone, Copy, Debug, Default)]
pub struct FixOptions {
	/// Also settle what a merge holds for review.
	pub unsafe_fixes: bool,
}

/// Every file of the input's mods that has something to repair.
#[derive(Clone, Debug, Default)]
pub struct RepairPlan {
	/// Files whose repaired bytes are known, in mod and path order.
	pub files: Vec<FileRepair>,
	/// Files with errors no repair covers, and why.
	pub unrepaired: Vec<UnrepairedFile>,
}

#[derive(Clone, Debug)]
pub struct FileRepair {
	pub mod_id: String,
	pub path: GamePathBuf,
	/// The source file the repair is for.
	pub source: PathBuf,
	/// What changes, one line each.
	pub changes: Vec<String>,
	pub sha256_before: String,
	pub sha256_after: String,
	pub original: Vec<u8>,
	pub repaired: Vec<u8>,
}

#[derive(Clone, Debug)]
pub struct UnrepairedFile {
	pub mod_id: String,
	pub path: GamePathBuf,
	pub reasons: Vec<String>,
}

#[derive(Debug)]
pub struct RepairError(String);

impl fmt::Display for RepairError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.write_str(&self.0)
	}
}

impl std::error::Error for RepairError {}

impl From<std::io::Error> for RepairError {
	fn from(error: std::io::Error) -> Self {
		Self(error.to_string())
	}
}

fn sha256(bytes: &[u8]) -> String {
	format!("{:x}", Sha256::digest(bytes))
}

/// Finds what can be repaired in the mods `request` resolves to, with the
/// reviewed repairs of `project`. Nothing is written.
pub fn plan_repairs(
	request: &InputRequest,
	project: &Project,
	options: FixOptions,
) -> Result<RepairPlan, RepairError> {
	let input = crate::input::resolve_input(request, false)
		.map_err(|error| RepairError(error.to_string()))?;
	let mut targets = BTreeMap::<(String, GamePathBuf), PathBuf>::new();
	for (candidate, snapshot) in input.mods.iter().zip(&input.mod_snapshots) {
		let (Some(root), Some(snapshot)) = (&candidate.root_path, snapshot) else {
			continue;
		};
		for issue in &snapshot.semantic_index.parse_issues {
			targets.insert(
				(candidate.mod_id.clone(), issue.path.clone()),
				issue.path.to_path(root),
			);
		}
		for entry in project
			.repairs
			.iter()
			.filter(|entry| entry.mod_id == candidate.mod_id)
		{
			if let Ok(path) = GamePathBuf::parse(&entry.file) {
				let source = path.to_path(root);
				targets.insert((candidate.mod_id.clone(), path), source);
			}
		}
	}
	plan_targets(project, targets, options)
}

/// Finds what can be repaired in the script files of the mod directory
/// `root`, an author's own mod. Nothing is written.
pub fn plan_directory_repairs(
	root: &Path,
	project: &Project,
	options: FixOptions,
) -> Result<RepairPlan, RepairError> {
	let mod_id = root
		.file_name()
		.and_then(|name| name.to_str())
		.unwrap_or("mod")
		.to_owned();
	let mut targets = BTreeMap::new();
	for entry in walkdir::WalkDir::new(root).sort_by_file_name() {
		let entry = entry.map_err(|error| RepairError(error.to_string()))?;
		if !entry.file_type().is_file() {
			continue;
		}
		let Ok(path) = GamePathBuf::from_physical(root, entry.path()) else {
			continue;
		};
		let script = crate::game::eu4::script::documents::classify_document_family(&path)
			== Some(crate::model::DocumentFamily::Clausewitz)
			&& path
				.extension()
				.is_some_and(|extension| !extension.eq_ignore_ascii_case("lua"));
		if script {
			targets.insert((mod_id.clone(), path), entry.path().to_owned());
		}
	}
	plan_targets(project, targets, options)
}

fn plan_targets(
	project: &Project,
	targets: BTreeMap<(String, GamePathBuf), PathBuf>,
	options: FixOptions,
) -> Result<RepairPlan, RepairError> {
	let mut plan = RepairPlan::default();
	for ((mod_id, path), source) in targets {
		let (repair, reasons) = match plan_file(project, &mod_id, &path, &source, options)? {
			Planned::Repair(repair, reasons) => (Some(repair), reasons),
			Planned::Unrepaired(reasons) => (None, reasons),
			Planned::Nothing => (None, Vec::new()),
		};
		plan.files.extend(repair);
		if !reasons.is_empty() {
			plan.unrepaired.push(UnrepairedFile {
				mod_id,
				path,
				reasons,
			});
		}
	}
	Ok(plan)
}

enum Planned {
	/// Fixes to write, and the errors they leave, as a linter fixes what it
	/// can and reports the rest.
	Repair(FileRepair, Vec<String>),
	Unrepaired(Vec<String>),
	Nothing,
}

fn plan_file(
	project: &Project,
	mod_id: &str,
	path: &GamePath,
	source: &Path,
	options: FixOptions,
) -> Result<Planned, RepairError> {
	let bytes = fs::read(source)?;
	let sha256_before = sha256(&bytes);
	let mut changes = Vec::new();
	let mut text = decode_paradox_bytes(&bytes).into_owned();
	let reviewed = project
		.repairs
		.iter()
		.find(|entry| entry.mod_id == mod_id && entry.file == path.as_str());
	if let Some(entry) = reviewed {
		if entry.sha256 != sha256_before {
			return Ok(Planned::Unrepaired(vec![format!(
				"the reviewed repair in foch.toml is stale: it was reviewed for SHA256 {}, the file now has {sha256_before}",
				entry.sha256
			)]));
		}
		text = repair_text(&text, &entry.edits).map_err(RepairError)?;
		changes.push(format!(
			"reviewed repair from foch.toml ({} edit(s))",
			entry.edits.len()
		));
	}
	let parsed = parse_clausewitz_for_path(path, &text);
	if let Some(entry) = reviewed {
		let touched = reviewed_definition_lines(&parsed.ast.statements, &entry.edits);
		if parsed.diagnostics.iter().any(|diagnostic| {
			touched
				.iter()
				.any(|lines| lines.contains(&diagnostic.span.start.line))
		}) {
			return Ok(Planned::Unrepaired(vec![
				"the reviewed repair in foch.toml leaves an error in the definitions it changes"
					.into(),
			]));
		}
	}
	let mut reasons = Vec::new();
	let mut unsafe_edits = Vec::new();
	for diagnostic in &parsed.diagnostics {
		if let Some(isolation) = &diagnostic.isolation {
			if options.unsafe_fixes
				&& let Some((edit, change)) = unsafe_fix(&text, isolation)
			{
				unsafe_edits.push(edit);
				changes.push(format!(
					"{}:{} [{}] {change} (unsafe: definition `{}` could not be read with confidence)",
					diagnostic.span.start.line,
					diagnostic.span.start.column,
					diagnostic.code.name(),
					isolation.definition
				));
				continue;
			}
			let proposals = isolation
				.proposals
				.iter()
				.map(|proposal| proposal.description())
				.collect::<Vec<_>>();
			let entry = isolation.proposals.first().and_then(|proposal| {
				crate::project::proposed_repair_entry(mod_id, path, &bytes, *proposal)
			});
			reasons.push(format!(
				"definition `{}` at {}-{} has no trustworthy repair{}{}",
				isolation.definition,
				diagnostic.span.start.line,
				isolation.end_line,
				if proposals.is_empty() {
					String::new()
				} else {
					format!("; repairs that could be meant: {}", proposals.join(", "))
				},
				entry.map_or_else(String::new, |entry| format!(
					"; to apply the first after review, add to foch.toml:\n{}",
					render_repairs_toml(&[entry])
				))
			));
		} else if diagnostic.repair.is_none() {
			reasons.push(format!(
				"{}:{} [{}] {}",
				diagnostic.span.start.line,
				diagnostic.span.start.column,
				diagnostic.code.name(),
				diagnostic.message
			));
		}
	}
	let mut edits: Vec<TextEdit> = repair_text_edits(&text, &parsed.diagnostics)
		.ok_or_else(|| RepairError(format!("{path}: a repair is not where the text has it")))?;
	edits.extend(unsafe_edits);
	for diagnostic in &parsed.diagnostics {
		if let Some(repair) = diagnostic.repair {
			changes.push(format!(
				"{}:{} [{}] {}",
				diagnostic.span.start.line,
				diagnostic.span.start.column,
				diagnostic.code.name(),
				repair.description()
			));
		}
	}
	if changes.is_empty() {
		return Ok(if reasons.is_empty() {
			Planned::Nothing
		} else {
			Planned::Unrepaired(reasons)
		});
	}
	let mut ordered = edits;
	ordered.sort_by_key(|edit| std::cmp::Reverse(edit.offset));
	for edit in ordered {
		text.replace_range(edit.offset..edit.offset + edit.remove, edit.insert);
	}
	// What is written may keep the errors no fix covers, but no other.
	let remaining = parse_clausewitz_for_path(path, &text).diagnostics;
	if remaining.len() > reasons.len()
		|| remaining
			.iter()
			.any(|diagnostic| diagnostic.repair.is_some())
	{
		return Ok(Planned::Unrepaired(
			remaining
				.iter()
				.map(|diagnostic| {
					format!(
						"after the fixes, {}:{}: {}",
						diagnostic.span.start.line,
						diagnostic.span.start.column,
						diagnostic.message
					)
				})
				.collect(),
		));
	}
	let Some(repaired) = reencode_paradox_bytes(&bytes, &text) else {
		return Ok(Planned::Unrepaired(vec![
			"its repairs cannot be written back in its encoding".into(),
		]));
	};
	Ok(Planned::Repair(
		FileRepair {
			mod_id: mod_id.to_owned(),
			path: path.to_owned(),
			source: source.to_owned(),
			changes,
			sha256_before,
			sha256_after: sha256(&repaired),
			original: bytes,
			repaired,
		},
		reasons,
	))
}

/// The unsafe fix of an isolated definition in `text`: its unreadable
/// statement's lines are left out, as a forced merge reads it, or, left out
/// whole, it takes its likeliest proposal. `None` when there is neither.
fn unsafe_fix(text: &str, isolation: &crate::model::Isolation) -> Option<(TextEdit, String)> {
	if let Some(lines) = isolation.dropped {
		let start = line_offset(text, lines.first)?;
		let end = line_offset(text, lines.last + 1).unwrap_or(text.len());
		return Some((
			TextEdit {
				offset: start,
				remove: end - start,
				insert: "",
			},
			format!("left out lines {}-{}", lines.first, lines.last),
		));
	}
	let proposal = isolation.proposals.first()?;
	let edit =
		crate::game::eu4::script::parser::repair_text_edit(text, proposal.edit, proposal.offset)?;
	Some((edit, proposal.description()))
}

/// Where line `line`, counted from 1, starts in `text`.
fn line_offset(text: &str, line: usize) -> Option<usize> {
	if line <= 1 {
		return Some(0);
	}
	text.match_indices('\n').nth(line - 2).map(|(at, _)| at + 1)
}

/// Whether `dir` lies in a Steam Workshop content folder, where Steam keeps
/// other people's mods and replaces their files on update.
pub fn is_workshop_item(dir: &Path) -> bool {
	// A relative path such as `.` names its folder only once resolved.
	let resolved = fs::canonicalize(dir).unwrap_or_else(|_| dir.to_path_buf());
	let names: Vec<String> = resolved
		.components()
		.map(|component| component.as_os_str().to_string_lossy().to_ascii_lowercase())
		.collect();
	names
		.windows(2)
		.any(|pair| pair[0] == "workshop" && pair[1] == "content")
}

/// Checks that no source changed since `plan` read it.
fn verify_unchanged(plan: &RepairPlan) -> Result<(), RepairError> {
	for file in &plan.files {
		if sha256(&fs::read(&file.source)?) != file.sha256_before {
			return Err(RepairError(format!(
				"{} changed since the repairs were planned; plan them again",
				file.source.display()
			)));
		}
	}
	Ok(())
}

/// Writes a patch mod into `out_dir`, which must not exist or be empty: a
/// `descriptor.mod` named `name` and every repaired file at its game path.
/// Loaded after the mods it repairs, it replaces their broken files.
pub fn write_patch_mod(plan: &RepairPlan, out_dir: &Path, name: &str) -> Result<(), RepairError> {
	if out_dir.exists() && fs::read_dir(out_dir)?.next().is_some() {
		return Err(RepairError(format!(
			"{} is not empty; write the patch mod into a new or empty directory",
			out_dir.display()
		)));
	}
	verify_unchanged(plan)?;
	let mut paths = BTreeMap::new();
	for file in &plan.files {
		if let Some(other) = paths.insert(&file.path, &file.mod_id) {
			return Err(RepairError(format!(
				"{} is repaired in both {other} and {}; one patch mod can hold only one",
				file.path, file.mod_id
			)));
		}
	}
	fs::create_dir_all(out_dir)?;
	for file in &plan.files {
		let target = file.path.to_path(out_dir);
		if let Some(parent) = target.parent() {
			fs::create_dir_all(parent)?;
		}
		fs::write(target, &file.repaired)?;
	}
	fs::write(
		out_dir.join("descriptor.mod"),
		format!(
			"name=\"{}\"\ntags={{\n\t\"Fixes\"\n}}\n",
			name.replace('"', "'")
		),
	)?;
	Ok(())
}

/// One file `repair_in_place` changed, as its backup manifest records it.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BackedUpFile {
	pub mod_id: String,
	pub path: GamePathBuf,
	pub source: PathBuf,
	/// The original bytes, relative to the backup directory.
	pub backup: PathBuf,
	pub sha256_before: String,
	pub sha256_after: String,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct BackupManifest {
	pub files: Vec<BackedUpFile>,
}

const BACKUP_MANIFEST: &str = "manifest.json";
const BACKUP_FILES: &str = "files";

/// The directory backups of in-place repairs are kept under.
pub fn backup_root() -> PathBuf {
	data_root().join("repair-backups")
}

/// Writes the repairs into the source files themselves. Each original is
/// copied first into a new directory under [`backup_root`], whose manifest
/// [`restore_backup`] reads; that directory is returned.
pub fn repair_in_place(plan: &RepairPlan) -> Result<PathBuf, RepairError> {
	repair_in_place_under(plan, &backup_root())
}

fn repair_in_place_under(plan: &RepairPlan, root: &Path) -> Result<PathBuf, RepairError> {
	verify_unchanged(plan)?;
	let stamp = std::time::SystemTime::now()
		.duration_since(std::time::UNIX_EPOCH)
		.map_or(0, |elapsed| elapsed.as_millis());
	let backup_dir = root.join(stamp.to_string());
	if backup_dir.exists() {
		return Err(RepairError(format!(
			"{} already exists; try again",
			backup_dir.display()
		)));
	}
	let mut manifest = BackupManifest::default();
	// A backup is named by its position, never by the mod id or game path,
	// which a playset or `foch.toml` sets and could lead out of the backup.
	fs::create_dir_all(backup_dir.join(BACKUP_FILES))?;
	for (index, file) in plan.files.iter().enumerate() {
		let backup = Path::new(BACKUP_FILES).join(index.to_string());
		fs::copy(&file.source, backup_dir.join(&backup))?;
		manifest.files.push(BackedUpFile {
			mod_id: file.mod_id.clone(),
			path: file.path.clone(),
			source: file.source.clone(),
			backup,
			sha256_before: file.sha256_before.clone(),
			sha256_after: file.sha256_after.clone(),
		});
	}
	fs::create_dir_all(&backup_dir)?;
	fs::write(
		backup_dir.join(BACKUP_MANIFEST),
		serde_json::to_vec_pretty(&manifest).map_err(|error| RepairError(error.to_string()))?,
	)?;
	for file in &plan.files {
		fs::write(&file.source, &file.repaired)?;
	}
	Ok(backup_dir)
}

/// What `restore_backup` did with each file of a backup.
#[derive(Clone, Debug, Default)]
pub struct RestoreSummary {
	pub restored: Vec<PathBuf>,
	/// Files that no longer hold the repaired bytes, left as they are: an
	/// update or another edit replaced them after the repair.
	pub skipped: Vec<PathBuf>,
}

/// Puts back the originals an in-place repair backed up into `backup_dir`,
/// for each file that still holds the bytes the repair wrote.
pub fn restore_backup(backup_dir: &Path) -> Result<RestoreSummary, RepairError> {
	let manifest: BackupManifest =
		serde_json::from_slice(&fs::read(backup_dir.join(BACKUP_MANIFEST))?)
			.map_err(|error| RepairError(format!("{}: {error}", backup_dir.display())))?;
	let mut summary = RestoreSummary::default();
	for file in manifest.files {
		if !file
			.backup
			.components()
			.all(|component| matches!(component, std::path::Component::Normal(_)))
		{
			return Err(RepairError(format!(
				"the backup of {} is not inside {}",
				file.source.display(),
				backup_dir.display()
			)));
		}
		let current = fs::read(&file.source).ok().map(|bytes| sha256(&bytes));
		if current.as_deref() != Some(file.sha256_after.as_str()) {
			summary.skipped.push(file.source);
			continue;
		}
		let original = fs::read(backup_dir.join(&file.backup))?;
		if sha256(&original) != file.sha256_before {
			return Err(RepairError(format!(
				"the backup of {} is damaged",
				file.source.display()
			)));
		}
		fs::write(&file.source, original)?;
		summary.restored.push(file.source);
	}
	Ok(summary)
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn a_backup_stays_inside_its_directory_whatever_the_mod_id() {
		let temp = tempfile::tempdir().unwrap();
		let source = temp.path().join("mods").join("a.txt");
		fs::create_dir_all(source.parent().unwrap()).unwrap();
		fs::write(&source, "a = {\n}\n}\n").unwrap();
		let original = fs::read(&source).unwrap();
		let repaired = b"a = {\n}\n\n".to_vec();
		let plan = RepairPlan {
			files: vec![FileRepair {
				mod_id: "../../escaped".into(),
				path: GamePath::new("common/a.txt").unwrap().to_owned(),
				source: source.clone(),
				changes: Vec::new(),
				sha256_before: sha256(&original),
				sha256_after: sha256(&repaired),
				original,
				repaired,
			}],
			unrepaired: Vec::new(),
		};
		let root = temp.path().join("backups");
		let backup_dir = repair_in_place_under(&plan, &root).unwrap();
		assert_eq!(
			fs::read_dir(temp.path()).unwrap().count(),
			2,
			"only the mods and the backups"
		);
		let manifest: BackupManifest =
			serde_json::from_slice(&fs::read(backup_dir.join(BACKUP_MANIFEST)).unwrap()).unwrap();
		assert_eq!(manifest.files[0].backup, Path::new("files").join("0"));

		// A manifest naming a file outside the backup is refused.
		let mut tampered = manifest.clone();
		tampered.files[0].backup = PathBuf::from("../../mods/a.txt");
		fs::write(
			backup_dir.join(BACKUP_MANIFEST),
			serde_json::to_vec(&tampered).unwrap(),
		)
		.unwrap();
		assert!(restore_backup(&backup_dir).is_err());
		fs::write(
			backup_dir.join(BACKUP_MANIFEST),
			serde_json::to_vec(&manifest).unwrap(),
		)
		.unwrap();
		let summary = restore_backup(&backup_dir).unwrap();
		assert_eq!(summary.restored, vec![source.clone()]);
		assert_eq!(fs::read(&source).unwrap(), b"a = {\n}\n}\n");
	}
}