Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't write justfiles unchanged by formatting to disk #2479

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,44 +310,44 @@ impl Subcommand {

let formatted = ast.to_string();

if formatted == src {
return Ok(());
}

if config.check {
return if formatted == src {
Ok(())
} else {
if !config.verbosity.quiet() {
use similar::{ChangeTag, TextDiff};

let diff = TextDiff::configure()
.algorithm(similar::Algorithm::Patience)
.diff_lines(src, &formatted);

for op in diff.ops() {
for change in diff.iter_changes(op) {
let (symbol, color) = match change.tag() {
ChangeTag::Delete => ("-", config.color.stdout().diff_deleted()),
ChangeTag::Equal => (" ", config.color.stdout()),
ChangeTag::Insert => ("+", config.color.stdout().diff_added()),
};

print!("{}{symbol}{change}{}", color.prefix(), color.suffix());
}
if !config.verbosity.quiet() {
use similar::{ChangeTag, TextDiff};

let diff = TextDiff::configure()
.algorithm(similar::Algorithm::Patience)
.diff_lines(src, &formatted);

for op in diff.ops() {
for change in diff.iter_changes(op) {
let (symbol, color) = match change.tag() {
ChangeTag::Delete => ("-", config.color.stdout().diff_deleted()),
ChangeTag::Equal => (" ", config.color.stdout()),
ChangeTag::Insert => ("+", config.color.stdout().diff_added()),
};

print!("{}{symbol}{change}{}", color.prefix(), color.suffix());
}
}
}

Err(Error::FormatCheckFoundDiff)
};
}
Err(Error::FormatCheckFoundDiff)
} else {
fs::write(&search.justfile, formatted).map_err(|io_error| Error::WriteJustfile {
justfile: search.justfile.clone(),
io_error,
})?;

fs::write(&search.justfile, formatted).map_err(|io_error| Error::WriteJustfile {
justfile: search.justfile.clone(),
io_error,
})?;
if config.verbosity.loud() {
eprintln!("Wrote justfile to `{}`", search.justfile.display());
}

if config.verbosity.loud() {
eprintln!("Wrote justfile to `{}`", search.justfile.display());
Ok(())
}

Ok(())
}

fn init(config: &Config) -> RunResult<'static> {
Expand Down
18 changes: 18 additions & 0 deletions tests/fmt.rs → tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,3 +1120,21 @@ fn doc_attribute_suppresses_comment() {
)
.run();
}

#[test]
fn unchanged_justfiles_are_not_written_to_disk() {
let tmp = tempdir();

let justfile = tmp.path().join("justfile");

fs::write(&justfile, "").unwrap();

let mut permissions = fs::metadata(&justfile).unwrap().permissions();
permissions.set_readonly(true);
fs::set_permissions(&justfile, permissions).unwrap();

Test::with_tempdir(tmp)
.no_justfile()
.args(["--fmt", "--unstable"])
.run();
}
2 changes: 1 addition & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod examples;
mod explain;
mod export;
mod fallback;
mod fmt;
mod format;
mod functions;
#[cfg(unix)]
mod global;
Expand Down
2 changes: 1 addition & 1 deletion tests/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
fn set_unstable_true_with_env_var() {
for val in ["true", "some-arbitrary-string"] {
Test::new()
.justfile("")
.justfile("# hello")
.args(["--fmt"])
.env("JUST_UNSTABLE", val)
.status(EXIT_SUCCESS)
Expand Down