diff --git a/README.md b/README.md index e643e2df..a40c1683 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ yamark format config.yaml docs/ Format the current directory: ```sh -yamark format +yamark format . ``` Check whether files are already formatted without writing changes: diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index beca0803..34458981 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,7 @@ +To format the current directory in place, use `yamark format .`. Running `yamark format` by itself now shows help. In-place writes require a path, even when the directory contains a `yamark.toml`. To check or preview changes, use `yamark format --check .` or `yamark format --diff .`. Both still default to the current directory when the path is omitted. + Use `exclude` in the `[format]` table of `yamark.toml` to keep files out of Yamark formatting. Its gitignore-style patterns are relative to the config directory. Unlike `.gitignore` and `.ignore`, which affect only directory traversal, `exclude` also applies to explicit paths, `--stdin-file-path`, and Git clean and smudge filters. Excluded file paths are counted as skipped, and stdin for an excluded path passes through unchanged. This also applies to editor Format Document and format-on-save; a configured next formatter still runs. `[paths]` overrides now match a `--stdin-file-path` that does not exist on disk on Windows, or when the path passes through a symlinked directory. Previously these paths could fail to match the config directory. diff --git a/external-tests/cli/test_help.py b/external-tests/cli/test_help.py index cd8dd83e..9702fd9b 100644 --- a/external-tests/cli/test_help.py +++ b/external-tests/cli/test_help.py @@ -7,6 +7,11 @@ from _support import decode_output, run_cli_case +def test_bare_format_matches_explicit_help() -> None: + help_output = run_cli_case("yamark format --help").stdout + assert run_cli_case("yamark format").stdout == help_output + + def test_debug_format_help_shows_verify_and_omits_compat_flag() -> None: result = run_cli_case("yamark format --help", stderr="") stdout = decode_output(result.stdout) diff --git a/src/cli.rs b/src/cli.rs index 7a3e0d73..75ed5336 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -79,7 +79,11 @@ enum Command { compact: bool, #[arg(long)] skip_embedded_formatters: bool, - #[arg(value_name = "PATHS")] + #[arg( + value_name = "PATHS", + required_unless_present_any = ["check", "diff", "stdin_file_path"], + help = "Files or directories to format in place. Use '.' for the current directory" + )] paths: Vec, }, #[command(about = "Convert JSON-family input from stdin to formatted YAML")] @@ -131,7 +135,10 @@ pub fn run(args: I) -> ExitCode where I: IntoIterator, { - let args = args.into_iter().collect::>(); + let mut args = args.into_iter().collect::>(); + if matches!(args.as_slice(), [_, subcommand] if subcommand == "format") { + args.push(OsString::from("--help")); + } if let Some(path) = default_format_path(&args) { return run_format( false, diff --git a/tests/cases/README.md b/tests/cases/README.md index 97fa0edd..119fbb7d 100644 --- a/tests/cases/README.md +++ b/tests/cases/README.md @@ -1,6 +1,6 @@ # CLI case files -Each `.case` file is executed through the compiled `yamark` binary. The format is: +Each `.case` file runs the compiled `yamark` binary with `NO_COLOR=1`. The format is: ```text -- args @@ -15,6 +15,8 @@ expected stderr 0 ``` +When output varies by platform or build profile, use `-- stdout-template` or `-- stderr-template` to record a JSON array of lines. Each quoted line preserves its trailing spaces and ends with a newline. Within these sections, `{{exe}}` stands for the Yamark binary's basename, `{{sep}}` for the native path separator, and a `{{debug-only}}` prefix includes the line only in builds with debug assertions. Ordinary `-- stdout` and `-- stderr` treat these markers literally. + The test harness intentionally avoids calling library internals. This keeps the public CLI contract readable and prevents implementation details from becoming part of the test API. Feature-specific Markdown decision transcripts live in `cases/format-trace/` and are run by `cargo test --features format-trace --test format_trace`. They use the same exact invocation/input/output/status sections; keeping them separate lets the ordinary transcript suite run without feature-dependent output. They check that retained preservation and unavailable layout plans are reported as skipped, including when the node has a nominal formatting emit variant. diff --git a/tests/cases/format_bare_help.case b/tests/cases/format_bare_help.case new file mode 100644 index 00000000..62b18461 --- /dev/null +++ b/tests/cases/format_bare_help.case @@ -0,0 +1,42 @@ +-- git-fixture +{ + "config": {"core.autocrlf": "false", "core.hooksPath": ".git/no-hooks"}, + "files": { + ".gitignore": "!/docs/\n!/docs/input.md\n", + "docs/input.md": "# Title\n" + } +} +-- args +format +-- stdin +# Piped +-- stdout-template +[ + "Usage: {{exe}} format [OPTIONS] [PATHS]...", + "", + "Arguments:", + " [PATHS]... Files or directories to format in place. Use '.' for the current directory", + "", + "Options:", + " --check ", + " --diff ", + " --diagnostics ", + "{{debug-only}} --verify Reparse changed YAML and reject invalid or value-changing output", + " --stdin-file-path ", + " --config ", + " --wrap [default: 72]", + " --table-widths Table widths: fit contents or preserve source widths [default: fit]", + " --canonical ", + " --preserve-footnotes ", + " --line-width [default: 80]", + " --prose-width [default: 72]", + " --indent-width [default: 2]", + " --compact ", + " --skip-embedded-formatters ", + " -h, --help Print help" +] +-- stderr +-- git-worktree +{"docs/input.md": "# Title\n"} +-- status +0 diff --git a/tests/cases/format_default_check_path.case b/tests/cases/format_default_check_path.case new file mode 100644 index 00000000..20037cb5 --- /dev/null +++ b/tests/cases/format_default_check_path.case @@ -0,0 +1,21 @@ +-- git-fixture +{ + "config": {"core.autocrlf": "false", "core.hooksPath": ".git/no-hooks"}, + "cwd": "work", + "files": { + ".gitignore": "!/work/\n!/work/docs/\n!/work/docs/input.md\n", + "yamark.toml": "", + "work/.ignore": "/.ignore\n!/docs/\n!/docs/input.md\n", + "work/docs/input.md": "# Title\n" + } +} +-- args +format --check +-- stdin +-- stdout +-- stderr +1 files scanned, 1 formatted, 0 unchanged, 0 skipped, 0 failed +-- git-worktree +{"work/docs/input.md": "# Title\n"} +-- status +1 diff --git a/tests/cases/format_default_diff_path.case b/tests/cases/format_default_diff_path.case new file mode 100644 index 00000000..71be04ff --- /dev/null +++ b/tests/cases/format_default_diff_path.case @@ -0,0 +1,28 @@ +-- git-fixture +{ + "config": {"core.autocrlf": "false", "core.hooksPath": ".git/no-hooks"}, + "cwd": "work", + "files": { + ".gitignore": "!/work/\n!/work/docs/\n!/work/docs/input.md\n", + "yamark.toml": "", + "work/.ignore": "/.ignore\n!/docs/\n!/docs/input.md\n", + "work/docs/input.md": "# Title\n" + } +} +-- args +format --diff +-- stdin +-- stdout-template +[ + "--- .{{sep}}docs{{sep}}input.md", + "+++ .{{sep}}docs{{sep}}input.md", + "@@ -1 +1 @@", + "-# Title", + "+# Title" +] +-- stderr +1 files scanned, 1 formatted, 0 unchanged, 0 skipped, 0 failed +-- git-worktree +{"work/docs/input.md": "# Title\n"} +-- status +1 diff --git a/tests/cases/format_missing_path_with_config.case b/tests/cases/format_missing_path_with_config.case new file mode 100644 index 00000000..aaedb977 --- /dev/null +++ b/tests/cases/format_missing_path_with_config.case @@ -0,0 +1,27 @@ +-- git-fixture +{ + "config": {"core.autocrlf": "false", "core.hooksPath": ".git/no-hooks"}, + "files": { + ".gitignore": "!/docs/\n!/docs/input.md\n!/yamark.toml\n", + "yamark.toml": "[format]\nwrap = \"none\"\n", + "docs/input.md": "# Title\n" + } +} +-- args +format --wrap 40 +-- stdin +# Piped +-- stdout +-- stderr-template +[ + "error: the following required arguments were not provided:", + " ...", + "", + "Usage: {{exe}} format --wrap ...", + "", + "For more information, try '--help'." +] +-- git-worktree +{"docs/input.md": "# Title\n"} +-- status +2 diff --git a/tests/cases/markdown_template_bare_basic.case b/tests/cases/markdown_template_bare_basic.case index 8c26669a..a5673b42 100644 --- a/tests/cases/markdown_template_bare_basic.case +++ b/tests/cases/markdown_template_bare_basic.case @@ -4,9 +4,13 @@ format --wrap sentence --stdin-file-path input.md First sentence with {{ foo }}. Second sentence. + +Keep {{exe}} here. -- stdout First sentence with {{ foo }}. Second sentence. + +Keep {{exe}} here. -- stderr -- status 0 diff --git a/tests/cli_cases.rs b/tests/cli_cases.rs index 307682f3..7c9769b0 100644 --- a/tests/cli_cases.rs +++ b/tests/cli_cases.rs @@ -1,4 +1,5 @@ use std::collections::BTreeMap; +use std::fmt::Write; use std::fs; use std::path::{Path, PathBuf}; @@ -55,6 +56,7 @@ fn cli_cases() { } command.args(split_args(&case.args)); command.write_stdin(case.stdin); + command.env("NO_COLOR", "1"); let assert = command.assert().code(case.status); let output = assert.get_output(); assert_eq!( @@ -271,6 +273,8 @@ fn flush(case: &mut Case, section: &str, body: &str) { "stdout" => case.stdout = body.to_owned(), "stderr" => case.stderr = body.to_owned(), "status" => case.status = value.trim().parse().unwrap(), + "stdout-template" => case.stdout = render_output_template(value), + "stderr-template" => case.stderr = render_output_template(value), "git-fixture" => case.git_fixture = Some(serde_json::from_str(value).unwrap()), "git-status" => case.git_status = Some(body.to_owned()), "git-index" => case.git_index = Some(serde_json::from_str(value).unwrap()), @@ -280,6 +284,24 @@ fn flush(case: &mut Case, section: &str, body: &str) { } } +fn render_output_template(json: &str) -> String { + let binary = assert_cmd::cargo::cargo_bin("yamark"); + let name = binary.file_name().unwrap().to_str().unwrap(); + let mut output = String::new(); + for line in serde_json::from_str::>(json).unwrap() { + let line = match line.strip_prefix("{{debug-only}}") { + Some(_) if !cfg!(debug_assertions) => continue, + Some(line) => line, + None => &line, + }; + let line = line + .replace("{{exe}}", name) + .replace("{{sep}}", std::path::MAIN_SEPARATOR_STR); + writeln!(output, "{line}").unwrap(); + } + output +} + fn split_args(input: &str) -> Vec { input.split_whitespace().map(ToOwned::to_owned).collect() } diff --git a/tests/website.rs b/tests/website.rs index 29f7a42d..abdfb73c 100644 --- a/tests/website.rs +++ b/tests/website.rs @@ -667,7 +667,7 @@ fn website_homepage_leads_with_purpose_and_scope() { for text in [ "A fast formatter for YAML and Markdown.", - "uvx yamark format", + "uvx yamark format .", ] { assert!(rendered.contains(text), "homepage should include {text:?}"); } @@ -1379,7 +1379,12 @@ fn website_documents_command_line_reference() { "`yamark format --diff PATHS`", "`yamark format --stdin-file-path PATH`", "`yamark to-yaml --stdin-file-path PATH`", - "When `PATHS` is omitted from `yamark format`, Yamark uses the current directory (`.`)", + "For help, run `yamark format --help`.", + "In Yamark 0.6.1, bare `yamark format` rewrites files in the current directory and its subdirectories.", + "Use `yamark format .` to format the current directory and its subdirectories.", + "`yamark format --check .`", + "`yamark format --diff .`", + "With `--check` or `--diff`, omitting the path selects the current directory (`.`).", "An unsupported `--stdin-file-path` is an error", "Unified diffs", "Formatted YAML only", diff --git a/website/assets/social-card.svg b/website/assets/social-card.svg index d0974707..b2e544b0 100644 --- a/website/assets/social-card.svg +++ b/website/assets/social-card.svg @@ -18,6 +18,6 @@ - $ uvx yamark format + $ uvx yamark format . diff --git a/website/cli-help.html.md b/website/cli-help.html.md index ea557463..07ca0b38 100644 --- a/website/cli-help.html.md +++ b/website/cli-help.html.md @@ -29,7 +29,15 @@ generated from the current Yamark binary. exits `1` when formatting fails and `0` on success otherwise. Invalid command syntax exits `2`. -When `PATHS` is omitted from `yamark format`, Yamark uses the current directory +For help, run `yamark format --help`. In Yamark 0.6.1, bare `yamark format` +rewrites files in the current directory and its subdirectories. + +In the development version documented here, bare `yamark format` shows help. +In-place formatting requires a path, even in a directory with a `yamark.toml`. +Use `yamark format .` to format the current directory and its subdirectories. +Use `yamark format --check .` to check those files or `yamark format --diff .` +to preview changes. +With `--check` or `--diff`, omitting the path selects the current directory (`.`). Format path mode counts unsupported extensions as skipped without failing. An unsupported `--stdin-file-path` is an error because stdin mode requires a supported file type. @@ -69,7 +77,7 @@ Run `yamark <COMMAND> --help` for command-level help.
Usage: yamark format [OPTIONS] [PATHS]...
 
 Arguments:
-  [PATHS]...
+  [PATHS]...  Files or directories to format in place. Use '.' for the current directory
 
 Options:
       --check
diff --git a/website/cli-help.qmd b/website/cli-help.qmd
index 679658c2..1c8de84d 100644
--- a/website/cli-help.qmd
+++ b/website/cli-help.qmd
@@ -62,7 +62,15 @@ generated from the current Yamark binary.
 exits `1` when formatting fails and `0` on success otherwise. Invalid command
 syntax exits `2`.
 
-When `PATHS` is omitted from `yamark format`, Yamark uses the current directory
+For help, run `yamark format --help`. In Yamark 0.6.1, bare `yamark format`
+rewrites files in the current directory and its subdirectories.
+
+In the development version documented here, bare `yamark format` shows help.
+In-place formatting requires a path, even in a directory with a `yamark.toml`.
+Use `yamark format .` to format the current directory and its subdirectories.
+Use `yamark format --check .` to check those files or `yamark format --diff .`
+to preview changes.
+With `--check` or `--diff`, omitting the path selects the current directory
 (`.`). Format path mode counts unsupported extensions as skipped without
 failing. An unsupported `--stdin-file-path` is an error because stdin mode
 requires a supported file type.
diff --git a/website/directives.html.md b/website/directives.html.md
index 426f724a..7f10d2af 100644
--- a/website/directives.html.md
+++ b/website/directives.html.md
@@ -248,8 +248,8 @@ workflow:
 - In an editor, enable format on save. The [Editors](editors.qmd) page shows
   the VS Code and Positron settings. This is the most direct way to see a
   directive take effect while editing.
-- In a pre-commit hook, run `yamark format`. In CI, use
-  `yamark format --check` to verify files without changing them.
+- In a pre-commit hook, run `yamark format .`. In CI, use
+  `yamark format --check .` to verify files without changing them.
 
 The experimental [Git Filter](git-filter.qmd) serves a narrower workflow: it
 stores selected Markdown files sentence-per-line while keeping the working tree
diff --git a/website/directives.qmd b/website/directives.qmd
index 426f724a..7f10d2af 100644
--- a/website/directives.qmd
+++ b/website/directives.qmd
@@ -248,8 +248,8 @@ workflow:
 - In an editor, enable format on save. The [Editors](editors.qmd) page shows
   the VS Code and Positron settings. This is the most direct way to see a
   directive take effect while editing.
-- In a pre-commit hook, run `yamark format`. In CI, use
-  `yamark format --check` to verify files without changing them.
+- In a pre-commit hook, run `yamark format .`. In CI, use
+  `yamark format --check .` to verify files without changing them.
 
 The experimental [Git Filter](git-filter.qmd) serves a narrower workflow: it
 stores selected Markdown files sentence-per-line while keeping the working tree
diff --git a/website/git-filter.html.md b/website/git-filter.html.md
index d7b7477f..18624900 100644
--- a/website/git-filter.html.md
+++ b/website/git-filter.html.md
@@ -232,5 +232,5 @@ Every clone that wants the working-tree view needs the `git config` commands.
 The `.gitattributes` file selects paths, but it does not define the filter
 driver by itself.
 
-Use a pre-commit hook or CI `yamark format --check` instead when you want the
-repository storage form to match the working-tree form.
+To keep the repository storage form the same as the working-tree form, use a
+pre-commit hook or run `yamark format --check .` in CI.
diff --git a/website/git-filter.qmd b/website/git-filter.qmd
index d7b7477f..18624900 100644
--- a/website/git-filter.qmd
+++ b/website/git-filter.qmd
@@ -232,5 +232,5 @@ Every clone that wants the working-tree view needs the `git config` commands.
 The `.gitattributes` file selects paths, but it does not define the filter
 driver by itself.
 
-Use a pre-commit hook or CI `yamark format --check` instead when you want the
-repository storage form to match the working-tree form.
+To keep the repository storage form the same as the working-tree form, use a
+pre-commit hook or run `yamark format --check .` in CI.
diff --git a/website/index.html.md b/website/index.html.md
index 6bcd2334..fe59bd7b 100644
--- a/website/index.html.md
+++ b/website/index.html.md
@@ -24,7 +24,7 @@ from code, keeping source readable and changes easy to review.
 
 ::: {.hero-command}
 Run directly from PyPI
-`uvx yamark format`
+`uvx yamark format .`
 :::
 :::
 :::
diff --git a/website/index.qmd b/website/index.qmd
index 338c86f3..6b6b1c9b 100644
--- a/website/index.qmd
+++ b/website/index.qmd
@@ -54,7 +54,7 @@ from code, keeping source readable and changes easy to review.
 
 ::: {.hero-command}
 Run directly from PyPI
-`uvx yamark format`
+`uvx yamark format .`
 :::
 :::
 :::
diff --git a/website/usage.html.md b/website/usage.html.md
index 8d8b4d05..b7d82955 100644
--- a/website/usage.html.md
+++ b/website/usage.html.md
@@ -41,7 +41,7 @@ yamark format config.yaml docs/
 Format the current directory:
 
 ```sh
-yamark format
+yamark format .
 ```
 
 By default, Markdown prose wraps at column 72 without forcing sentence
diff --git a/website/usage.qmd b/website/usage.qmd
index 8d8b4d05..b7d82955 100644
--- a/website/usage.qmd
+++ b/website/usage.qmd
@@ -41,7 +41,7 @@ yamark format config.yaml docs/
 Format the current directory:
 
 ```sh
-yamark format
+yamark format .
 ```
 
 By default, Markdown prose wraps at column 72 without forcing sentence