Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(yaml): add support for yamlfmt #104

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading