Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert_cmd and predicated to dev deps #2

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 171 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ name = "fakemall"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.1.4", features = ["derive"] }
toml_edit = "0.19.0"
toml = "0.7.0"
serde = { version = "1.0.152", features = ["derive"] }
anyhow = "1.0.68"
anyhow = "1.0.68"

[dev-dependencies]
assert_cmd = "2.0.12"
predicates = "3.0.4"
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
fs::{self, File},
io::{Read, Write},
os::unix::prelude::PermissionsExt,
path::{PathBuf},
path::PathBuf,
process,
};
/// A simple tool for building fake command line interfaces
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() -> Result<()> {

match &cli.command {
Commands::Exec { set, command } => exec(set, command.trim()),
Commands::Save {.. } => todo!(),
Commands::Save { .. } => todo!(),
Commands::Build { set, path } => build(set, path),
}
}
Expand Down
41 changes: 41 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::process::Command;

#[test]
fn fakes_commands() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("fakemall")?;

cmd.arg("exec")
.arg("tests/test_config.toml")
.arg("dmidecode");
cmd.assert()
.success()
.stdout(predicate::str::contains("this is a sample output"));

Ok(())
}

#[test]
fn set_doesnt_exist() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("fakemall")?;

cmd.arg("exec").arg("test/file/doesnt/exist").arg("kekw");
cmd.assert()
.failure()
.stderr(predicate::str::contains("No such file or directory"));

Ok(())
}

#[test]
fn command_doesnt_exist() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("fakemall")?;

cmd.arg("exec").arg("tests/test_config.toml").arg("asdf");
cmd.assert()
.failure()
.stdout(predicate::str::contains("asdf: command not found"));

Ok(())
}
3 changes: 3 additions & 0 deletions tests/test_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[commands]]
matches = "dmidecode"
output = "this is a sample output"
Loading