Lints and suggestions for the Nix programming language

chore: extract helper fns for test_lint!

+35 -28
+35 -28
bin/tests/main.rs
··· 1 + use std::path::Path; 2 + 1 3 use lib::session::{SessionInfo, Version}; 2 4 3 5 macro_rules! session_info { ··· 25 27 paste::paste! { 26 28 #[test] 27 29 fn [<$tname _lint>](){ 28 - use statix::{config::OutFormat, traits::WriteDiagnostic, lint}; 29 - use vfs::ReadOnlyVfs; 30 - 31 30 let file_path = concat!("data/", stringify!($tname), ".nix"); 32 31 let contents = include_str!(concat!("data/", stringify!($tname), ".nix")); 33 - 34 - let vfs = ReadOnlyVfs::singleton(file_path, contents.as_bytes()); 35 - 36 - let session = $sess; 37 - 38 - let mut buffer = Vec::new(); 39 - vfs.iter().map(|entry| lint::lint(&entry, &session)).for_each(|r| { 40 - buffer.write(&r, &vfs, OutFormat::StdErr).unwrap(); 41 - }); 42 - 43 - let stripped = strip_ansi_escapes::strip(&buffer).unwrap(); 44 - let out = std::str::from_utf8(&stripped).unwrap(); 45 - insta::assert_snapshot!(&out); 32 + test_lint(&$sess, file_path, contents); 46 33 } 47 34 48 35 #[test] 49 36 fn [<$tname _fix>](){ 50 37 let file_path = concat!("tests/data/", stringify!($tname), ".nix"); 51 - 52 - let output = std::process::Command::new("cargo") 53 - .arg("run") 54 - .arg("fix") 55 - .arg("-d") 56 - .arg(file_path) 57 - .output() 58 - .expect("command runs successfully"); 59 - 60 - let stdout = String::from_utf8(output.stdout).expect("output is valid utf8"); 61 - 62 - insta::assert_snapshot!(&stdout); 38 + test_fix(file_path); 63 39 } 64 40 } 65 41 66 42 }; 67 43 } 44 + } 45 + 46 + fn test_lint(session: &SessionInfo, file_path: impl AsRef<Path>, contents: &str) { 47 + use statix::{config::OutFormat, lint, traits::WriteDiagnostic}; 48 + use vfs::ReadOnlyVfs; 49 + 50 + let vfs = ReadOnlyVfs::singleton(file_path, contents.as_bytes()); 51 + 52 + let mut buffer = Vec::new(); 53 + vfs.iter() 54 + .map(|entry| lint::lint(&entry, session)) 55 + .for_each(|r| { 56 + buffer.write(&r, &vfs, OutFormat::StdErr).unwrap(); 57 + }); 58 + 59 + let stripped = strip_ansi_escapes::strip(&buffer).unwrap(); 60 + let out = std::str::from_utf8(&stripped).unwrap(); 61 + insta::assert_snapshot!(&out); 62 + } 63 + fn test_fix(file_path: &str) { 64 + let output = std::process::Command::new("cargo") 65 + .arg("run") 66 + .arg("fix") 67 + .arg("-d") 68 + .arg(file_path) 69 + .output() 70 + .expect("command runs successfully"); 71 + 72 + let stdout = String::from_utf8(output.stdout).expect("output is valid utf8"); 73 + 74 + insta::assert_snapshot!(&stdout); 68 75 } 69 76 70 77 test_lint! {