Remove assert_cmd and predicates

This commit is contained in:
mo8it 2024-07-25 14:34:43 +02:00
parent e2492f65a0
commit 3f49decce9
3 changed files with 131 additions and 227 deletions

147
Cargo.lock generated
View file

@ -14,15 +14,6 @@ dependencies = [
"zerocopy", "zerocopy",
] ]
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "allocator-api2" name = "allocator-api2"
version = "0.2.18" version = "0.2.18"
@ -84,21 +75,6 @@ version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "assert_cmd"
version = "2.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed72493ac66d5804837f480ab3766c72bdfab91a65e565fc54fa9e42db0073a8"
dependencies = [
"anstyle",
"bstr",
"doc-comment",
"predicates",
"predicates-core",
"predicates-tree",
"wait-timeout",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.3.0" version = "1.3.0"
@ -117,17 +93,6 @@ version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "bstr"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706"
dependencies = [
"memchr",
"regex-automata",
"serde",
]
[[package]] [[package]]
name = "cassowary" name = "cassowary"
version = "0.3.0" version = "0.3.0"
@ -248,18 +213,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "difflib"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]] [[package]]
name = "either" name = "either"
version = "1.13.0" version = "1.13.0"
@ -284,15 +237,6 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "float-cmp"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "fsevent-sys" name = "fsevent-sys"
version = "4.1.0" version = "4.1.0"
@ -438,12 +382,6 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "normalize-line-endings"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
[[package]] [[package]]
name = "notify" name = "notify"
version = "6.1.1" version = "6.1.1"
@ -473,15 +411,6 @@ dependencies = [
"notify", "notify",
] ]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.19.0" version = "1.19.0"
@ -527,36 +456,6 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "predicates"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b87bfd4605926cdfefc1c3b5f8fe560e3feca9d5552cf68c466d3d8236c7e8"
dependencies = [
"anstyle",
"difflib",
"float-cmp",
"normalize-line-endings",
"predicates-core",
"regex",
]
[[package]]
name = "predicates-core"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174"
[[package]]
name = "predicates-tree"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf"
dependencies = [
"predicates-core",
"termtree",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.86" version = "1.0.86"
@ -614,47 +513,16 @@ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
] ]
[[package]]
name = "regex"
version = "1.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]] [[package]]
name = "rustlings" name = "rustlings"
version = "6.1.0" version = "6.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"assert_cmd",
"clap", "clap",
"crossterm", "crossterm",
"hashbrown", "hashbrown",
"notify-debouncer-mini", "notify-debouncer-mini",
"os_pipe", "os_pipe",
"predicates",
"ratatui", "ratatui",
"rustlings-macros", "rustlings-macros",
"serde", "serde",
@ -829,12 +697,6 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "termtree"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76"
[[package]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.6.6" version = "0.6.6"
@ -898,15 +760,6 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wait-timeout"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.5.0" version = "2.5.0"

View file

@ -58,10 +58,6 @@ serde_json = "1.0.120"
serde.workspace = true serde.workspace = true
toml_edit.workspace = true toml_edit.workspace = true
[dev-dependencies]
assert_cmd = "2.0.14"
predicates = "3.1.0"
[profile.release] [profile.release]
panic = "abort" panic = "abort"

View file

@ -1,134 +1,189 @@
use assert_cmd::prelude::*; use std::{
use std::process::Command; env::{self, consts::EXE_SUFFIX},
process::{Command, Stdio},
str::from_utf8,
};
#[derive(Default)]
struct Cmd<'a> {
current_dir: Option<&'a str>,
args: &'a [&'a str],
stdout: Option<&'a str>,
full_stdout: bool,
}
impl<'a> Cmd<'a> {
#[inline]
fn current_dir(&mut self, current_dir: &'a str) -> &mut Self {
self.current_dir = Some(current_dir);
self
}
#[inline]
fn args(&mut self, args: &'a [&'a str]) -> &mut Self {
self.args = args;
self
}
#[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;
self
}
fn assert(&self, success: bool) {
let rustlings_bin = {
let mut path = env::current_exe().unwrap();
// Pop test binary name
path.pop();
// Pop `/deps`
path.pop();
path.push("rustlings");
let mut path = path.into_os_string();
path.push(EXE_SUFFIX);
path
};
let mut cmd = Command::new(rustlings_bin);
if let Some(current_dir) = self.current_dir {
cmd.current_dir(current_dir);
}
cmd.args(self.args)
.stdin(Stdio::null())
.stderr(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));
}
output.status
} else {
cmd.stdout(Stdio::null()).status().unwrap()
};
assert_eq!(status.success(), success);
}
#[inline]
fn success(&self) {
self.assert(true);
}
#[inline]
fn fail(&self) {
self.assert(false);
}
}
#[test] #[test]
fn fails_when_in_wrong_dir() { fn fails_when_in_wrong_dir() {
Command::cargo_bin("rustlings") Cmd::default().current_dir("tests").fail();
.unwrap()
.current_dir("tests/")
.assert()
.code(1);
} }
#[test] #[test]
fn run_single_compile_success() { fn run_single_compile_success() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap() .current_dir("tests/fixture/success")
.args(["run", "compSuccess"]) .args(&["run", "compSuccess"])
.current_dir("tests/fixture/success/")
.assert()
.success(); .success();
} }
#[test] #[test]
fn run_single_compile_failure() { fn run_single_compile_failure() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap() .current_dir("tests/fixture/failure")
.args(["run", "compFailure"]) .args(&["run", "compFailure"])
.current_dir("tests/fixture/failure/") .fail();
.assert()
.code(1);
} }
#[test] #[test]
fn run_single_test_success() { fn run_single_test_success() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap() .current_dir("tests/fixture/success")
.args(["run", "testSuccess"]) .args(&["run", "testSuccess"])
.current_dir("tests/fixture/success/")
.assert()
.success(); .success();
} }
#[test] #[test]
fn run_single_test_failure() { fn run_single_test_failure() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap() .current_dir("tests/fixture/failure")
.args(["run", "testFailure"]) .args(&["run", "testFailure"])
.current_dir("tests/fixture/failure/") .fail();
.assert()
.code(1);
} }
#[test] #[test]
fn run_single_test_not_passed() { fn run_single_test_not_passed() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap() .current_dir("tests/fixture/failure")
.args(["run", "testNotPassed.rs"]) .args(&["run", "testNotPassed.rs"])
.current_dir("tests/fixture/failure/") .fail();
.assert()
.code(1);
} }
#[test] #[test]
fn run_single_test_no_exercise() { fn run_single_test_no_exercise() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap()
.args(["run", "compNoExercise.rs"])
.current_dir("tests/fixture/failure") .current_dir("tests/fixture/failure")
.assert() .args(&["run", "compNoExercise.rs"])
.code(1); .fail();
} }
#[test] #[test]
fn reset_single_exercise() { fn reset_single_exercise() {
Command::cargo_bin("rustlings") Cmd::default().args(&["reset", "intro1"]).success();
.unwrap()
.args(["reset", "intro1"])
.assert()
.code(0);
} }
#[test] #[test]
fn reset_no_exercise() { fn reset_no_exercise() {
Command::cargo_bin("rustlings") Cmd::default().args(&["reset"]).fail();
.unwrap()
.arg("reset")
.assert()
.code(2)
.stderr(predicates::str::contains(
"required arguments were not provided",
));
} }
#[test] #[test]
fn get_hint_for_single_test() { fn get_hint_for_single_test() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap()
.args(["hint", "testFailure"])
.current_dir("tests/fixture/failure") .current_dir("tests/fixture/failure")
.assert() .args(&["hint", "testFailure"])
.code(0) .stdout("Hello!\n")
.stdout("Hello!\n"); .full_stdout()
.success();
} }
#[test] #[test]
fn run_compile_exercise_does_not_prompt() { fn run_compile_exercise_does_not_prompt() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap()
.args(["run", "pending_exercise"])
.current_dir("tests/fixture/state") .current_dir("tests/fixture/state")
.assert() .args(&["run", "pending_exercise"])
.code(0); .success();
} }
#[test] #[test]
fn run_test_exercise_does_not_prompt() { fn run_test_exercise_does_not_prompt() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap()
.args(["run", "pending_test_exercise"])
.current_dir("tests/fixture/state") .current_dir("tests/fixture/state")
.assert() .args(&["run", "pending_test_exercise"])
.code(0); .success();
} }
#[test] #[test]
fn run_single_test_success_with_output() { fn run_single_test_success_with_output() {
Command::cargo_bin("rustlings") Cmd::default()
.unwrap() .current_dir("tests/fixture/success")
.args(["run", "testSuccess"]) .args(&["run", "testSuccess"])
.current_dir("tests/fixture/success/") .stdout("\nTHIS TEST TOO SHALL PASS\n")
.assert() .success();
.code(0)
.stdout(predicates::str::contains("THIS TEST TOO SHALL PASS"));
} }