Skip to content

Commit

Permalink
add formatter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
b1ek committed May 29, 2024
1 parent ad5a351 commit 8432007
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/tests/formatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::{env, fs::{self, Permissions}, os::unix::fs::PermissionsExt};

use crate::modules::formatter::BashFormatter;

fn create_fake_binary(fmt: BashFormatter) {
let body = if cfg!(unix) {
"#!/usr/bin/env bash\nexit 0"
} else {
panic!("this test is not available for non-unix platforms")
};

let name: String = fmt.as_cmd();

fs::write(&name, body).expect("Couldn't write fake script");
fs::set_permissions(&name, Permissions::from_mode(0o755)).expect("Couldn't set perms for fake script");
}

#[test]
fn all_exist() {
let path = env::var("PATH").expect("Cannot get $PATH");

env::set_var("PATH", format!("{path}:./")); // temporary unset to ensure that shfmt exists in $PATH
let fmts = BashFormatter::get_all();
for fmt in fmts {
create_fake_binary(fmt);
assert_eq!(fmt.is_available(), true);
assert_eq!(BashFormatter::get_available().is_some(), true);
fs::remove_file(fmt.as_cmd::<String>()).expect("Couldn't remove formatter's fake binary");
}

env::set_var("PATH", &path);
assert_eq!(env::var("PATH").expect("Cannot get $PATH"), path);
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod formatter;
pub mod validity;

0 comments on commit 8432007

Please sign in to comment.