reifydb-testing 0.4.10

Testing utilities and helpers for ReifyDB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

// This file includes and modifies code from the toydb project (https://github.com/erikgrinaker/toydb),
// originally licensed under the Apache License, Version 2.0.
// Original copyright:
//   Copyright (c) 2024 Erik Grinaker
//
// The original Apache License can be found at:
//   http://www.apache.org/licenses/LICENSE-2.0

use std::{env::temp_dir, error::Error, fs, io, io::Write as _, panic, path, process, time};

use fs::read_to_string;
use io::ErrorKind;
use panic::AssertUnwindSafe;
use path::Path;
use time::SystemTime;

use crate::{
	goldenfile::Mint,
	testscript::{command::Command, parser::parse},
};

/// Runs testscript commands, returning their output.
pub trait Runner {
	/// Runs a testscript command, returning its output, or an error if the
	/// command fails.
	///
	/// Arguments can be accessed directly via [`Command::args`], or by
	/// using the [`Command::consume_args`] helper for more convenient
	/// processing.
	///
	/// Error cases are typically tested by running the command with a `!`
	/// prefix (expecting a failure), but the runner can also handle these
	/// itself and return an `Ok` result with appropriate output.
	fn run(&mut self, command: &Command) -> Result<String, Box<dyn Error>>;

	/// Called at the start of a testscript. Used e.g. for initial setup.
	/// Can't return output, since it's not called in the context of a
	/// block.
	fn start_script(&mut self) -> Result<(), Box<dyn Error>> {
		Ok(())
	}

	/// Called at the end of a testscript. Used e.g. for state assertions.
	/// Can't return output, since it's not called in the context of a
	/// block.
	fn end_script(&mut self) -> Result<(), Box<dyn Error>> {
		Ok(())
	}

	/// Called at the start of a block. Used e.g. to output initial state.
	/// Any output is prepended to the block's output.
	fn start_block(&mut self) -> Result<String, Box<dyn Error>> {
		Ok(String::new())
	}

	/// Called at the end of a block. Used e.g. to output final state.
	/// Any output is appended to the block's output.
	fn end_block(&mut self) -> Result<String, Box<dyn Error>> {
		Ok(String::new())
	}

	/// Called at the start of a command. Used e.g. for setup. Any output is
	/// prepended to the command's output, and is affected e.g. by the
	/// prefix and silencing of the command.
	#[allow(unused_variables)]
	fn start_command(&mut self, command: &Command) -> Result<String, Box<dyn Error>> {
		Ok(String::new())
	}

	/// Called at the end of a command. Used e.g. for cleanup. Any output is
	/// appended to the command's output, and is affected e.g. by the prefix
	/// and silencing of the command.
	#[allow(unused_variables)]
	fn end_command(&mut self, command: &Command) -> Result<String, Box<dyn Error>> {
		Ok(String::new())
	}
}

/// Runs a testscript at the given path.
///
/// Panics if the script output differs from the current input file. Errors on
/// IO, parser, or runner failure. If the environment variable
/// `UPDATE_TESTFILES=1` is set, the new output file will replace the input
/// file.
pub fn run_path<R: Runner, P: AsRef<Path>>(runner: &mut R, path: P) -> io::Result<()> {
	let path = path.as_ref();
	let Some(dir) = path.parent() else {
		return Err(io::Error::new(ErrorKind::InvalidInput, format!("invalid path '{path:?}'")));
	};
	let Some(filename) = path.file_name() else {
		return Err(io::Error::new(ErrorKind::InvalidInput, format!("invalid path '{path:?}'")));
	};

	if filename.to_str().unwrap().ends_with(".skip") {
		return Ok(());
	}

	let input = read_to_string(dir.join(filename))?;
	let output = generate(runner, &input)?;

	Mint::new(dir).new_goldenfile(filename)?.write_all(output.as_bytes())
}

pub fn run<R: Runner, S: Into<String>>(runner: R, test: S) {
	try_run(runner, test).unwrap();
}

pub fn try_run<R: Runner, S: Into<String>>(mut runner: R, test: S) -> io::Result<()> {
	let input = test.into();

	let dir = temp_dir();
	#[allow(clippy::disallowed_methods)]
	let file_name = format!(
		"test-{}-{}.txt",
		process::id(),
		SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap().as_nanos()
	);
	let file_path = dir.join(&file_name);

	let mut file = fs::File::create(&file_path)?;
	file.write_all(input.as_bytes())?;

	let output = generate(&mut runner, &input)?;
	Mint::new(dir).new_goldenfile(&file_name)?.write_all(output.as_bytes())
}

/// Generates output for a testscript input, without comparing them.
pub fn generate<R: Runner>(runner: &mut R, input: &str) -> io::Result<String> {
	let mut output = String::with_capacity(input.len()); // common case: output == input

	// Detect end-of-line format.
	let eol = match input.find("\r\n") {
		Some(_) => "\r\n",
		None => "\n",
	};

	// Parse the script.
	let blocks = parse(input).map_err(|e| {
		io::Error::new(
			ErrorKind::InvalidInput,
			format!(
				"parse error at line {} column {} for {:?}:\n{}\n{}^",
				e.input.location_line(),
				e.input.get_column(),
				e.code,
				String::from_utf8_lossy(e.input.get_line_beginning()),
				' '.to_string().repeat(e.input.get_utf8_column() - 1)
			),
		)
	})?;

	// Call the start_script() hook.
	runner.start_script().map_err(|e| io::Error::other(format!("start_script failed: {e}")))?;

	for (i, block) in blocks.iter().enumerate() {
		// There may be a trailing block with no commands if the script
		// has bare comments at the end. If so, just retain its
		// literal contents.
		if block.commands.is_empty() {
			output.push_str(&block.literal);
			continue;
		}

		// Process each block of commands and accumulate their output.
		let mut block_output = String::new();

		// Call the start_block() hook.
		block_output.push_str(&ensure_eol(
			runner.start_block().map_err(|e| {
				io::Error::other(format!("start_block failed at line {}: {e}", block.line_number))
			})?,
			eol,
		));

		for command in &block.commands {
			let mut command_output = String::new();

			// Call the start_command() hook.
			command_output.push_str(&ensure_eol(
				runner.start_command(command).map_err(|e| {
					io::Error::other(format!(
						"start_command failed at line {}: {e}",
						command.line_number
					))
				})?,
				eol,
			));

			// Execute the command. Handle panics and errors if
			// requested. We assume the command is unwind-safe
			// when handling panics, it is up to callers to
			// manage this appropriately.
			let run = AssertUnwindSafe(|| runner.run(command));
			command_output.push_str(&match panic::catch_unwind(run) {
				// Unexpected success, error out.
				Ok(Ok(output)) if command.fail => {
					return Err(io::Error::other(format!(
						"expected command '{}' to fail at line {}, succeeded with: {output}",
						command.name, command.line_number
					)));
				}

				// Expected success, output the result.
				Ok(Ok(output)) => output,

				// Expected error, output it.
				Ok(Err(e)) if command.fail => {
					format!("{e}")
				}

				// Unexpected error, return it.
				Ok(Err(e)) => {
					return Err(io::Error::other(format!(
						"command '{}' failed at line {}: {e}",
						command.name, command.line_number
					)));
				}

				// Expected panic, output it.
				Err(panic) if command.fail => {
					let message = panic
						.downcast_ref::<&str>()
						.map(|s| s.to_string())
						.or_else(|| panic.downcast_ref::<String>().cloned())
						.unwrap_or_else(|| panic::resume_unwind(panic));
					format!("Panic: {message}")
				}

				// Unexpected panic, throw it.
				Err(panic) => panic::resume_unwind(panic),
			});

			// Make sure the command output has a trailing newline,
			// unless empty.
			command_output = ensure_eol(command_output, eol);

			// Call the end_command() hook.
			command_output.push_str(&ensure_eol(
				runner.end_command(command).map_err(|e| {
					io::Error::other(format!(
						"end_command failed at line {}: {e}",
						command.line_number
					))
				})?,
				eol,
			));

			// Silence the output if requested.
			if command.silent {
				command_output = "".to_string();
			}

			// Prefix output lines if requested.
			if let Some(prefix) = &command.prefix
				&& !command_output.is_empty()
			{
				command_output = format!(
					"{prefix}: {}{eol}",
					command_output
						.strip_suffix(eol)
						.unwrap_or(command_output.as_str())
						.replace('\n', &format!("\n{prefix}: "))
				);
			}

			block_output.push_str(&command_output);
		}

		// Call the end_block() hook.
		block_output.push_str(&ensure_eol(
			runner.end_block().map_err(|e| {
				io::Error::other(format!("end_block failed at line {}: {e}", block.line_number))
			})?,
			eol,
		));

		// If the block doesn't have any output, default to "ok".
		if block_output.is_empty() {
			block_output.push_str("ok\n")
		}

		// If the block output contains blank lines, use a > prefix for
		// it.
		//
		// We'd be better off using regular expressions here, but don't
		// want to add a dependency just for this.
		if block_output.starts_with('\n')
			|| block_output.starts_with("\r\n")
			|| block_output.contains("\n\n")
			|| block_output.contains("\n\r\n")
		{
			block_output = format!("> {}", block_output.replace('\n', "\n> "));
			// Remove trailing space from blank lines ("> \n" -> ">\n")
			block_output = block_output.replace("> \n", ">\n");
			// We guarantee above that block output ends with a
			// newline, so we remove the "> " at the end of the
			// output.
			block_output.pop();
			block_output.pop();
		}

		// Add the resulting block to the output. If this is not the
		// last block, also add a newline separator.
		output.push_str(&format!("{}---{eol}{}", block.literal, block_output));
		if i < blocks.len() - 1 {
			output.push_str(eol);
		}
	}

	// Call the end_script() hook.
	runner.end_script().map_err(|e| io::Error::other(format!("end_script failed: {e}")))?;

	Ok(output)
}

/// Appends a newline if the string is not empty and doesn't already have one.
fn ensure_eol(mut s: String, eol: &str) -> String {
	if let Some(c) = s.chars().next_back()
		&& c != '\n'
	{
		s.push_str(eol)
	}
	s
}

// NB: most tests are done as testscripts under tests/.
#[cfg(test)]
pub mod tests {
	use super::*;

	/// A runner which simply counts the number of times its hooks are
	/// called.
	#[derive(Default)]
	struct HookRunner {
		start_script_count: usize,
		end_script_count: usize,
		start_block_count: usize,
		end_block_count: usize,
		start_command_count: usize,
		end_command_count: usize,
	}

	impl Runner for HookRunner {
		fn run(&mut self, _: &Command) -> Result<String, Box<dyn Error>> {
			Ok(String::new())
		}

		fn start_script(&mut self) -> Result<(), Box<dyn Error>> {
			self.start_script_count += 1;
			Ok(())
		}

		fn end_script(&mut self) -> Result<(), Box<dyn Error>> {
			self.end_script_count += 1;
			Ok(())
		}

		fn start_block(&mut self) -> Result<String, Box<dyn Error>> {
			self.start_block_count += 1;
			Ok(String::new())
		}

		fn end_block(&mut self) -> Result<String, Box<dyn Error>> {
			self.end_block_count += 1;
			Ok(String::new())
		}

		fn start_command(&mut self, _: &Command) -> Result<String, Box<dyn Error>> {
			self.start_command_count += 1;
			Ok(String::new())
		}

		fn end_command(&mut self, _: &Command) -> Result<String, Box<dyn Error>> {
			self.end_command_count += 1;
			Ok(String::new())
		}
	}

	/// Tests that runner hooks are called as expected.
	#[test]
	fn hooks() {
		let mut runner = HookRunner::default();
		generate(
			&mut runner,
			r#"
command
---

command
command
---
"#,
		)
		.unwrap();

		assert_eq!(runner.start_script_count, 1);
		assert_eq!(runner.end_script_count, 1);
		assert_eq!(runner.start_block_count, 2);
		assert_eq!(runner.end_block_count, 2);
		assert_eq!(runner.start_command_count, 3);
		assert_eq!(runner.end_command_count, 3);
	}
}