Skip to content

Commit ce9062a

Browse files
authored
Don't write justfiles unchanged by formatting (#2479)
1 parent beb275a commit ce9062a

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

Diff for: src/subcommand.rs

+31-31
Original file line numberDiff line numberDiff line change
@@ -310,44 +310,44 @@ impl Subcommand {
310310

311311
let formatted = ast.to_string();
312312

313+
if formatted == src {
314+
return Ok(());
315+
}
316+
313317
if config.check {
314-
return if formatted == src {
315-
Ok(())
316-
} else {
317-
if !config.verbosity.quiet() {
318-
use similar::{ChangeTag, TextDiff};
319-
320-
let diff = TextDiff::configure()
321-
.algorithm(similar::Algorithm::Patience)
322-
.diff_lines(src, &formatted);
323-
324-
for op in diff.ops() {
325-
for change in diff.iter_changes(op) {
326-
let (symbol, color) = match change.tag() {
327-
ChangeTag::Delete => ("-", config.color.stdout().diff_deleted()),
328-
ChangeTag::Equal => (" ", config.color.stdout()),
329-
ChangeTag::Insert => ("+", config.color.stdout().diff_added()),
330-
};
331-
332-
print!("{}{symbol}{change}{}", color.prefix(), color.suffix());
333-
}
318+
if !config.verbosity.quiet() {
319+
use similar::{ChangeTag, TextDiff};
320+
321+
let diff = TextDiff::configure()
322+
.algorithm(similar::Algorithm::Patience)
323+
.diff_lines(src, &formatted);
324+
325+
for op in diff.ops() {
326+
for change in diff.iter_changes(op) {
327+
let (symbol, color) = match change.tag() {
328+
ChangeTag::Delete => ("-", config.color.stdout().diff_deleted()),
329+
ChangeTag::Equal => (" ", config.color.stdout()),
330+
ChangeTag::Insert => ("+", config.color.stdout().diff_added()),
331+
};
332+
333+
print!("{}{symbol}{change}{}", color.prefix(), color.suffix());
334334
}
335335
}
336+
}
336337

337-
Err(Error::FormatCheckFoundDiff)
338-
};
339-
}
338+
Err(Error::FormatCheckFoundDiff)
339+
} else {
340+
fs::write(&search.justfile, formatted).map_err(|io_error| Error::WriteJustfile {
341+
justfile: search.justfile.clone(),
342+
io_error,
343+
})?;
340344

341-
fs::write(&search.justfile, formatted).map_err(|io_error| Error::WriteJustfile {
342-
justfile: search.justfile.clone(),
343-
io_error,
344-
})?;
345+
if config.verbosity.loud() {
346+
eprintln!("Wrote justfile to `{}`", search.justfile.display());
347+
}
345348

346-
if config.verbosity.loud() {
347-
eprintln!("Wrote justfile to `{}`", search.justfile.display());
349+
Ok(())
348350
}
349-
350-
Ok(())
351351
}
352352

353353
fn init(config: &Config) -> RunResult<'static> {

Diff for: tests/fmt.rs renamed to tests/format.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1120,3 +1120,21 @@ fn doc_attribute_suppresses_comment() {
11201120
)
11211121
.run();
11221122
}
1123+
1124+
#[test]
1125+
fn unchanged_justfiles_are_not_written_to_disk() {
1126+
let tmp = tempdir();
1127+
1128+
let justfile = tmp.path().join("justfile");
1129+
1130+
fs::write(&justfile, "").unwrap();
1131+
1132+
let mut permissions = fs::metadata(&justfile).unwrap().permissions();
1133+
permissions.set_readonly(true);
1134+
fs::set_permissions(&justfile, permissions).unwrap();
1135+
1136+
Test::with_tempdir(tmp)
1137+
.no_justfile()
1138+
.args(["--fmt", "--unstable"])
1139+
.run();
1140+
}

Diff for: tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod examples;
5959
mod explain;
6060
mod export;
6161
mod fallback;
62-
mod fmt;
62+
mod format;
6363
mod functions;
6464
#[cfg(unix)]
6565
mod global;

Diff for: tests/unstable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::*;
44
fn set_unstable_true_with_env_var() {
55
for val in ["true", "some-arbitrary-string"] {
66
Test::new()
7-
.justfile("")
7+
.justfile("# hello")
88
.args(["--fmt"])
99
.env("JUST_UNSTABLE", val)
1010
.status(EXIT_SUCCESS)

0 commit comments

Comments
 (0)