Skip to content

Commit

Permalink
feat(yaml): add support for yamlfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 21, 2024
1 parent a84abc9 commit 7b7a627
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Expand Up @@ -200,5 +200,8 @@ jobs:
- name: rufo
run: gem install rufo

- name: yamlfmt
run: go install github.com/google/yamlfmt/cmd/yamlfmt@latest

- name: run tests
run: cargo test
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Expand Up @@ -48,6 +48,7 @@ pub mod stylua;
pub mod taplo;
pub mod usort;
pub mod xmllint;
pub mod yamlfmt;
pub mod yapf;
pub mod zigfmt;

Expand Down
75 changes: 75 additions & 0 deletions src/formatters/yamlfmt.rs
@@ -0,0 +1,75 @@
use super::execute_command;

#[inline]
pub fn format_using_yamlfmt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("yamlfmt");

cmd.arg("-quiet").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_yamlfmt {
use crate::{formatters::setup_snippet, languages::Language};

use super::format_using_yamlfmt;

#[test_with::executable(yamlfmt)]
#[test]
fn it_should_format_yaml() {
let input = "
version: 2
updates:
- package-ecosystem: \"cargo\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25
- package-ecosystem: \"github-actions\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25
";

let expected_output = "version: 2
updates:
- package-ecosystem: \"cargo\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25
- package-ecosystem: \"github-actions\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25
";

let snippet = setup_snippet(input, Language::Yaml.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_yamlfmt(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(output, expected_output);
}
}
49 changes: 47 additions & 2 deletions src/languages/yaml.rs
@@ -1,6 +1,8 @@
use schemars::JsonSchema;

use crate::formatters::{prettier::format_using_prettier, MdsfFormatter};
use crate::formatters::{
prettier::format_using_prettier, yamlfmt::format_using_yamlfmt, MdsfFormatter,
};

use super::{Lang, LanguageFormatter};

Expand All @@ -10,6 +12,8 @@ pub enum Yaml {
#[default]
#[serde(rename = "prettier")]
Prettier,
#[serde(rename = "yamlfmt")]
YamlFmt,
}

impl Default for Lang<Yaml> {
Expand All @@ -25,7 +29,10 @@ impl Default for Lang<Yaml> {
impl Default for MdsfFormatter<Yaml> {
#[inline]
fn default() -> Self {
Self::Single(Yaml::Prettier)
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Yaml::Prettier),
Self::Single(Yaml::YamlFmt),
])])
}
}

Expand All @@ -37,6 +44,7 @@ impl LanguageFormatter for Yaml {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Prettier => format_using_prettier(snippet_path, true),
Self::YamlFmt => format_using_yamlfmt(snippet_path),
}
}
}
Expand Down Expand Up @@ -130,4 +138,41 @@ updates:

assert_eq!(output, expected_output);
}

#[test_with::executable(yamlfmt)]
#[test]
fn test_yamlfmt() {
let l = Lang::<Yaml> {
enabled: true,
formatter: MdsfFormatter::Single(Yaml::YamlFmt),
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path)
.expect("it to not fail")
.expect("it to be a snippet");

let expected_output = "version: 2
updates:
- package-ecosystem: \"cargo\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25
- package-ecosystem: \"github-actions\"
directory: \"/\"
schedule:
interval: \"monthly\"
assignees:
- \"hougesen\"
open-pull-requests-limit: 25
";

assert_eq!(output, expected_output);
}
}

0 comments on commit 7b7a627

Please sign in to comment.