use std::fs;
use std::path::Path;
use assert_cmd::Command;
use tempfile::TempDir;
fn tree(files: &[(&str, &str)]) -> TempDir {
let dir = tempfile::tempdir().unwrap();
for (path, text) in files {
let path = dir.path().join(path);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
}
dir
}
fn lint(dir: &Path, args: &[&str]) -> (i32, String, String) {
let output = Command::cargo_bin("blade")
.unwrap()
.current_dir(dir)
.arg("lint")
.args(args)
.output()
.unwrap();
(
output.status.code().unwrap(),
String::from_utf8(output.stdout).unwrap(),
String::from_utf8(output.stderr).unwrap(),
)
}
#[test]
fn lints_a_directory_human() {
let dir = tree(&[("a.blade.php", "@if($a)\n")]);
let (code, out, _) = lint(dir.path(), &[]);
assert_eq!(code, 1, "{out}");
for expected in [
"a.blade.php:1:1: error[syntax]: unclosed @if: expected @endif",
"1 | @if($a)",
"^^^",
"1 error in 1 file",
] {
assert!(out.contains(expected), "missing {expected:?} in:\n{out}");
}
}
#[test]
fn human_output_snippet_with_tab_and_crlf() {
let dir = tree(&[("a.blade.php", "x\r\n\t{!! $h !!}\r\n")]);
let (code, out, _) = lint(dir.path(), &[]);
assert_eq!(code, 0, "{out}");
assert!(out.contains("2 | \t{!! $h !!}\n"), "{out:?}");
assert!(out.contains(" | \t^^^^^^^^^^\n"), "{out:?}");
assert!(out.contains("1 warning in 1 file"), "{out}");
}
#[test]
fn walk_respects_gitignore_and_explicit_files() {
let bad = "@if($a)";
let dir = tree(&[
(".gitignore", "ignored/\n"),
("ignored/x.blade.php", bad),
(".hidden/y.blade.php", bad),
("ok/z.blade.php", bad),
("notes.txt", bad),
]);
let (code, out, _) = lint(dir.path(), &["."]);
assert_eq!(code, 1, "{out}");
assert!(out.contains("ok/z.blade.php:1:1"), "{out}");
for hidden in ["x.blade.php", "y.blade.php", "notes.txt"] {
assert!(!out.contains(hidden), "{hidden} in:\n{out}");
}
let (_, out, _) = lint(dir.path(), &["ignored/x.blade.php"]);
assert!(out.contains("ignored/x.blade.php:1:1"), "{out}");
}
#[test]
fn exit_codes() {
let dir = tree(&[
("clean.blade.php", "<p>{{ $x }}</p>\n"),
("warn.blade.php", "{!! $x !!}\n"),
("error.blade.php", "@if($x)\n"),
]);
let (code, out, _) = lint(dir.path(), &["clean.blade.php"]);
assert_eq!(code, 0);
assert!(out.contains("no problems in 1 file"), "{out}");
assert_eq!(lint(dir.path(), &["warn.blade.php"]).0, 0);
assert_eq!(lint(dir.path(), &["error.blade.php"]).0, 1);
let (code, _, err) = lint(dir.path(), &["missing.blade.php"]);
assert_eq!(code, 2);
assert!(err.contains("missing.blade.php"), "{err}");
}
#[test]
fn config_turns_rules_off() {
let dir = tree(&[
("blade.toml", "[lint.rules]\nraw-echo = \"off\"\n"),
("a.blade.php", "{!! $x !!}\n"),
]);
let (code, out, _) = lint(dir.path(), &[]);
assert_eq!(code, 0);
assert!(!out.contains("raw-echo"), "{out}");
assert!(out.contains("no problems in 1 file"), "{out}");
}
#[test]
fn bad_config_is_exit_2() {
for (config, name) in [
("[lint.rules]\nnope = \"off\"\n", "nope"),
("[lint]\ncolour = 1\n", "colour"),
("[lint.rules]\nraw-echo = \"loud\"\n", "loud"),
] {
let dir = tree(&[("blade.toml", config), ("a.blade.php", "x")]);
let (code, _, err) = lint(dir.path(), &[]);
assert_eq!(code, 2, "{config}");
assert!(err.contains(name), "{name} not in: {err}");
}
}
#[test]
fn json_output() {
let dir = tree(&[("a.blade.php", "@if($a)\n")]);
let (code, out, _) = lint(dir.path(), &["--format", "json"]);
assert_eq!(code, 1);
let json: serde_json::Value = serde_json::from_str(&out).unwrap();
let d = &json["diagnostics"][0];
assert_eq!(d["path"], "a.blade.php");
assert_eq!(d["rule"], "syntax");
assert_eq!(d["severity"], "error");
assert_eq!(d["message"], "unclosed @if: expected @endif");
assert_eq!(d["line"], 1);
assert_eq!(d["column"], 1);
assert_eq!(d["end_line"], 1);
assert_eq!(d["end_column"], 4);
assert_eq!(json["summary"]["files"], 1);
assert_eq!(json["summary"]["errors"], 1);
assert_eq!(json["summary"]["warnings"], 0);
}
#[test]
fn github_output() {
let dir = tree(&[("a.blade.php", "@if($a)\n")]);
let (code, out, _) = lint(dir.path(), &["--format", "github"]);
assert_eq!(code, 1);
assert_eq!(
out,
"::error file=a.blade.php,line=1,col=1,endLine=1,endColumn=4,title=blade lint (syntax)::unclosed @if: expected @endif\n"
);
}