mirror of
https://github.com/imjasonh/testscript-rs
synced 2026-07-08 17:16:38 +00:00
1.6 KiB
1.6 KiB
Sample CLI Tool Example
This directory demonstrates how to use testscript-rs to test a CLI application.
What's Here
src/main.rs- A sample CLI tool with various subcommandstests/integration_test.rs- Integration test using testscript-rstestdata/- Test scripts in.txtarformat
The CLI Tool
The sample CLI tool supports:
count- Count lines, words, or characters in filesgrep- Search for patterns in filescreate- Create files with optional contentremove- Remove fileslist- List directory contents
The Tests
The integration test demonstrates key testscript-rs features:
- Setup Hook - Compiles the CLI binary before running tests
- Binary Testing - Tests the actual compiled binary
- File Operations - Creates files, checks existence, compares content
- Output Validation - Checks stdout/stderr with regex patterns
- Error Testing - Verifies error conditions with negated commands
Running the Tests
cargo test
This will:
- Compile the
sample-clibinary - Copy it to each test's temporary directory
- Run all
.txttest scripts intestdata/ - Report results
Example Test Script
# Test basic functionality
exec ./sample-cli --version
stdout "sample-cli 1.0"
# Test file operations
exec ./sample-cli create hello.txt --content "Hello, World!"
exists hello.txt
exec ./sample-cli count hello.txt
stdout "hello.txt: 1"
# Test cleanup
exec ./sample-cli remove hello.txt
! exists hello.txt
This demonstrates how testscript-rs makes CLI testing both powerful and readable!