mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 00:00:05 +03:00
Test initialization
This commit is contained in:
parent
8fec5155c7
commit
8beb290842
|
@ -95,10 +95,6 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
match args.command {
|
match args.command {
|
||||||
Some(Subcommands::Init) => {
|
Some(Subcommands::Init) => {
|
||||||
if DEBUG_PROFILE {
|
|
||||||
bail!("Disabled in the debug build");
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut stdout = io::stdout().lock();
|
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 ")?;
|
stdout.write_all(b"This command will create the directory `rustlings/` which will contain the exercises.\nPress ENTER to continue ")?;
|
||||||
|
|
|
@ -1,15 +1,23 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::{self, consts::EXE_SUFFIX},
|
env::{self, consts::EXE_SUFFIX},
|
||||||
|
fs,
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
str::from_utf8,
|
str::from_utf8,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum Output<'a> {
|
||||||
|
FullStdout(&'a str),
|
||||||
|
PartialStdout(&'a str),
|
||||||
|
PartialStderr(&'a str),
|
||||||
|
}
|
||||||
|
|
||||||
|
use Output::*;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Cmd<'a> {
|
struct Cmd<'a> {
|
||||||
current_dir: Option<&'a str>,
|
current_dir: Option<&'a str>,
|
||||||
args: &'a [&'a str],
|
args: &'a [&'a str],
|
||||||
stdout: Option<&'a str>,
|
output: Option<Output<'a>>,
|
||||||
full_stdout: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Cmd<'a> {
|
impl<'a> Cmd<'a> {
|
||||||
|
@ -26,14 +34,8 @@ impl<'a> Cmd<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn stdout(&mut self, stdout: &'a str) -> &mut Self {
|
fn output(&mut self, output: Output<'a>) -> &mut Self {
|
||||||
self.stdout = Some(stdout);
|
self.output = Some(output);
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn full_stdout(&mut self) -> &mut Self {
|
|
||||||
self.full_stdout = true;
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,26 +59,32 @@ impl<'a> Cmd<'a> {
|
||||||
cmd.current_dir(current_dir);
|
cmd.current_dir(current_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.args(self.args)
|
cmd.args(self.args).stdin(Stdio::null());
|
||||||
.stdin(Stdio::null())
|
|
||||||
.stderr(Stdio::null());
|
|
||||||
|
|
||||||
let status = if let Some(expected_stdout) = self.stdout {
|
let status = match self.output {
|
||||||
let output = cmd.output().unwrap();
|
None => cmd
|
||||||
let stdout = from_utf8(&output.stdout).unwrap();
|
.stdout(Stdio::null())
|
||||||
|
.stderr(Stdio::null())
|
||||||
if self.full_stdout {
|
.status()
|
||||||
assert_eq!(stdout, expected_stdout);
|
.unwrap(),
|
||||||
} else {
|
Some(FullStdout(stdout)) => {
|
||||||
assert!(stdout.contains(expected_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]
|
#[inline]
|
||||||
|
@ -90,11 +98,6 @@ impl<'a> Cmd<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn wrong_dir() {
|
|
||||||
Cmd::default().current_dir("tests").fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn run_compilation_success() {
|
fn run_compilation_success() {
|
||||||
Cmd::default()
|
Cmd::default()
|
||||||
|
@ -116,7 +119,7 @@ fn run_test_success() {
|
||||||
Cmd::default()
|
Cmd::default()
|
||||||
.current_dir("tests/test_exercises")
|
.current_dir("tests/test_exercises")
|
||||||
.args(&["run", "test_success"])
|
.args(&["run", "test_success"])
|
||||||
.stdout("\nOutput from `main` function\n")
|
.output(PartialStdout("\nOutput from `main` function\n"))
|
||||||
.success();
|
.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +149,34 @@ fn hint() {
|
||||||
Cmd::default()
|
Cmd::default()
|
||||||
.current_dir("tests/test_exercises")
|
.current_dir("tests/test_exercises")
|
||||||
.args(&["hint", "test_failure"])
|
.args(&["hint", "test_failure"])
|
||||||
.stdout("The answer to everything: 42\n")
|
.output(FullStdout("The answer to everything: 42\n"))
|
||||||
.full_stdout()
|
|
||||||
.success();
|
.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();
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,6 @@ fn main() {}
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn fails() {
|
fn fails() {
|
||||||
asset!(false);
|
assert!(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue