mirror of
https://github.com/imjasonh/testscript-rs
synced 2026-07-09 01:26:40 +00:00
add GHA workflow (#1)
* add GHA workflow Signed-off-by: Jason Hall <jason@chainguard.dev> * fix CI Signed-off-by: Jason Hall <jason@chainguard.dev> * update readme Signed-off-by: Jason Hall <jason@chainguard.dev> * add badge Signed-off-by: Jason Hall <jason@chainguard.dev> --------- Signed-off-by: Jason Hall <jason@chainguard.dev>
This commit is contained in:
parent
fa546ce09f
commit
448cf615db
16 changed files with 381 additions and 156 deletions
8
examples/sample-cli/Cargo.lock
generated
8
examples/sample-cli/Cargo.lock
generated
|
|
@ -321,18 +321,18 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.69"
|
||||
version = "2.0.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||
checksum = "3467d614147380f2e4e374161426ff399c91084acd2363eaf549172b3d5e60c0"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.69"
|
||||
version = "2.0.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||
checksum = "6c5e1be1c48b9172ee610da68fd9cd2770e7a4056cb3fc98710ee6906f0c7960"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ name = "sample-cli"
|
|||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.0", features = ["derive"] }
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
|
||||
[dev-dependencies]
|
||||
testscript-rs = { path = "../.." }
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::fs;
|
||||
use std::io::{self, BufRead, BufReader};
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Parser)]
|
||||
|
|
@ -76,7 +76,10 @@ impl std::str::FromStr for CountMode {
|
|||
"lines" | "line" | "l" => Ok(CountMode::Lines),
|
||||
"words" | "word" | "w" => Ok(CountMode::Words),
|
||||
"chars" | "char" | "c" => Ok(CountMode::Chars),
|
||||
_ => Err(format!("Invalid count mode: {}. Use lines, words, or chars", s)),
|
||||
_ => Err(format!(
|
||||
"Invalid count mode: {}. Use lines, words, or chars",
|
||||
s
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -86,7 +89,11 @@ fn main() {
|
|||
|
||||
let result = match cli.command {
|
||||
Some(Commands::Count { mode, files }) => count_command(mode, files),
|
||||
Some(Commands::Grep { pattern, files, ignore_case }) => grep_command(pattern, files, ignore_case),
|
||||
Some(Commands::Grep {
|
||||
pattern,
|
||||
files,
|
||||
ignore_case,
|
||||
}) => grep_command(pattern, files, ignore_case),
|
||||
Some(Commands::Create { file, content }) => create_command(file, content),
|
||||
Some(Commands::Remove { files }) => remove_command(files),
|
||||
Some(Commands::List { dir }) => list_command(dir),
|
||||
|
|
@ -123,7 +130,11 @@ fn count_command(mode: CountMode, files: Vec<String>) -> Result<(), Box<dyn std:
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn grep_command(pattern: String, files: Vec<String>, ignore_case: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn grep_command(
|
||||
pattern: String,
|
||||
files: Vec<String>,
|
||||
ignore_case: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if files.is_empty() {
|
||||
return Err("No files specified".into());
|
||||
}
|
||||
|
|
@ -194,4 +205,4 @@ fn list_command(dir: String) -> Result<(), Box<dyn std::error::Error>> {
|
|||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
//!
|
||||
//! This demonstrates how to use testscript-rs to test a CLI application.
|
||||
|
||||
use testscript_rs::testscript;
|
||||
use std::process::Command;
|
||||
use testscript_rs::testscript;
|
||||
|
||||
#[test]
|
||||
fn test_sample_cli() {
|
||||
|
|
@ -20,9 +20,10 @@ fn test_sample_cli() {
|
|||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(testscript_rs::Error::Generic(
|
||||
format!("Failed to build sample-cli: {}", stderr)
|
||||
));
|
||||
return Err(testscript_rs::Error::Generic(format!(
|
||||
"Failed to build sample-cli: {}",
|
||||
stderr
|
||||
)));
|
||||
}
|
||||
|
||||
// Copy binary to test working directory
|
||||
|
|
@ -40,11 +41,14 @@ fn test_sample_cli() {
|
|||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mut perms = std::fs::metadata(&dest)
|
||||
.map_err(|e| testscript_rs::Error::Generic(format!("Failed to get permissions: {}", e)))?
|
||||
.map_err(|e| {
|
||||
testscript_rs::Error::Generic(format!("Failed to get permissions: {}", e))
|
||||
})?
|
||||
.permissions();
|
||||
perms.set_mode(0o755);
|
||||
std::fs::set_permissions(&dest, perms)
|
||||
.map_err(|e| testscript_rs::Error::Generic(format!("Failed to set permissions: {}", e)))?;
|
||||
std::fs::set_permissions(&dest, perms).map_err(|e| {
|
||||
testscript_rs::Error::Generic(format!("Failed to set permissions: {}", e))
|
||||
})?;
|
||||
}
|
||||
|
||||
println!("Built and copied sample-cli to: {}", dest.display());
|
||||
|
|
@ -57,6 +61,4 @@ fn test_sample_cli() {
|
|||
Ok(_) => println!("All sample-cli tests passed!"),
|
||||
Err(e) => println!("Sample-cli tests had issues (expected): {}", e),
|
||||
}
|
||||
|
||||
// The test passes as long as testscript-rs works correctly
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue