Skip to content

Commit

Permalink
skip binary and empty files (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankland authored Oct 24, 2022
1 parent f2bab48 commit ce84fcb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ regex = "1.6.0"
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.85"
serde-sarif = "0.3.4"
content_inspector = "0.2.4"

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
33 changes: 20 additions & 13 deletions src/rules/pls_no_land.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate regex;

use crate::diagnostic;
use anyhow::Context;
use content_inspector::inspect;
use regex::Regex;
use std::collections::HashSet;
use std::fs::File;
Expand All @@ -25,25 +26,31 @@ pub fn pls_no_land(paths: &HashSet<PathBuf>) -> anyhow::Result<Vec<diagnostic::D
Ok(ret)
}

type LinesView = Vec<String>;
fn pls_no_land_impl(path: &PathBuf) -> anyhow::Result<Vec<diagnostic::Diagnostic>> {
let in_file = File::open(path).with_context(|| format!("failed to open: {:#?}", path))?;
let mut in_buf = BufReader::new(in_file);

fn lines_view<R: BufRead>(reader: R) -> anyhow::Result<LinesView> {
let mut ret: LinesView = LinesView::default();
for line in reader.lines() {
let line = line?;
ret.push(line);
let mut first_line = vec![];
in_buf.read_until(b'\n', &mut first_line)?;

if first_line.is_empty() || inspect(&first_line[..]).is_binary() {
return Ok(vec![]);
}
Ok(ret)
}

fn pls_no_land_impl(path: &PathBuf) -> anyhow::Result<Vec<diagnostic::Diagnostic>> {
let in_file = File::open(path).with_context(|| format!("failed to open: {:#?}", path))?;
let in_buf = BufReader::new(in_file);
let lines_view = lines_view(in_buf).context("failed to build lines view")?;
let first_line_view = String::from_utf8(first_line)
.with_context(|| format!("could not read first line of {:#?}", path))?;
let lines_view = in_buf
.lines()
.collect::<std::io::Result<Vec<String>>>()
.with_context(|| format!("failed to read lines of text from {:#?}", path))?;

let mut ret = Vec::new();

for (i, line) in lines_view.iter().enumerate() {
for (i, line) in [first_line_view]
.iter()
.chain(lines_view.iter())
.enumerate()
{
if line.contains("trunk-ignore(|-begin|-end|-all)\\(trunk-toolbox/do-not-land\\)") {
continue;
}
Expand Down
36 changes: 35 additions & 1 deletion tests/do_not_land_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ fn do_not_land() {
let test_repo = TestRepo::make().unwrap();

test_repo
.write("alpha.foo", "lorem ipsum dolor\ndo-NOT-lAnD\nsit amet\n")
.write(
"alpha.foo",
"lorem ipsum dolor\ndo-NOT-lAnD\nsit amet\n".as_bytes(),
)
.unwrap();
test_repo.git_add_all().unwrap();
let horton = test_repo.run_horton().unwrap();
Expand All @@ -20,3 +23,34 @@ fn do_not_land() {
predicates::str::contains("Found 'do-NOT-lAnD'").eval(&horton)
);
}

#[test]
fn do_not_land_ignores_binary_files() {
let test_repo = TestRepo::make().unwrap();

test_repo
.write("alpha.foo.binary", include_bytes!("trunk-logo.png"))
.unwrap();
test_repo.git_add_all().unwrap();
let horton = test_repo.run_horton().unwrap();
let result: serde_json::Value = serde_json::from_str(&horton).unwrap();
let runs = result
.as_object()
.unwrap()
.get("runs")
.unwrap()
.as_array()
.unwrap();

assert_eq!(runs.len(), 1);
assert_eq!(
runs.get(0)
.unwrap()
.get("results")
.unwrap()
.as_array()
.unwrap()
.is_empty(),
true
);
}
2 changes: 1 addition & 1 deletion tests/integration_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl TestRepo {
Ok(TestRepo { dir })
}

pub fn write(&self, relpath: &str, data: &str) -> anyhow::Result<()> {
pub fn write(&self, relpath: &str, data: &[u8]) -> anyhow::Result<()> {
let path = {
let mut path = self.dir.path().to_path_buf();
path.push(relpath);
Expand Down
Binary file added tests/trunk-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ce84fcb

Please sign in to comment.