Skip to content

Commit

Permalink
fix: try binary before using npx (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Jun 13, 2024
1 parent 30dd6b8 commit 56e7beb
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 26 deletions.
3 changes: 2 additions & 1 deletion mdsf.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"toml": "taplo",
"typescript": [["prettier", "biome", "deno_fmt"]],
"yaml": [["prettier", "yamlfmt", "yamlfix"]],
"zig": "zigfmt"
"zig": "zigfmt",
"vue": "prettier"
}
}
15 changes: 11 additions & 4 deletions src/formatters/blade_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn set_blade_formatter_args(cmd: &mut std::process::Command, snippet_path: &std:
}

#[inline]
fn invote_blade_formatter(
fn invoke_blade_formatter(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
Expand All @@ -20,15 +20,22 @@ fn invote_blade_formatter(
pub fn format_using_blade_formatter(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
invote_blade_formatter(setup_npm_script("blade-formatter"), snippet_path)
if let Ok(path_result) =
invoke_blade_formatter(std::process::Command::new("blade-formatter"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_blade_formatter(setup_npm_script("blade-formatter"), snippet_path)
}

#[cfg(test)]
mod test_blade_formatter {
use crate::{formatters::setup_snippet, generated::language_to_ext};

#[test_with::executable(npx)]
#[test]
#[test_with::executable(blade-formatter)]
fn it_should_format_blade() {
let input = r#"@extends('frontend.layouts.app')
@section('title') foo
Expand Down
10 changes: 9 additions & 1 deletion src/formatters/elm_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ fn invoke_elm_format(
pub fn format_using_elm_format(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
if let Ok(path_result) =
invoke_elm_format(std::process::Command::new("elm-format"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_elm_format(setup_npm_script("elm-format"), snippet_path)
}

Expand All @@ -30,7 +38,7 @@ mod test_elm_format {
generated::language_to_ext,
};

#[test]
#[test_with::executable(npx)]
fn it_should_format_elm() {
let input = r#"import Html exposing (text)
Expand Down
20 changes: 10 additions & 10 deletions src/formatters/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod test_prettier {
generated::language_to_ext,
};

#[test]
#[test_with::executable(npx)]
fn it_should_format_json() {
let input = "
{
Expand Down Expand Up @@ -90,7 +90,7 @@ mod test_prettier {
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_javascript() {
let input = "
async function asyncAddition(
Expand All @@ -117,7 +117,7 @@ mod test_prettier {
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_typescript() {
let input = "
async function asyncAddition(
Expand Down Expand Up @@ -147,7 +147,7 @@ number>
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_markdown() {
let input = "
Expand All @@ -174,7 +174,7 @@ this is a paragraph
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_not_format_code_snippets_in_markdown() {
let input = "```typescript
async function asyncAddition(
Expand All @@ -201,7 +201,7 @@ number>
assert_eq!(input, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_html() {
let input = " <!doctype html> <html> <head> <style> body {background-color: powderblue;} h1 {color: blue;} p {color: red;} </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html> ";

Expand Down Expand Up @@ -238,7 +238,7 @@ number>
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_css() {
let input = " h1 {color: blue;} p {color: red;} ";

Expand All @@ -261,7 +261,7 @@ p {
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_yaml() {
let input = "
Expand Down Expand Up @@ -317,7 +317,7 @@ updates:
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_vue() {
let input = "<script lang=\"ts\" setup >
import {
Expand Down Expand Up @@ -363,7 +363,7 @@ function add(a: number, b: number): number {
assert_eq!(expected_output, output);
}

#[test]
#[test_with::executable(npx)]
fn it_should_format_graphql() {
let input = "{ hero { name
# Queries can have comments!
Expand Down
8 changes: 8 additions & 0 deletions src/formatters/purs_tidy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ fn invoke_purs_tidy(
pub fn format_using_purs_tidy(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
if let Ok(path_result) = invoke_purs_tidy(std::process::Command::new("purs-tidy"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_purs_tidy(setup_npm_script("purs-tidy"), snippet_path)
}

Expand All @@ -29,6 +36,7 @@ mod test_purs_tidy {
use crate::{formatters::setup_snippet, generated::language_to_ext};

#[test]
#[test_with::executable(npx)]
fn it_should_format_purescript() {
let input = r#"module Test.Main where
Expand Down
14 changes: 11 additions & 3 deletions src/formatters/rescript_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn set_rescript_format_args(cmd: &mut std::process::Command, snippet_path: &std:
}

#[inline]
fn invote_rescript_format(
fn invoke_rescript_format(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
Expand All @@ -20,14 +20,22 @@ fn invote_rescript_format(
pub fn format_using_rescript_format(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
invote_rescript_format(setup_npm_script("rescript"), snippet_path)
if let Ok(path_result) =
invoke_rescript_format(std::process::Command::new("rescript"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_rescript_format(setup_npm_script("rescript"), snippet_path)
}

#[cfg(test)]
mod test_rescript_format {
use crate::{formatters::setup_snippet, generated::language_to_ext};

#[test]
#[test_with::executable(npx)]
fn it_should_format_rescript() {
let input = r#"module Button = {
@react.component
Expand Down
14 changes: 11 additions & 3 deletions src/formatters/sql_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn set_sql_formatter_args(cmd: &mut std::process::Command, snippet_path: &std::p
}

#[inline]
fn invote_sql_formatter(
fn invoke_sql_formatter(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
Expand All @@ -20,14 +20,22 @@ fn invote_sql_formatter(
pub fn format_using_sql_formatter(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
invote_sql_formatter(setup_npm_script("sql-formatter"), snippet_path)
if let Ok(path_result) =
invoke_sql_formatter(std::process::Command::new("sql-formatter"), snippet_path)
{
if !path_result.0 {
return Ok(path_result);
}
}

invoke_sql_formatter(setup_npm_script("sql-formatter"), snippet_path)
}

#[cfg(test)]
mod test_sql_formatter {
use crate::{formatters::setup_snippet, generated::language_to_ext};

#[test]
#[test_with::executable(npx)]
fn it_should_format_sql() {
let input = "SELECT * FROM tbl WHERE foo = 'bar';";

Expand Down
5 changes: 2 additions & 3 deletions src/formatters/sqlfluff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn set_sqlfluff_args(cmd: &mut std::process::Command, snippet_path: &std::path::
}

#[inline]
fn invote_sqlfluff(
fn invoke_sqlfluff(
mut cmd: std::process::Command,
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
Expand All @@ -24,15 +24,14 @@ fn invote_sqlfluff(
pub fn format_using_sqlfluff(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
invote_sqlfluff(std::process::Command::new("sqlfluff"), snippet_path)
invoke_sqlfluff(std::process::Command::new("sqlfluff"), snippet_path)
}

#[cfg(test)]
mod test_sqlfluff {
use crate::{formatters::setup_snippet, generated::language_to_ext};

#[test_with::executable(sqlfluff)]
#[test]
fn it_should_format_sql() {
let input = "SELECT * FROM tbl
WHERE foo = 'bar'; ";
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/taplo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod test_taplo {
generated::language_to_ext,
};

#[test]
#[test_with::executable(npx)]
fn it_should_format_toml() {
let input = " package = \"mdsf\"
author = \"Mads Hougesen\"
Expand Down

0 comments on commit 56e7beb

Please sign in to comment.