Skip to content

Commit

Permalink
fake-formatter: add --uppercase and --append formatting behaviors
Browse files Browse the repository at this point in the history
This allows tests to easily distinguish the effects of multiple formatters that
could be applied during a single `jj fix` command, which is helpful for testing
the feature for configuring multiple formatters on potentially overlapping
filesets.

Uppercase is a counterpart to lowercase. Append exposes the number of changes
and their order of execution in the file content, which provides a terse way of
writing test expectations.
  • Loading branch information
hooper committed Jul 25, 2024
1 parent 5a19852 commit 8fe2274
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions cli/testing/fake-formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ struct Args {
#[arg(long, default_value_t = false)]
uppercase: bool,

/// Convert all characters to lowercase when reading stdin.
#[arg(long, default_value_t = false)]
lowercase: bool,

/// Adds a line to the end of the file
#[arg(long)]
append: Option<String>,

/// Write this string to stdout, and ignore stdin.
#[arg(long)]
stdout: Option<String>,
Expand All @@ -64,9 +72,14 @@ fn main() -> ExitCode {
eprint!("{}", data);
}
let stdout = if let Some(data) = args.stdout {
data // --reverse doesn't apply to --stdout.
// Other content-altering flags don't apply to --stdout.
assert!(!args.reverse);
assert!(!args.uppercase);
assert!(!args.lowercase);
assert!(args.append.is_none());
data
} else {
std::io::stdin()
let mut stdout = std::io::stdin()
.lines()
.map(|line| {
format!("{}\n", {
Expand All @@ -76,13 +89,21 @@ fn main() -> ExitCode {
line.unwrap()
};
if args.uppercase {
assert!(!args.lowercase);
line.to_uppercase()
} else if args.lowercase {
assert!(!args.uppercase);
line.to_lowercase()
} else {
line
}
})
})
.join("")
.join("");
if let Some(line) = args.append {
stdout.push_str(&line);
}
stdout
};
print!("{}", stdout);
if let Some(path) = args.tee {
Expand Down

0 comments on commit 8fe2274

Please sign in to comment.