mirror of
https://github.com/numtide/treefmt.git
synced 2024-08-16 20:30:25 +03:00
Always expanding the path given in the treefmt.toml (#107)
* add cabal-fmt formatter * always expand the path provided in treefmt.toml * adding */ format in expanding path for string * adding unit test for expand_if_path function * Update src/lib.rs * revert treefmt.toml Co-authored-by: Jonas Chevalier <zimbatm@zimbatm.com>
This commit is contained in:
parent
29d7a8b225
commit
ca59a61d0c
@ -17,6 +17,7 @@ packages = [
|
||||
"elmPackages.elm-format",
|
||||
"go",
|
||||
"haskellPackages.ormolu",
|
||||
"haskellPackages.cabal-fmt",
|
||||
"nixpkgs-fmt",
|
||||
"nodePackages.prettier",
|
||||
"python3.pkgs.black",
|
||||
|
4
examples/haskell/Foo.hs
Normal file
4
examples/haskell/Foo.hs
Normal file
@ -0,0 +1,4 @@
|
||||
module Foo where
|
||||
|
||||
foo :: IO ()
|
||||
foo = putStrLn "Hello, Riyan!"
|
4
examples/haskell/Nested/Foo.hs
Normal file
4
examples/haskell/Nested/Foo.hs
Normal file
@ -0,0 +1,4 @@
|
||||
module Nested.Foo where
|
||||
|
||||
foo :: IO ()
|
||||
foo = putStrLn "Hello, Riyan!"
|
10
examples/haskell/treefmt.toml
Normal file
10
examples/haskell/treefmt.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[formatter.haskell]
|
||||
command = "ormolu"
|
||||
options = [
|
||||
"--ghc-opt", "-XBangPatterns",
|
||||
"--ghc-opt", "-XPatternSynonyms",
|
||||
"--ghc-opt", "-XTypeApplications",
|
||||
"--mode", "inplace",
|
||||
"--check-idempotence",
|
||||
]
|
||||
includes = ["Foo.hs"]
|
78
src/lib.rs
78
src/lib.rs
@ -57,6 +57,82 @@ pub fn expand_if_path(str: String, reference: &Path) -> String {
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
} else {
|
||||
str
|
||||
format!("*/{}",str)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{path::Path};
|
||||
use globset::{GlobBuilder, GlobSetBuilder};
|
||||
use super::expand_if_path;
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_expand_if_path_single_pattern() {
|
||||
let path = vec![
|
||||
"/Foo.hs",
|
||||
"/nested/Foo.hs",
|
||||
"/nested/nested_again/Foo.hs",
|
||||
"/different_folder/Foo.hs",
|
||||
"/nested/different_folder/Foo.hs",
|
||||
];
|
||||
let pattern = "Foo.hs";
|
||||
let mut sum = GlobSetBuilder::new();
|
||||
let tree_root = Path::new("/");
|
||||
let pat = expand_if_path(pattern.to_string(), &tree_root);
|
||||
let glob = GlobBuilder::new(&pat).build().unwrap();
|
||||
sum.add(glob);
|
||||
let result = sum.build().unwrap();
|
||||
let test = path.clone().into_iter().filter(|p| result.is_match(p)).collect::<Vec<&str>>();
|
||||
|
||||
assert_eq!(path, test);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expand_if_path_wildcard() {
|
||||
let path = vec![
|
||||
"/Foo.hs",
|
||||
"/nested/Foo.hs",
|
||||
"/nested/nested_again/Foo.hs",
|
||||
"/different_folder/Foo.hs",
|
||||
"/nested/different_folder/Foo.hs",
|
||||
"/nested/Bar.hs",
|
||||
"/nested/different_folder/Bar.hs"
|
||||
];
|
||||
|
||||
let pattern = "*.hs";
|
||||
let mut sum = GlobSetBuilder::new();
|
||||
let tree_root = Path::new("/");
|
||||
let pat = expand_if_path(pattern.to_string(), &tree_root);
|
||||
let glob = GlobBuilder::new(&pat).build().unwrap();
|
||||
sum.add(glob);
|
||||
let result = sum.build().unwrap();
|
||||
let test = path.clone().into_iter().filter(|p| result.is_match(p)).collect::<Vec<&str>>();
|
||||
|
||||
assert_eq!(path, test);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expand_if_path_single_pattern_with_slash() {
|
||||
let path = vec![
|
||||
"/Foo.hs",
|
||||
"/nested/Foo.hs",
|
||||
"/nested/nested/Foo.hs",
|
||||
"/different_folder/Foo.hs",
|
||||
"/nested/different_folder/Foo.hs",
|
||||
"/nested/Bar.hs",
|
||||
"/nested/different_folder/Bar.hs"
|
||||
];
|
||||
let pattern = "nested/Foo.hs";
|
||||
let mut sum = GlobSetBuilder::new();
|
||||
let tree_root = Path::new("/");
|
||||
let pat = expand_if_path(pattern.to_string(), &tree_root);
|
||||
let glob = GlobBuilder::new(&pat).build().unwrap();
|
||||
sum.add(glob);
|
||||
let result = sum.build().unwrap();
|
||||
let test = path.into_iter().filter(|p| result.is_match(p)).collect::<Vec<&str>>();
|
||||
|
||||
assert_eq!(vec!["/nested/Foo.hs"], test);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user