Skip to content
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
40 changes: 32 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
name: CI

on:
push:
branches: [main]
branches: ["**"]
pull_request:
branches: [main]

env:
CARGO_TERM_COLOR: always

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable

- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt

- name: Cache cargo
uses: actions/cache@v4
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- run: cargo fmt --check
- run: cargo clippy -- -D warnings
- run: cargo test
- run: cargo build --release
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Check formatting
run: cargo fmt --all -- --check

- name: Clippy
run: cargo clippy --all-targets --all-features -- -D warnings

- name: Test
run: cargo test --all

- name: Build release
run: cargo build --release
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "screendiff", about = "Screenshot pixel diff engine — detect visual regressions", version)]
#[command(
name = "screendiff",
about = "Screenshot pixel diff engine — detect visual regressions",
version
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
mod cli;
mod output;

use screendiff::{diff, report};
use anyhow::Result;
use clap::Parser;
use cli::{Cli, Commands};
use screendiff::{diff, report};

fn main() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Compare { before, after, threshold, json } => {
Commands::Compare {
before,
after,
threshold,
json,
} => {
let result = diff::compare(&before, &after, threshold)?;
let report = report::build(&before, &after, &result);
if json {
Expand Down
2 changes: 1 addition & 1 deletion src/report.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use crate::diff::DiffResult;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct DiffReport {
Expand Down
48 changes: 12 additions & 36 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ fn test_compare_help() {
fn test_identical_images_zero_diff() {
let a = solid_png(255, 0, 0);
let b = solid_png(255, 0, 0);
let result = diff::compare(
a.path().to_str().unwrap(),
b.path().to_str().unwrap(),
0.01,
)
.unwrap();
let result =
diff::compare(a.path().to_str().unwrap(), b.path().to_str().unwrap(), 0.01).unwrap();
assert_eq!(result.diff_pixels, 0);
assert_eq!(result.diff_percent, 0.0);
}
Expand All @@ -65,12 +61,8 @@ fn test_identical_images_zero_diff() {
fn test_completely_different_images_full_diff() {
let a = solid_png(255, 0, 0);
let b = solid_png(0, 0, 255);
let result = diff::compare(
a.path().to_str().unwrap(),
b.path().to_str().unwrap(),
0.01,
)
.unwrap();
let result =
diff::compare(a.path().to_str().unwrap(), b.path().to_str().unwrap(), 0.01).unwrap();
assert_eq!(result.diff_pixels, 10_000);
assert_eq!(result.diff_percent, 1.0);
}
Expand All @@ -79,12 +71,8 @@ fn test_completely_different_images_full_diff() {
fn test_one_pixel_diff_counted() {
let a = solid_png(255, 0, 0);
let b = mostly_red_png();
let result = diff::compare(
a.path().to_str().unwrap(),
b.path().to_str().unwrap(),
0.01,
)
.unwrap();
let result =
diff::compare(a.path().to_str().unwrap(), b.path().to_str().unwrap(), 0.01).unwrap();
assert_eq!(result.diff_pixels, 1);
}

Expand All @@ -110,12 +98,8 @@ fn test_dimension_mismatch_returns_error() {
fn test_zero_diff_verdict_is_pass() {
let a = solid_png(0, 255, 0);
let b = solid_png(0, 255, 0);
let result = diff::compare(
a.path().to_str().unwrap(),
b.path().to_str().unwrap(),
0.01,
)
.unwrap();
let result =
diff::compare(a.path().to_str().unwrap(), b.path().to_str().unwrap(), 0.01).unwrap();
let rep = report::build("a.png", "b.png", &result);
assert!(matches!(rep.verdict, Verdict::Pass));
}
Expand All @@ -124,12 +108,8 @@ fn test_zero_diff_verdict_is_pass() {
fn test_over_threshold_verdict_is_fail() {
let a = solid_png(255, 0, 0);
let b = solid_png(0, 0, 255);
let result = diff::compare(
a.path().to_str().unwrap(),
b.path().to_str().unwrap(),
0.01,
)
.unwrap();
let result =
diff::compare(a.path().to_str().unwrap(), b.path().to_str().unwrap(), 0.01).unwrap();
let rep = report::build("a.png", "b.png", &result);
assert!(matches!(rep.verdict, Verdict::Fail));
}
Expand All @@ -139,12 +119,8 @@ fn test_single_pixel_diff_within_threshold_passes() {
// 1 changed pixel in 10000 = 0.0001 — well under 1% threshold
let a = solid_png(255, 0, 0);
let b = mostly_red_png();
let result = diff::compare(
a.path().to_str().unwrap(),
b.path().to_str().unwrap(),
0.01,
)
.unwrap();
let result =
diff::compare(a.path().to_str().unwrap(), b.path().to_str().unwrap(), 0.01).unwrap();
let rep = report::build("a.png", "b.png", &result);
assert!(matches!(rep.verdict, Verdict::Pass));
}
Expand Down
Loading