diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 6f25ca1..47c042b 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -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 diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 94fd55c..1f6fbb0 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -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; diff --git a/src/formatters/yamlfmt.rs b/src/formatters/yamlfmt.rs new file mode 100644 index 0000000..e4ce58d --- /dev/null +++ b/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)> { + 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); + } +} diff --git a/src/languages/yaml.rs b/src/languages/yaml.rs index ba670bc..0aa50f5 100644 --- a/src/languages/yaml.rs +++ b/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}; @@ -10,6 +12,8 @@ pub enum Yaml { #[default] #[serde(rename = "prettier")] Prettier, + #[serde(rename = "yamlfmt")] + YamlFmt, } impl Default for Lang { @@ -25,7 +29,10 @@ impl Default for Lang { impl Default for MdsfFormatter { #[inline] fn default() -> Self { - Self::Single(Yaml::Prettier) + Self::Multiple(vec![Self::Multiple(vec![ + Self::Single(Yaml::Prettier), + Self::Single(Yaml::YamlFmt), + ])]) } } @@ -37,6 +44,7 @@ impl LanguageFormatter for Yaml { ) -> std::io::Result<(bool, Option)> { match self { Self::Prettier => format_using_prettier(snippet_path, true), + Self::YamlFmt => format_using_yamlfmt(snippet_path), } } } @@ -130,4 +138,41 @@ updates: assert_eq!(output, expected_output); } + + #[test_with::executable(yamlfmt)] + #[test] + fn test_yamlfmt() { + let l = Lang:: { + 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); + } }