From 77f0389a9e812430e5757f5967b5265c9abebe83 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 21 Jun 2023 14:17:19 -0500 Subject: [PATCH 1/2] test(config): Reproduce types bug --- .../tests/cmd/override-default-type.in/_typos.toml | 11 +++++++++++ .../tests/cmd/override-default-type.in/key.asc | 1 + crates/typos-cli/tests/cmd/override-default-type.toml | 11 +++++++++++ 3 files changed, 23 insertions(+) create mode 100644 crates/typos-cli/tests/cmd/override-default-type.in/_typos.toml create mode 100644 crates/typos-cli/tests/cmd/override-default-type.in/key.asc create mode 100644 crates/typos-cli/tests/cmd/override-default-type.toml diff --git a/crates/typos-cli/tests/cmd/override-default-type.in/_typos.toml b/crates/typos-cli/tests/cmd/override-default-type.in/_typos.toml new file mode 100644 index 0000000..9128c76 --- /dev/null +++ b/crates/typos-cli/tests/cmd/override-default-type.in/_typos.toml @@ -0,0 +1,11 @@ +[type.asc] # GPG keys +extend-glob = ["*.asc"] +check-file = false + +[default.extend-words] +foo = "bar" + +[files] +extend-exclude = [ + "_typos.toml" +] diff --git a/crates/typos-cli/tests/cmd/override-default-type.in/key.asc b/crates/typos-cli/tests/cmd/override-default-type.in/key.asc new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/crates/typos-cli/tests/cmd/override-default-type.in/key.asc @@ -0,0 +1 @@ +foo diff --git a/crates/typos-cli/tests/cmd/override-default-type.toml b/crates/typos-cli/tests/cmd/override-default-type.toml new file mode 100644 index 0000000..90ff659 --- /dev/null +++ b/crates/typos-cli/tests/cmd/override-default-type.toml @@ -0,0 +1,11 @@ +bin.name = "typos" +args = "--file-types" +status.code = 0 +stdin = ''' +\n\n +Destory +''' +stdout = """ +./key.asc:asciidoc +""" +stderr = "" From fce92e4e5f3e943842549e93beb049446daf83df Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 21 Jun 2023 14:31:04 -0500 Subject: [PATCH 2/2] fix(config): User file types override default file types This also ensures `typos --type-list` will report the glob in only one place. Fixes #754 --- crates/typos-cli/src/file_type.rs | 30 ++++++++++++++----- .../tests/cmd/override-default-type.toml | 2 +- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/typos-cli/src/file_type.rs b/crates/typos-cli/src/file_type.rs index 4b044bc..be08999 100644 --- a/crates/typos-cli/src/file_type.rs +++ b/crates/typos-cli/src/file_type.rs @@ -4,7 +4,7 @@ use kstring::KString; #[derive(Default, Clone, Debug)] pub struct TypesBuilder { - definitions: BTreeMap>, + definitions: BTreeMap>, } impl TypesBuilder { @@ -18,7 +18,7 @@ impl TypesBuilder { .iter() .map(|(name, glob)| { let name = KString::from(*name); - let globs = glob.iter().map(|s| KString::from(*s)).collect(); + let globs = glob.iter().map(|s| (KString::from(*s), 0)).collect(); (name, globs) }), ); @@ -31,7 +31,11 @@ impl TypesBuilder { pub fn add(&mut self, name: impl Into, glob: impl Into) { let name = name.into(); let glob = glob.into(); - self.definitions.entry(name).or_default().push(glob); + let weight = self.definitions.len(); + self.definitions + .entry(name) + .or_default() + .push((glob, weight)); } pub fn build(self) -> Result { @@ -39,17 +43,29 @@ impl TypesBuilder { .definitions .iter() .flat_map(|(name, globs)| { - globs.iter().map(move |glob| { + globs.iter().map(move |(glob, weight)| { let sort = sort_key(glob); - (sort, name, glob) + (sort, weight, name, glob) }) }) .collect::>(); definitions.sort(); + let rev_definitions = definitions + .iter() + .map(|(_, _, name, glob)| (*glob, *name)) + .collect::>(); + let mut unique_definitions = BTreeMap::>::new(); + for (glob, name) in rev_definitions { + unique_definitions + .entry(name.clone()) + .or_default() + .push(glob.clone()); + } + let mut glob_to_name = Vec::new(); let mut build_set = globset::GlobSetBuilder::new(); - for (_, name, glob) in definitions { + for (_, _, name, glob) in definitions { glob_to_name.push(name.clone()); build_set.add( globset::GlobBuilder::new(glob) @@ -60,7 +76,7 @@ impl TypesBuilder { let set = build_set.build()?; Ok(Types { - definitions: self.definitions, + definitions: unique_definitions, glob_to_name, set, matches: std::sync::Arc::new(thread_local::ThreadLocal::default()), diff --git a/crates/typos-cli/tests/cmd/override-default-type.toml b/crates/typos-cli/tests/cmd/override-default-type.toml index 90ff659..64d4293 100644 --- a/crates/typos-cli/tests/cmd/override-default-type.toml +++ b/crates/typos-cli/tests/cmd/override-default-type.toml @@ -6,6 +6,6 @@ stdin = ''' Destory ''' stdout = """ -./key.asc:asciidoc +./key.asc:asc """ stderr = ""