Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/mq-formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ miette = {workspace = true, features = ["fancy"]}
mq-lang = {workspace = true, features = ["cst"]}

[dev-dependencies]
assert_cmd = {workspace = true}
divan = {workspace = true}
rstest = {workspace = true}

Expand Down
8 changes: 7 additions & 1 deletion crates/mq-formatter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ curl -sSL https://mqlang.org/install_fmt.sh | bash
```

The installer will:
- Download the latest `mq-crawl` binary for your platform
- Download the latest `mq-fmt` binary for your platform
- Install it to `~/.local/bin/`
- Verify the checksum of the downloaded binary
- Update your shell profile to add `mq-fmt` to your PATH
Expand Down Expand Up @@ -56,6 +56,12 @@ mq-fmt --sort-imports --sort-functions file.mq

# Wrap long `|`-chained pipelines at 80 columns
mq-fmt --max-width 80 file.mq

# Read from stdin and write the formatted result to stdout
cat file.mq | mq-fmt -

# Check formatting of stdin without writing anything (exits with non-zero if unformatted)
cat file.mq | mq-fmt --check -
```

### Via mq
Expand Down
24 changes: 23 additions & 1 deletion crates/mq-formatter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
use glob::glob;
use miette::IntoDiagnostic;
use miette::miette;
use std::io::{Read, Write};
use std::{fs, path::PathBuf};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -43,7 +44,8 @@ struct Cli {
#[arg(long, default_value_t = false)]
sort_fields: bool,

/// Path to the mq file(s) to format
/// Path to the mq file(s) to format. Pass `-` to read from stdin and write
/// the formatted result to stdout instead of modifying files on disk.
files: Option<Vec<PathBuf>>,
}

Expand All @@ -58,6 +60,26 @@ fn main() -> miette::Result<()> {
max_width: cli.max_width,
}));

if let Some(files) = &cli.files
&& files.len() == 1
&& files[0].as_os_str() == "-"
{
let mut content = String::new();
std::io::stdin().read_to_string(&mut content).into_diagnostic()?;

let formatted = formatter.format(&content).map_err(|e| miette!("<stdin>: {e}"))?;

if cli.check {
return if formatted != content {
Err(miette!("The input is not formatted: <stdin>"))
} else {
Ok(())
};
}

return std::io::stdout().write_all(formatted.as_bytes()).into_diagnostic();
}

let files = match cli.files {
Some(f) => f,
None => glob("./**/*.mq")
Expand Down
42 changes: 42 additions & 0 deletions crates/mq-formatter/tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use assert_cmd::cargo;

#[test]
fn test_stdin_stdout_formats_and_prints_to_stdout() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = cargo::cargo_bin_cmd!("mq-fmt");

cmd.arg("-")
.write_stdin("def foo():1;")
.assert()
.success()
.stdout("def foo(): 1;");

Ok(())
}

#[test]
fn test_stdin_check_passes_when_already_formatted() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = cargo::cargo_bin_cmd!("mq-fmt");

cmd.arg("--check")
.arg("-")
.write_stdin("def foo(): 1;\n")
.assert()
.success()
.stdout("");

Ok(())
}

#[test]
fn test_stdin_check_fails_when_not_formatted() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = cargo::cargo_bin_cmd!("mq-fmt");

cmd.arg("--check")
.arg("-")
.write_stdin("def foo():1;")
.assert()
.failure()
.code(1);

Ok(())
}
Loading