Skip to content

Commit

Permalink
test: add test framework for the CLI (#291)
Browse files Browse the repository at this point in the history
This introduces a testing framework for the CLI, which was completely uncovered by tests, allowing regression issues.
  • Loading branch information
plusvic authored Jan 16, 2025
1 parent 8968a39 commit 6a99e6a
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
*.cst text eol=lf
*.ir eol=lf
*.unformatted text eol=lf
*.stdout text eol=lf
*.stderr text eol=lf
*.yar text eol=lf

7 changes: 6 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ jobs:
with:
toolchain: ${{ matrix.rust }}

- name: Run cargo test
- name: Build
run: cargo build --all-targets ${{ matrix.args }}
env:
RUSTFLAGS: ${{ matrix.rust_flags }}

- name: Run tests
run: cargo test --all-targets ${{ matrix.args }}
env:
RUSTFLAGS: ${{ matrix.rust_flags }}
90 changes: 90 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ num-traits = "0.2.19"
num-derive = "0.4.2"
p256 = "0.13.2"
p384 = "0.13.0"
predicates = "3.1.3"
pretty_assertions = "1.4.0"
protobuf = "3.7.1"
protobuf-codegen = "3.7.1"
Expand Down
10 changes: 10 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ rust-version.workspace = true
[[bin]]
name = "yr"
path = "src/main.rs"
# Don't run the CLI tests by default when `cargo test` is executed. The
# CLI tests require the `target/debug/yr` binary do exists before the
# tests are executed. The CLI tests can be run with:
# cargo build
# cargo test -p yara-x-cli --bin yr
test = false

[features]
# Enable the "debug" command for developers.
Expand Down Expand Up @@ -66,3 +72,7 @@ encoding_rs = "0.8.35"
strum_macros = "0.26.4"
superconsole = "0.2.0"
wild = "2.2.1"

[dev-dependencies]
assert_cmd = "2.0.16"
predicates = { workspace = true }
1 change: 1 addition & 0 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn create_compiler(
.colorize_errors(stdout().is_tty());

for m in args.get_many::<String>("ignore-module").into_iter().flatten() {
println!("ignore {}", m);
compiler.ignore_module(m);
}

Expand Down
11 changes: 9 additions & 2 deletions cli/src/commands/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,10 +912,17 @@ mod output_handler {
}
}
PatternKind::Hex => {
for b in data {
for (pos, b) in data.iter().with_position()
{
match_str.push_str(
format!("{:02x} ", b).as_str(),
format!("{:02x}", b).as_str(),
);
if !matches!(
pos,
itertools::Position::Last
) {
match_str.push(' ');
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod config;
mod help;
mod walk;

#[cfg(test)]
mod tests;

use config::{load_config_from_file, Config};
use crossterm::tty::IsTty;
use std::{io, panic, process};
Expand Down
Loading

0 comments on commit 6a99e6a

Please sign in to comment.