From 8beb2908420b0225aabe983e3425055db761b356 Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 25 Jul 2024 16:14:38 +0200 Subject: [PATCH] Test initialization --- src/main.rs | 4 - tests/integration_tests.rs | 98 ++++++++++++------- .../test_exercises/exercises/test_failure.rs | 2 +- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3c96d1a9..658d551d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,10 +95,6 @@ fn main() -> Result<()> { match args.command { Some(Subcommands::Init) => { - if DEBUG_PROFILE { - bail!("Disabled in the debug build"); - } - { let mut stdout = io::stdout().lock(); stdout.write_all(b"This command will create the directory `rustlings/` which will contain the exercises.\nPress ENTER to continue ")?; diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index f21ee2f6..3ab54f97 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1,15 +1,23 @@ use std::{ env::{self, consts::EXE_SUFFIX}, + fs, process::{Command, Stdio}, str::from_utf8, }; +enum Output<'a> { + FullStdout(&'a str), + PartialStdout(&'a str), + PartialStderr(&'a str), +} + +use Output::*; + #[derive(Default)] struct Cmd<'a> { current_dir: Option<&'a str>, args: &'a [&'a str], - stdout: Option<&'a str>, - full_stdout: bool, + output: Option>, } impl<'a> Cmd<'a> { @@ -26,14 +34,8 @@ impl<'a> Cmd<'a> { } #[inline] - fn stdout(&mut self, stdout: &'a str) -> &mut Self { - self.stdout = Some(stdout); - self - } - - #[inline] - fn full_stdout(&mut self) -> &mut Self { - self.full_stdout = true; + fn output(&mut self, output: Output<'a>) -> &mut Self { + self.output = Some(output); self } @@ -57,26 +59,32 @@ impl<'a> Cmd<'a> { cmd.current_dir(current_dir); } - cmd.args(self.args) - .stdin(Stdio::null()) - .stderr(Stdio::null()); + cmd.args(self.args).stdin(Stdio::null()); - let status = if let Some(expected_stdout) = self.stdout { - let output = cmd.output().unwrap(); - let stdout = from_utf8(&output.stdout).unwrap(); - - if self.full_stdout { - assert_eq!(stdout, expected_stdout); - } else { - assert!(stdout.contains(expected_stdout)); + let status = match self.output { + None => cmd + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .unwrap(), + Some(FullStdout(stdout)) => { + let output = cmd.stderr(Stdio::null()).output().unwrap(); + assert_eq!(from_utf8(&output.stdout).unwrap(), stdout); + output.status + } + Some(PartialStdout(stdout)) => { + let output = cmd.stderr(Stdio::null()).output().unwrap(); + assert!(from_utf8(&output.stdout).unwrap().contains(stdout)); + output.status + } + Some(PartialStderr(stderr)) => { + let output = cmd.stdout(Stdio::null()).output().unwrap(); + assert!(from_utf8(&output.stderr).unwrap().contains(stderr)); + output.status } - - output.status - } else { - cmd.stdout(Stdio::null()).status().unwrap() }; - assert_eq!(status.success(), success); + assert_eq!(status.success(), success, "{cmd:?}"); } #[inline] @@ -90,11 +98,6 @@ impl<'a> Cmd<'a> { } } -#[test] -fn wrong_dir() { - Cmd::default().current_dir("tests").fail(); -} - #[test] fn run_compilation_success() { Cmd::default() @@ -116,7 +119,7 @@ fn run_test_success() { Cmd::default() .current_dir("tests/test_exercises") .args(&["run", "test_success"]) - .stdout("\nOutput from `main` function\n") + .output(PartialStdout("\nOutput from `main` function\n")) .success(); } @@ -146,7 +149,34 @@ fn hint() { Cmd::default() .current_dir("tests/test_exercises") .args(&["hint", "test_failure"]) - .stdout("The answer to everything: 42\n") - .full_stdout() + .output(FullStdout("The answer to everything: 42\n")) .success(); } + +#[test] +fn init() { + let _ = fs::remove_dir_all("tests/rustlings"); + + Cmd::default().current_dir("tests").fail(); + + Cmd::default() + .current_dir("tests") + .args(&["init"]) + .success(); + + // Running `init` after a successful initialization. + Cmd::default() + .current_dir("tests") + .args(&["init"]) + .output(PartialStderr("`cd rustlings`")) + .fail(); + + // Running `init` in the initialized directory. + Cmd::default() + .current_dir("tests/rustlings") + .args(&["init"]) + .output(PartialStderr("already initialized")) + .fail(); + + fs::remove_dir_all("tests/rustlings").unwrap(); +} diff --git a/tests/test_exercises/exercises/test_failure.rs b/tests/test_exercises/exercises/test_failure.rs index 9dc142a4..8c8d59d8 100644 --- a/tests/test_exercises/exercises/test_failure.rs +++ b/tests/test_exercises/exercises/test_failure.rs @@ -4,6 +4,6 @@ fn main() {} mod tests { #[test] fn fails() { - asset!(false); + assert!(false); } }