Skip to content

Commit e3c191e

Browse files
author
Ed Page
committed
fix(cli): Display shortened paths to users
Before, we always displayed absolute paths and now we'll display relative ones. The main issue was loading the config correctly. We just have to cannonicalize whenever doing so.
1 parent 7c6b85c commit e3c191e

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/bin/typos-cli/main.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,14 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
6565
let global_cwd = std::env::current_dir()?;
6666

6767
let path = &args.path[0];
68-
let path = if path == std::path::Path::new("-") {
69-
path.to_owned()
70-
} else {
71-
path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?
72-
};
7368
let cwd = if path == std::path::Path::new("-") {
7469
global_cwd.as_path()
7570
} else if path.is_file() {
7671
path.parent().unwrap()
7772
} else {
7873
path.as_path()
7974
};
75+
let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?;
8076

8177
let storage = typos_cli::policy::ConfigStorage::new();
8278
let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
@@ -92,7 +88,7 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
9288
engine.set_overrides(overrides);
9389

9490
let config = engine
95-
.load_config(cwd)
91+
.load_config(&cwd)
9692
.with_code(proc_exit::Code::CONFIG_ERR)?;
9793

9894
let mut defaulted_config = typos_cli::config::Config::from_defaults();
@@ -111,18 +107,14 @@ fn run_type_list(args: &args::Args) -> proc_exit::ExitResult {
111107
let global_cwd = std::env::current_dir()?;
112108

113109
let path = &args.path[0];
114-
let path = if path == std::path::Path::new("-") {
115-
path.to_owned()
116-
} else {
117-
path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?
118-
};
119110
let cwd = if path == std::path::Path::new("-") {
120111
global_cwd.as_path()
121112
} else if path.is_file() {
122113
path.parent().unwrap()
123114
} else {
124115
path.as_path()
125116
};
117+
let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?;
126118

127119
let storage = typos_cli::policy::ConfigStorage::new();
128120
let mut engine = typos_cli::policy::ConfigEngine::new(&storage);
@@ -138,9 +130,9 @@ fn run_type_list(args: &args::Args) -> proc_exit::ExitResult {
138130
engine.set_overrides(overrides);
139131

140132
engine
141-
.init_dir(cwd)
133+
.init_dir(&cwd)
142134
.with_code(proc_exit::Code::CONFIG_ERR)?;
143-
let definitions = engine.file_types(cwd);
135+
let definitions = engine.file_types(&cwd);
144136

145137
let stdout = std::io::stdout();
146138
let mut handle = stdout.lock();
@@ -179,23 +171,19 @@ fn run_checks(
179171
let mut typos_found = false;
180172
let mut errors_found = false;
181173
for path in args.path.iter() {
182-
let path = if path == std::path::Path::new("-") {
183-
path.to_owned()
184-
} else {
185-
path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?
186-
};
187174
let cwd = if path == std::path::Path::new("-") {
188175
global_cwd.as_path()
189176
} else if path.is_file() {
190177
path.parent().unwrap()
191178
} else {
192179
path.as_path()
193180
};
181+
let cwd = cwd.canonicalize().with_code(proc_exit::Code::USAGE_ERR)?;
194182

195183
engine
196-
.init_dir(cwd)
184+
.init_dir(&cwd)
197185
.with_code(proc_exit::Code::CONFIG_ERR)?;
198-
let walk_policy = engine.walk(cwd);
186+
let walk_policy = engine.walk(&cwd);
199187

200188
let threads = if path.is_file() { 1 } else { args.threads };
201189
let single_threaded = threads == 1;

src/file.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,14 @@ fn walk_entry(
598598
};
599599
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
600600
let explicit = entry.depth() == 0;
601-
let path = if entry.is_stdin() {
602-
std::path::Path::new("-")
601+
let (path, lookup_path) = if entry.is_stdin() {
602+
let path = std::path::Path::new("-");
603+
(path, path.to_owned())
603604
} else {
604-
entry.path()
605+
let path = entry.path();
606+
(path, path.canonicalize()?)
605607
};
606-
let policy = engine.policy(path);
608+
let policy = engine.policy(&lookup_path);
607609
checks.check_file(path, explicit, &policy, reporter)?;
608610
}
609611

src/policy.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl<'s> ConfigEngine<'s> {
6868
}
6969

7070
pub fn walk(&self, cwd: &std::path::Path) -> &crate::config::Walk {
71+
debug_assert!(cwd.is_absolute(), "{} is not absolute", cwd.display());
7172
let dir = self
7273
.configs
7374
.get(cwd)
@@ -76,6 +77,7 @@ impl<'s> ConfigEngine<'s> {
7677
}
7778

7879
pub fn file_types(&self, cwd: &std::path::Path) -> &[ignore::types::FileTypeDef] {
80+
debug_assert!(cwd.is_absolute(), "{} is not absolute", cwd.display());
7981
let dir = self
8082
.configs
8183
.get(cwd)
@@ -84,6 +86,7 @@ impl<'s> ConfigEngine<'s> {
8486
}
8587

8688
pub fn policy(&self, path: &std::path::Path) -> Policy<'_, '_> {
89+
debug_assert!(path.is_absolute(), "{} is not absolute", path.display());
8790
let dir = self.get_dir(path).expect("`walk()` should be called first");
8891
let file_config = dir.get_file_config(path);
8992
Policy {
@@ -120,6 +123,7 @@ impl<'s> ConfigEngine<'s> {
120123
&self,
121124
cwd: &std::path::Path,
122125
) -> Result<crate::config::Config, anyhow::Error> {
126+
debug_assert!(cwd.is_absolute(), "{} is not absolute", cwd.display());
123127
let mut config = crate::config::Config::default();
124128

125129
if !self.isolated {
@@ -157,6 +161,7 @@ impl<'s> ConfigEngine<'s> {
157161
}
158162

159163
pub fn init_dir(&mut self, cwd: &std::path::Path) -> Result<(), anyhow::Error> {
164+
debug_assert!(cwd.is_absolute(), "{} is not absolute", cwd.display());
160165
if self.configs.contains_key(cwd) {
161166
return Ok(());
162167
}
@@ -380,7 +385,7 @@ mod test {
380385
};
381386
engine.set_overrides(config);
382387

383-
let cwd = std::path::Path::new(".");
388+
let cwd = std::path::Path::new(".").canonicalize().unwrap();
384389
let loaded = engine.load_config(&cwd).unwrap();
385390
assert_eq!(loaded.default.binary, Some(false));
386391
assert_eq!(loaded.default.check_filename, Some(true));
@@ -414,7 +419,7 @@ mod test {
414419
};
415420
engine.set_overrides(config);
416421

417-
let cwd = std::path::Path::new(".");
422+
let cwd = std::path::Path::new(".").canonicalize().unwrap();
418423
let result = engine.init_dir(&cwd);
419424
assert!(result.is_err());
420425
}
@@ -428,7 +433,7 @@ mod test {
428433
let config = crate::config::Config::default();
429434
engine.set_overrides(config);
430435

431-
let cwd = std::path::Path::new(".");
436+
let cwd = std::path::Path::new(".").canonicalize().unwrap();
432437
engine.init_dir(&cwd).unwrap();
433438
let policy = engine.policy(&cwd.join("Cargo.toml"));
434439
assert!(!policy.binary);
@@ -460,7 +465,7 @@ mod test {
460465
};
461466
engine.set_overrides(config);
462467

463-
let cwd = std::path::Path::new(".");
468+
let cwd = std::path::Path::new(".").canonicalize().unwrap();
464469
engine.init_dir(&cwd).unwrap();
465470
let policy = engine.policy(&cwd.join("Cargo.toml"));
466471
assert!(policy.binary);
@@ -492,7 +497,7 @@ mod test {
492497
};
493498
engine.set_overrides(config);
494499

495-
let cwd = std::path::Path::new(".");
500+
let cwd = std::path::Path::new(".").canonicalize().unwrap();
496501
engine.init_dir(&cwd).unwrap();
497502
let policy = engine.policy(&cwd.join("Cargo.toml"));
498503
assert!(policy.binary);

0 commit comments

Comments
 (0)