|
1 | 1 | use assert_fs::assert::PathAssert; |
2 | 2 | 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}; |
4 | 4 |
|
5 | 5 | use crate::common::{TestContext, cmd_snapshot}; |
6 | 6 |
|
@@ -68,14 +68,14 @@ fn yaml_to_toml_writes_default_output() -> anyhow::Result<()> { |
68 | 68 | context |
69 | 69 | .command() |
70 | 70 | .args(["util", "yaml-to-toml", "config.yaml"]), |
71 | | - @r#" |
| 71 | + @" |
72 | 72 | success: true |
73 | 73 | exit_code: 0 |
74 | 74 | ----- stdout ----- |
75 | | - Written to `prek.toml` |
| 75 | + Converted `config.yaml` → `prek.toml` |
76 | 76 |
|
77 | 77 | ----- stderr ----- |
78 | | - "# |
| 78 | + " |
79 | 79 | ); |
80 | 80 |
|
81 | 81 | insta::assert_snapshot!(context.read(PREK_TOML), @r#" |
@@ -185,7 +185,7 @@ fn yaml_to_toml_force_overwrite() -> anyhow::Result<()> { |
185 | 185 | success: true |
186 | 186 | exit_code: 0 |
187 | 187 | ----- stdout ----- |
188 | | - Written to `prek.toml` |
| 188 | + Converted `config.yaml` → `prek.toml` |
189 | 189 |
|
190 | 190 | ----- stderr ----- |
191 | 191 | " |
@@ -257,3 +257,133 @@ fn yaml_to_toml_same_output() -> anyhow::Result<()> { |
257 | 257 |
|
258 | 258 | Ok(()) |
259 | 259 | } |
| 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 | +} |
0 commit comments