diffguard 0.2.0

CLI for diff-scoped governance linting in pull requests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Test repository helper for BDD integration tests.
//!
//! Provides a `TestRepo` struct that encapsulates creating and manipulating
//! temporary git repositories for testing diffguard CLI scenarios.

#![allow(dead_code)]
#![allow(deprecated)]

use assert_cmd::Command;
use std::path::{Path, PathBuf};
use tempfile::TempDir;

/// A test git repository for integration testing.
///
/// Provides helpers for:
/// - Creating commits with specific file content
/// - Running diffguard CLI commands
/// - Asserting on exit codes and output
pub struct TestRepo {
    /// The temporary directory containing the repo.
    pub dir: TempDir,
    /// The SHA of the base commit (first commit).
    pub base_sha: String,
}

impl TestRepo {
    /// Create a new empty git repository with an initial commit.
    ///
    /// The initial commit contains a simple baseline file at `src/lib.rs`.
    pub fn new() -> Self {
        let dir = TempDir::new().expect("failed to create temp dir");
        let path = dir.path();

        // Initialize git repo
        run_git(path, &["init"]);
        run_git(path, &["config", "user.email", "test@example.com"]);
        run_git(path, &["config", "user.name", "Test"]);

        // Create baseline file
        std::fs::create_dir_all(path.join("src")).expect("create src dir");
        std::fs::write(
            path.join("src/lib.rs"),
            "pub fn f() -> Option<u32> { Some(1) }\n",
        )
        .expect("write baseline file");

        run_git(path, &["add", "."]);
        run_git(path, &["commit", "-m", "initial baseline"]);

        let base_sha = run_git(path, &["rev-parse", "HEAD"]);

        Self { dir, base_sha }
    }

    /// Create a new repository with custom initial content.
    pub fn with_initial_content(files: &[(&str, &str)]) -> Self {
        let dir = TempDir::new().expect("failed to create temp dir");
        let path = dir.path();

        // Initialize git repo
        run_git(path, &["init"]);
        run_git(path, &["config", "user.email", "test@example.com"]);
        run_git(path, &["config", "user.name", "Test"]);

        // Create all files
        for (file_path, content) in files {
            let full_path = path.join(file_path);
            if let Some(parent) = full_path.parent() {
                std::fs::create_dir_all(parent).expect("create parent dir");
            }
            std::fs::write(&full_path, content).expect("write file");
        }

        run_git(path, &["add", "."]);
        run_git(path, &["commit", "-m", "initial baseline"]);

        let base_sha = run_git(path, &["rev-parse", "HEAD"]);

        Self { dir, base_sha }
    }

    /// Get the path to the repository root.
    pub fn path(&self) -> &Path {
        self.dir.path()
    }

    /// Write content to a file in the repository.
    pub fn write_file(&self, relative_path: &str, content: &str) {
        let full_path = self.path().join(relative_path);
        if let Some(parent) = full_path.parent() {
            std::fs::create_dir_all(parent).expect("create parent dir");
        }
        std::fs::write(&full_path, content).expect("write file");
    }

    /// Create a new commit with the current changes.
    ///
    /// Returns the SHA of the new commit.
    pub fn commit(&self, message: &str) -> String {
        run_git(self.path(), &["add", "."]);
        run_git(self.path(), &["commit", "-m", message]);
        run_git(self.path(), &["rev-parse", "HEAD"])
    }

    /// Create a config file in the repository.
    pub fn write_config(&self, content: &str) {
        self.write_file("diffguard.toml", content);
    }

    /// Run diffguard check command and return the result.
    pub fn run_check(&self, head_sha: &str) -> DiffguardResult {
        self.run_check_with_args(head_sha, &[])
    }

    /// Run diffguard check command with additional arguments.
    pub fn run_check_with_args(&self, head_sha: &str, extra_args: &[&str]) -> DiffguardResult {
        let out_path = self.path().join("artifacts/diffguard/report.json");

        let mut cmd = Command::cargo_bin("diffguard").expect("diffguard binary");
        cmd.current_dir(self.path())
            .arg("check")
            .arg("--base")
            .arg(&self.base_sha)
            .arg("--head")
            .arg(head_sha)
            .arg("--out")
            .arg(&out_path);

        for arg in extra_args {
            cmd.arg(arg);
        }

        let output = cmd.output().expect("run diffguard");

        let receipt = if out_path.exists() {
            Some(std::fs::read_to_string(&out_path).expect("read receipt"))
        } else {
            None
        };

        DiffguardResult {
            exit_code: output.status.code().unwrap_or(-1),
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            receipt,
            output_path: out_path,
        }
    }

    /// Run diffguard check with a custom config file.
    pub fn run_check_with_config(&self, head_sha: &str, config_path: &str) -> DiffguardResult {
        let out_path = self.path().join("artifacts/diffguard/report.json");

        let mut cmd = Command::cargo_bin("diffguard").expect("diffguard binary");
        cmd.current_dir(self.path())
            .arg("check")
            .arg("--base")
            .arg(&self.base_sha)
            .arg("--head")
            .arg(head_sha)
            .arg("--config")
            .arg(config_path)
            .arg("--out")
            .arg(&out_path);

        let output = cmd.output().expect("run diffguard");

        let receipt = if out_path.exists() {
            Some(std::fs::read_to_string(&out_path).expect("read receipt"))
        } else {
            None
        };

        DiffguardResult {
            exit_code: output.status.code().unwrap_or(-1),
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            receipt,
            output_path: out_path,
        }
    }

    /// Run git command that should fail (e.g., for testing missing refs).
    pub fn run_check_with_invalid_base(
        &self,
        head_sha: &str,
        invalid_base: &str,
    ) -> DiffguardResult {
        let out_path = self.path().join("artifacts/diffguard/report.json");

        let mut cmd = Command::cargo_bin("diffguard").expect("diffguard binary");
        cmd.current_dir(self.path())
            .arg("check")
            .arg("--base")
            .arg(invalid_base)
            .arg("--head")
            .arg(head_sha)
            .arg("--out")
            .arg(&out_path);

        let output = cmd.output().expect("run diffguard");

        let receipt = if out_path.exists() {
            Some(std::fs::read_to_string(&out_path).expect("read receipt"))
        } else {
            None
        };

        DiffguardResult {
            exit_code: output.status.code().unwrap_or(-1),
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            receipt,
            output_path: out_path,
        }
    }
}

/// The result of running a diffguard command.
#[derive(Debug)]
pub struct DiffguardResult {
    /// The exit code of the process.
    pub exit_code: i32,
    /// Standard output.
    pub stdout: String,
    /// Standard error.
    pub stderr: String,
    /// The JSON receipt content (if written).
    pub receipt: Option<String>,
    /// Path where the receipt was written.
    pub output_path: PathBuf,
}

impl DiffguardResult {
    /// Assert that the exit code matches expected.
    pub fn assert_exit_code(&self, expected: i32) -> &Self {
        assert_eq!(
            self.exit_code, expected,
            "Expected exit code {} but got {}.\nstderr: {}\nstdout: {}",
            expected, self.exit_code, self.stderr, self.stdout
        );
        self
    }

    /// Assert that a receipt was written.
    pub fn assert_receipt_exists(&self) -> &Self {
        assert!(
            self.receipt.is_some(),
            "Expected receipt to be written at {:?}",
            self.output_path
        );
        self
    }

    /// Assert that the receipt contains the given string.
    pub fn assert_receipt_contains(&self, needle: &str) -> &Self {
        let receipt = self.receipt.as_ref().expect("receipt should exist");
        assert!(
            receipt.contains(needle),
            "Expected receipt to contain '{}', but it didn't.\nReceipt: {}",
            needle,
            receipt
        );
        self
    }

    /// Assert that the receipt does not contain the given string.
    pub fn assert_receipt_not_contains(&self, needle: &str) -> &Self {
        let receipt = self.receipt.as_ref().expect("receipt should exist");
        assert!(
            !receipt.contains(needle),
            "Expected receipt NOT to contain '{}', but it did.\nReceipt: {}",
            needle,
            receipt
        );
        self
    }

    /// Assert that stderr contains the given string.
    pub fn assert_stderr_contains(&self, needle: &str) -> &Self {
        assert!(
            self.stderr.contains(needle),
            "Expected stderr to contain '{}', but it didn't.\nstderr: {}",
            needle,
            self.stderr
        );
        self
    }

    /// Parse the receipt as JSON and return a parsed receipt.
    pub fn parse_receipt(&self) -> ParsedReceipt {
        let receipt = self.receipt.as_ref().expect("receipt should exist");
        let json: serde_json::Value =
            serde_json::from_str(receipt).expect("receipt should be valid JSON");
        ParsedReceipt { json }
    }
}

/// A parsed JSON receipt for detailed assertions.
pub struct ParsedReceipt {
    json: serde_json::Value,
}

impl ParsedReceipt {
    /// Get the number of findings in the receipt.
    pub fn findings_count(&self) -> usize {
        self.json["findings"]
            .as_array()
            .map(|a| a.len())
            .unwrap_or(0)
    }

    /// Get the total count from verdict (info + warn + error).
    pub fn total_count(&self) -> u64 {
        let counts = &self.json["verdict"]["counts"];
        counts["info"].as_u64().unwrap_or(0)
            + counts["warn"].as_u64().unwrap_or(0)
            + counts["error"].as_u64().unwrap_or(0)
    }

    /// Get the error count from verdict.
    pub fn error_count(&self) -> u64 {
        self.json["verdict"]["counts"]["error"]
            .as_u64()
            .unwrap_or(0)
    }

    /// Get the warning count from verdict.
    pub fn warn_count(&self) -> u64 {
        self.json["verdict"]["counts"]["warn"].as_u64().unwrap_or(0)
    }

    /// Check if a specific rule ID appears in the findings.
    pub fn has_finding_with_rule(&self, rule_id: &str) -> bool {
        self.json["findings"]
            .as_array()
            .map(|findings| {
                findings
                    .iter()
                    .any(|f| f["rule_id"].as_str() == Some(rule_id))
            })
            .unwrap_or(false)
    }

    /// Check if a finding exists for a specific file and line.
    pub fn has_finding_at(&self, path: &str, line: u32) -> bool {
        self.json["findings"]
            .as_array()
            .map(|findings| {
                findings.iter().any(|f| {
                    f["path"].as_str() == Some(path) && f["line"].as_u64() == Some(line as u64)
                })
            })
            .unwrap_or(false)
    }

    /// Get the verdict status.
    pub fn verdict_status(&self) -> Option<&str> {
        self.json["verdict"]["status"].as_str()
    }

    /// Get all finding rule IDs.
    pub fn finding_rule_ids(&self) -> Vec<String> {
        self.json["findings"]
            .as_array()
            .map(|findings| {
                findings
                    .iter()
                    .filter_map(|f| f["rule_id"].as_str().map(String::from))
                    .collect()
            })
            .unwrap_or_default()
    }
}

/// Run a git command and return the trimmed stdout.
fn run_git(dir: &Path, args: &[&str]) -> String {
    let output = std::process::Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .expect("git command should run");

    assert!(
        output.status.success(),
        "git {:?} failed: {}",
        args,
        String::from_utf8_lossy(&output.stderr)
    );

    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_repo_creates_valid_git_repo() {
        let repo = TestRepo::new();
        assert!(repo.path().join(".git").exists());
        assert!(!repo.base_sha.is_empty());
    }

    #[test]
    fn test_repo_can_write_and_commit() {
        let repo = TestRepo::new();
        repo.write_file("new_file.txt", "content");
        let new_sha = repo.commit("add new file");
        assert_ne!(repo.base_sha, new_sha);
    }
}