Skip to content

Commit

Permalink
test: formatters (#10)
Browse files Browse the repository at this point in the history
* test: add test for rustfmt

* test: write test for ruff

* ci: install ruff

* ci: setup python

* ci: remove macos-14

* ci: only run test on ubuntu

* test: validate taplo works

* fix: install taplo-cli instead of taplo

* test(json): validate biome

* test(javascript): validate biome

* test(typescript): validate biome

* ci: setup node

* chore: remove npm cache

* test(nim): validate nimpretty

* ci: validate formatters are installed

* ci: use latest biome

* ci: install binary release of taplo

* refactor: set tempfile permissions to 777

* ci: use action for taplo

* refactor: remove permission

* test(zig): validate zigfmt

* style: remove usage of raw strings

* test(lua): validate using stylua

* ci: remove latest from biome
  • Loading branch information
hougesen authored Mar 7, 2024
1 parent 09a8156 commit 38db873
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 2 deletions.
58 changes: 56 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ env:
RUST_BACKTRACE: full

jobs:
test:
name: test
lint:
name: lint
strategy:
matrix:
os:
Expand All @@ -30,4 +30,58 @@ jobs:
- run: cargo fetch
- run: cargo fmt -- --check --color always
- run: cargo clippy

test:
name: test
strategy:
matrix:
os:
- ubuntu-latest
node:
- 20
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
# Used by ruff
- uses: actions/setup-python@v5
with:
cache: "pip"
# Used by biome
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
# Used by nimpretty
- uses: jiro4989/setup-nim-action@v1
# Used by zigfmt
- uses: goto-bus-stop/setup-zig@v2
- uses: uncenter/setup-taplo@v1

- run: rustup toolchain install stable --profile minimal
- run: rustup component add rustfmt clippy

- name: Validate taplo
run: taplo --version

- name: Install ruff
run: pip install ruff

- name: Validate ruff
run: ruff --version

- name: Install stylua
run: cargo install stylua

- name: Validate stylua
run: stylua --version

- name: Validate biome
run: npx --yes @biomejs/biome --version

- name: Validate nimpretty
run: nimpretty --version

- name: Validate zig fmt
run: zig fmt --help

- run: cargo test
94 changes: 94 additions & 0 deletions src/formatters/biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,97 @@ pub fn format_using_biome(file_path: &std::path::Path) -> std::io::Result<Option

Ok(None)
}

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

#[test]
fn it_should_format_json() {
let input = "
{
// comments are allowed
\"key\": \"value\",
\"key2\": [
\"value2\",
\"value3\",
1
, null]
}
";

let expected_output = "{
\t// comments are allowed
\t\"key\": \"value\",
\t\"key2\": [\"value2\", \"value3\", 1, null]
}
";

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

let output = format_using_biome(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}

#[test]
fn it_should_format_javascript() {
let input = "
async function asyncAddition(
a,b
) {
return a+b
}
";

let expected_output = "async function asyncAddition(a, b) {
\treturn a + b;
}
";

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

let output = format_using_biome(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}

#[test]
fn it_should_format_typescript() {
let input = "
async function asyncAddition(
a:number,b:number
) :Promise<
number>
{
return a+b
}
";

let expected_output =
"async function asyncAddition(a: number, b: number): Promise<number> {
\treturn a + b;
}
";

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

let output = format_using_biome(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
27 changes: 27 additions & 0 deletions src/formatters/nimpretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,30 @@ pub fn format_using_nimpretty(file_path: &std::path::Path) -> std::io::Result<Op

Ok(None)
}

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

#[test]
fn it_should_format_toml() {
let input = "proc add( a :int , b:int ) : int =
return a + b ";

let expected_output = "proc add(a: int, b: int): int =
return a + b
";

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

let output = format_using_nimpretty(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
23 changes: 23 additions & 0 deletions src/formatters/ruff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,26 @@ pub fn format_using_ruff(file_path: &std::path::Path) -> std::io::Result<Option<

Ok(None)
}

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

use super::format_using_ruff;

#[test]
fn it_should_format_python() {
let input = "def add( a: int , b:int)->int: return a+b";

let expected_output = "def add(a: int, b: int) -> int:\n return a + b\n";

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

let output = format_using_ruff(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
27 changes: 27 additions & 0 deletions src/formatters/rustfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,30 @@ pub fn format_using_rustfmt(file_path: &std::path::Path) -> std::io::Result<Opti

Ok(None)
}

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

use super::format_using_rustfmt;

#[test]
fn it_should_format_rust() {
let input = "pub
async
fn add( a: i32,
b:i32 )-> i32 {a+b}
";

let expected_output = "pub async fn add(a: i32, b: i32) -> i32 {\n a + b\n}\n";

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

let output = format_using_rustfmt(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
34 changes: 34 additions & 0 deletions src/formatters/stylua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,37 @@ pub fn format_using_stylua(file_path: &std::path::Path) -> std::io::Result<Optio

Ok(None)
}

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

#[test]
fn it_should_format_lua() {
let input = "
local function add ( a , b
)
return a +b
end
";

let expected_output = "local function add(a, b)\n\treturn a + b\nend\n";

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

let output = format_using_stylua(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
28 changes: 28 additions & 0 deletions src/formatters/taplo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,31 @@ pub fn format_using_taplo(file_path: &std::path::Path) -> std::io::Result<Option

Ok(None)
}

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

#[test]
fn it_should_format_toml() {
let input = " package = \"mdsf\"
author = \"Mads Hougesen\"
";

let expected_output = "package = \"mdsf\"
author = \"Mads Hougesen\"
";

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

let output = format_using_taplo(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
32 changes: 32 additions & 0 deletions src/formatters/zigfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,35 @@ pub fn format_using_zigfmt(file_path: &std::path::Path) -> std::io::Result<Optio

Ok(None)
}

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

#[test]
fn it_should_format_zig() {
let input = "
fn add (a : i32 , b : i32 ) i32 {
return a + b ;
}
";

let expected_output = "fn add(a: i32, b: i32) i32 {
return a + b;
}
";

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

let output = format_using_zigfmt(snippet.path())
.expect("it to be succesful")
.expect("it to be some");

assert_eq!(expected_output, output);
}
}

0 comments on commit 38db873

Please sign in to comment.