Skip to content

Commit c99a61e

Browse files
shaanmajidj178
andauthored
Make yaml-to-toml CONFIG argument optional (#1593)
Now attempts to default to `.pre-commit-config.yaml` and `.pre-commit-config.yml` in PWD before erroring. Feel free to close if you disagree, just ran into this when spinning up #1592, and figured it's worth fixing :) --------- Co-authored-by: Jo <10510431+j178@users.noreply.github.com>
1 parent 01348b2 commit c99a61e

5 files changed

Lines changed: 176 additions & 15 deletions

File tree

crates/prek/src/cli/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,10 @@ pub(crate) enum UtilCommand {
734734

735735
#[derive(Debug, Args)]
736736
pub(crate) struct YamlToTomlArgs {
737-
/// The YAML configuration file to convert.
737+
/// The YAML configuration file to convert. If omitted, discovers
738+
/// `.pre-commit-config.yaml` or `.pre-commit-config.yml` in the current directory.
738739
#[arg(value_name = "CONFIG", value_hint = ValueHint::FilePath)]
739-
pub(crate) input: PathBuf,
740+
pub(crate) input: Option<PathBuf>,
740741

741742
/// Path to write the generated prek.toml file.
742743
/// Defaults to `prek.toml` in the same directory as the input file.

crates/prek/src/cli/yaml_to_toml.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,53 @@ use std::path::{Path, PathBuf};
44

55
use anyhow::{Context, Result};
66
use owo_colors::OwoColorize;
7-
use prek_consts::PREK_TOML;
7+
use prek_consts::{PRE_COMMIT_CONFIG_YAML, PRE_COMMIT_CONFIG_YML, PREK_TOML};
88
use toml_edit::{Array, ArrayOfTables, DocumentMut, InlineTable, Table, Value};
99

1010
use crate::cli::ExitStatus;
1111
use crate::config;
1212
use crate::fs::Simplified;
1313
use crate::printer::Printer;
1414

15+
/// Resolve the input config path, falling back to `.pre-commit-config.yaml` or
16+
/// `.pre-commit-config.yml` in the current directory.
17+
fn resolve_input(input: Option<PathBuf>) -> Result<PathBuf> {
18+
if let Some(path) = input {
19+
return Ok(path);
20+
}
21+
22+
let yaml = Path::new(PRE_COMMIT_CONFIG_YAML);
23+
if yaml.is_file() {
24+
return Ok(yaml.to_path_buf());
25+
}
26+
27+
let yml = Path::new(PRE_COMMIT_CONFIG_YML);
28+
if yml.is_file() {
29+
return Ok(yml.to_path_buf());
30+
}
31+
32+
anyhow::bail!(
33+
"No `{}` or `{}` found in the current directory\n\n\
34+
{} Provide a path explicitly: {}",
35+
PRE_COMMIT_CONFIG_YAML.cyan(),
36+
PRE_COMMIT_CONFIG_YML.cyan(),
37+
"hint:".yellow().bold(),
38+
"prek util yaml-to-toml <CONFIG>".cyan()
39+
);
40+
}
41+
1542
pub(crate) fn yaml_to_toml(
16-
input: &Path,
43+
input: Option<PathBuf>,
1744
output: Option<PathBuf>,
1845
force: bool,
1946
printer: Printer,
2047
) -> Result<ExitStatus> {
48+
let input = resolve_input(input)?;
49+
2150
// Validate the input file first.
22-
let _ = config::load_config(input)?;
51+
let _ = config::load_config(&input)?;
2352

24-
let content = fs_err::read_to_string(input)?;
53+
let content = fs_err::read_to_string(&input)?;
2554
let value: serde_json::Value = serde_saphyr::from_str(&content)?;
2655

2756
let output = output.unwrap_or_else(|| input.parent().unwrap_or(Path::new(".")).join(PREK_TOML));
@@ -65,7 +94,8 @@ pub(crate) fn yaml_to_toml(
6594

6695
writeln!(
6796
printer.stdout(),
68-
"Written to `{}`",
97+
"Converted `{}` → `{}`",
98+
input.simplified_display().cyan(),
6999
output.simplified_display().cyan()
70100
)?;
71101

crates/prek/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ async fn run(cli: Cli) -> Result<ExitStatus> {
401401
UtilCommand::YamlToToml(args) => {
402402
show_settings!(args);
403403

404-
cli::yaml_to_toml(&args.input, args.output, args.force, printer)
404+
cli::yaml_to_toml(args.input, args.output, args.force, printer)
405405
}
406406
UtilCommand::GenerateShellCompletion(args) => {
407407
show_settings!(args);

crates/prek/tests/yaml_to_toml.rs

Lines changed: 135 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use assert_fs::assert::PathAssert;
22
use assert_fs::fixture::{FileWriteStr, PathChild};
3-
use prek_consts::PREK_TOML;
3+
use prek_consts::{PRE_COMMIT_CONFIG_YAML, PRE_COMMIT_CONFIG_YML, PREK_TOML};
44

55
use crate::common::{TestContext, cmd_snapshot};
66

@@ -68,14 +68,14 @@ fn yaml_to_toml_writes_default_output() -> anyhow::Result<()> {
6868
context
6969
.command()
7070
.args(["util", "yaml-to-toml", "config.yaml"]),
71-
@r#"
71+
@"
7272
success: true
7373
exit_code: 0
7474
----- stdout -----
75-
Written to `prek.toml`
75+
Converted `config.yaml` → `prek.toml`
7676
7777
----- stderr -----
78-
"#
78+
"
7979
);
8080

8181
insta::assert_snapshot!(context.read(PREK_TOML), @r#"
@@ -185,7 +185,7 @@ fn yaml_to_toml_force_overwrite() -> anyhow::Result<()> {
185185
success: true
186186
exit_code: 0
187187
----- stdout -----
188-
Written to `prek.toml`
188+
Converted `config.yaml` → `prek.toml`
189189
190190
----- stderr -----
191191
"
@@ -257,3 +257,133 @@ fn yaml_to_toml_same_output() -> anyhow::Result<()> {
257257

258258
Ok(())
259259
}
260+
261+
#[test]
262+
fn yaml_to_toml_discovers_pre_commit_config_yaml() -> anyhow::Result<()> {
263+
let context = TestContext::new();
264+
265+
context
266+
.work_dir()
267+
.child(PRE_COMMIT_CONFIG_YAML)
268+
.write_str(YAML_CONFIG)?;
269+
270+
cmd_snapshot!(
271+
context.filters(),
272+
context.command().args(["util", "yaml-to-toml"]),
273+
@"
274+
success: true
275+
exit_code: 0
276+
----- stdout -----
277+
Converted `.pre-commit-config.yaml` → `prek.toml`
278+
279+
----- stderr -----
280+
"
281+
);
282+
283+
context
284+
.work_dir()
285+
.child(PREK_TOML)
286+
.assert(predicates::path::exists());
287+
288+
Ok(())
289+
}
290+
291+
#[test]
292+
fn yaml_to_toml_discovers_pre_commit_config_yml() -> anyhow::Result<()> {
293+
let context = TestContext::new();
294+
295+
context
296+
.work_dir()
297+
.child(PRE_COMMIT_CONFIG_YML)
298+
.write_str(YAML_CONFIG)?;
299+
300+
cmd_snapshot!(
301+
context.filters(),
302+
context.command().args(["util", "yaml-to-toml"]),
303+
@"
304+
success: true
305+
exit_code: 0
306+
----- stdout -----
307+
Converted `.pre-commit-config.yml` → `prek.toml`
308+
309+
----- stderr -----
310+
"
311+
);
312+
313+
context
314+
.work_dir()
315+
.child(PREK_TOML)
316+
.assert(predicates::path::exists());
317+
318+
Ok(())
319+
}
320+
321+
#[test]
322+
fn yaml_to_toml_prefers_yaml_over_yml() -> anyhow::Result<()> {
323+
let context = TestContext::new();
324+
325+
// Write different content to each file so we can verify which was used.
326+
let yaml_only = indoc::indoc! {r"
327+
repos:
328+
- repo: builtin
329+
hooks:
330+
- id: trailing-whitespace
331+
"};
332+
let yml_only = indoc::indoc! {r"
333+
repos:
334+
- repo: builtin
335+
hooks:
336+
- id: end-of-file-fixer
337+
"};
338+
339+
context
340+
.work_dir()
341+
.child(PRE_COMMIT_CONFIG_YAML)
342+
.write_str(yaml_only)?;
343+
context
344+
.work_dir()
345+
.child(PRE_COMMIT_CONFIG_YML)
346+
.write_str(yml_only)?;
347+
348+
cmd_snapshot!(
349+
context.filters(),
350+
context.command().args(["util", "yaml-to-toml"]),
351+
@"
352+
success: true
353+
exit_code: 0
354+
----- stdout -----
355+
Converted `.pre-commit-config.yaml` → `prek.toml`
356+
357+
----- stderr -----
358+
"
359+
);
360+
361+
// The .yaml file contains trailing-whitespace, the .yml contains end-of-file-fixer.
362+
let output = context.read(PREK_TOML);
363+
assert!(
364+
output.contains("trailing-whitespace"),
365+
"Expected .yaml to be preferred over .yml"
366+
);
367+
368+
Ok(())
369+
}
370+
371+
#[test]
372+
fn yaml_to_toml_error_when_no_config_found() {
373+
let context = TestContext::new();
374+
375+
cmd_snapshot!(
376+
context.filters(),
377+
context.command().args(["util", "yaml-to-toml"]),
378+
@r#"
379+
success: false
380+
exit_code: 2
381+
----- stdout -----
382+
383+
----- stderr -----
384+
error: No `.pre-commit-config.yaml` or `.pre-commit-config.yml` found in the current directory
385+
386+
hint: Provide a path explicitly: prek util yaml-to-toml <CONFIG>
387+
"#
388+
);
389+
}

docs/cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,12 +914,12 @@ Convert a YAML configuration file to prek.toml
914914
<h3 class="cli-reference">Usage</h3>
915915

916916
```
917-
prek util yaml-to-toml [OPTIONS] <CONFIG>
917+
prek util yaml-to-toml [OPTIONS] [CONFIG]
918918
```
919919

920920
<h3 class="cli-reference">Arguments</h3>
921921

922-
<dl class="cli-reference"><dt id="prek-util-yaml-to-toml--input"><a href="#prek-util-yaml-to-toml--input"><code>CONFIG</code></a></dt><dd><p>The YAML configuration file to convert</p>
922+
<dl class="cli-reference"><dt id="prek-util-yaml-to-toml--input"><a href="#prek-util-yaml-to-toml--input"><code>CONFIG</code></a></dt><dd><p>The YAML configuration file to convert. If omitted, discovers <code>.pre-commit-config.yaml</code> or <code>.pre-commit-config.yml</code> in the current directory</p>
923923
</dd></dl>
924924

925925
<h3 class="cli-reference">Options</h3>

0 commit comments

Comments
 (0)