Skip to content

Commit

Permalink
refactor: move tests directly to command
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Jan 20, 2025
1 parent 68cc23a commit 9cbf265
Show file tree
Hide file tree
Showing 334 changed files with 1,727 additions and 3,429 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [Unreleased](https://github.com/hougesen/mdsf/compare/v0.4.1...HEAD)

- feat: add metadata to plugin command schema [`68cc23a`](https://github.com/hougesen/mdsf/commit/68cc23a3b49c07d5de314492ad286155c126f90c)

#### [v0.4.1](https://github.com/hougesen/mdsf/compare/v0.4.0...v0.4.1)

> 18 January 2025
Expand Down
57 changes: 23 additions & 34 deletions codegen/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,35 @@ use convert_case::{Case, Casing};

const INDENT: &str = " ";

#[derive(Debug, serde::Deserialize, schemars::JsonSchema, Hash)]
#[derive(Debug, serde::Deserialize, schemars::JsonSchema, Hash, Clone)]
#[schemars(deny_unknown_fields)]
pub struct ToolTest {
pub struct ToolCommandTest {
/// Codeblock language used when generating tests
pub language: String,

pub command: String,

pub test_input: String,

pub test_output: String,
}

#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
#[derive(Debug, serde::Deserialize, schemars::JsonSchema, Clone)]
#[schemars(deny_unknown_fields)]
pub struct ToolCommand {
pub command: Vec<String>,
pub arguments: Vec<String>,

#[expect(unused)]
pub ignore_output: bool,

#[expect(unused)]
pub description: String,
pub description: Option<String>,

#[expect(unused)]
pub homepage: String,
pub homepage: Option<String>,

#[expect(unused)]
pub tests: Vec<ToolTest>,
pub tests: Option<Vec<ToolCommandTest>>,
}

#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
#[derive(Debug, serde::Deserialize, schemars::JsonSchema, Clone)]
#[schemars(deny_unknown_fields)]
pub struct Tool {
#[expect(unused)]
Expand All @@ -61,8 +58,6 @@ pub struct Tool {
pub categories: std::collections::HashSet<String>,

pub languages: std::collections::HashSet<String>,

pub tests: Option<Vec<ToolTest>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -99,12 +94,14 @@ impl Tool {
)
}

fn generate_test(&self, test: &ToolTest) -> (String, String) {
fn generate_test(&self, command: &str, test: &ToolCommandTest) -> (String, String) {

Check warning on line 97 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L97

Added line #L97 was not covered by tests
let mut hasher = DefaultHasher::new();

Check warning on line 99 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L99

Added line #L99 was not covered by tests
test.hash(&mut hasher);

Check warning on line 101 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L101

Added line #L101 was not covered by tests
let id = format!("{:x}", hasher.finish());

let module_name = self.get_command_name(&test.command).to_case(Case::Snake);
let module_name = self.get_command_name(command).to_case(Case::Snake);

Check warning on line 104 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L104

Added line #L104 was not covered by tests

let language = test.language.to_case(Case::Snake);

Expand Down Expand Up @@ -140,17 +137,6 @@ impl Tool {
fn generate(&self) -> Vec<GeneratedCommand> {
let mut all_commands = Vec::new();

let mut generated_tests: std::collections::HashMap<String, Vec<String>> =
std::collections::HashMap::new();

if let Some(tests) = &self.tests {
for test in tests {
let (module, code) = self.generate_test(test);

generated_tests.entry(module).or_default().push(code);
}
}

for (cmd, options) in &self.commands {

Check warning on line 140 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L140

Added line #L140 was not covered by tests
let command_name = self.get_command_name(cmd);

Expand Down Expand Up @@ -187,7 +173,7 @@ impl Tool {
let mut args_includes_path = false;

let string_args = options
.command
.arguments

Check warning on line 176 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L175-L176

Added lines #L175 - L176 were not covered by tests
.iter()
.map(|arg| {
if arg == "$PATH" {
Expand Down Expand Up @@ -218,15 +204,20 @@ impl Tool {

let module_name = command_name.to_case(Case::Snake);

let tests = generated_tests
.remove(&module_name)
let mut tests = options
.tests
.clone()

Check warning on line 209 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L207-L209

Added lines #L207 - L209 were not covered by tests
.unwrap_or_default()
.join("\n\n");
.iter()
.map(|test| self.generate_test(cmd, test).1)
.collect::<Vec<_>>();

Check warning on line 213 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L211-L213

Added lines #L211 - L213 were not covered by tests

let tests = if tests.is_empty() {
String::new()
} else {
format!("\n{tests}\n")
tests.sort_unstable();

format!("\n{}\n", tests.join("\n\n"))

Check warning on line 220 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L218-L220

Added lines #L218 - L220 were not covered by tests
};

let code = format!(
Expand Down Expand Up @@ -278,13 +269,11 @@ mod test_{module_name} {{{tests}}}
if cmd.is_empty() { "" } else { ":" },
cmd
),
args: options.command.clone(),
args: options.arguments.clone(),

Check warning on line 272 in codegen/src/tools.rs

View check run for this annotation

Codecov / codecov/patch

codegen/src/tools.rs#L272

Added line #L272 was not covered by tests
binary: self.binary.clone(),
});
}

assert!(generated_tests.is_empty());

all_commands
}
}
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/alejandra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_alejandra {
#[test_with::executable(alejandra)]
fn test_alejandra_nix_7414a8a930f85473() {
fn test_alejandra_nix_f38bff8f20c2aa02() {
let input = r#"{
lib, buildPythonPackage, fetchFromGitHub, redis }:
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/auto_optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_auto_optional {
#[test_with::executable(auto-optional)]
fn test_auto_optional_python_8164e93f38554b33() {
fn test_auto_optional_python_c43199b18f48026d() {
let input = r#"def foo(bar: str = None):
pass
"#;
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/autoflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_autoflake {
#[test_with::executable(autoflake)]
fn test_autoflake_python_a7a00a63bb271a62() {
fn test_autoflake_python_27cfd9b948e80d7f() {
let input = r#"import math
import re
import os
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/autopep_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_autopep_8 {
#[test_with::executable(autopep8)]
fn test_autopep_8_python_32e3f57971ad5635() {
fn test_autopep_8_python_a868b5ad9905fc3f() {
let input = r#"def add( a: int , b:int)->int: return a+b"#;
let output = r#"def add(a: int, b: int) -> int: return a+b
"#;
Expand Down
16 changes: 8 additions & 8 deletions mdsf/src/tools/beautysh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_beautysh {
#[test_with::executable(beautysh)]
fn test_beautysh_shell_cb36b80a8b253f58() {
let input = r#"#!/bin/shell
fn test_beautysh_bash_a6831a7ad31bd0a6() {
let input = r#"#!/bin/bash

Check warning on line 37 in mdsf/src/tools/beautysh.rs

View check run for this annotation

Codecov / codecov/patch

mdsf/src/tools/beautysh.rs#L36-L37

Added lines #L36 - L37 were not covered by tests
add() {
echo "$1" + "$2"
}
"#;
let output = r#"#!/bin/shell
let output = r#"#!/bin/bash

Check warning on line 43 in mdsf/src/tools/beautysh.rs

View check run for this annotation

Codecov / codecov/patch

mdsf/src/tools/beautysh.rs#L43

Added line #L43 was not covered by tests
add() {
echo "$1" + "$2"
}
"#;
let file_ext = crate::fttype::get_file_extension("shell");
let file_ext = crate::fttype::get_file_extension("bash");

Check warning on line 49 in mdsf/src/tools/beautysh.rs

View check run for this annotation

Codecov / codecov/patch

mdsf/src/tools/beautysh.rs#L49

Added line #L49 was not covered by tests
let snippet =
crate::execution::setup_snippet(input, &file_ext).expect("it to create a snippet file");
let result = crate::tools::beautysh::run(snippet.path())
Expand All @@ -57,20 +57,20 @@ add() {
}

#[test_with::executable(beautysh)]
fn test_beautysh_bash_256eb200f416406f() {
let input = r#"#!/bin/bash
fn test_beautysh_shell_f8c934ee37e2888() {
let input = r#"#!/bin/shell

Check warning on line 61 in mdsf/src/tools/beautysh.rs

View check run for this annotation

Codecov / codecov/patch

mdsf/src/tools/beautysh.rs#L60-L61

Added lines #L60 - L61 were not covered by tests
add() {
echo "$1" + "$2"
}
"#;
let output = r#"#!/bin/bash
let output = r#"#!/bin/shell

Check warning on line 67 in mdsf/src/tools/beautysh.rs

View check run for this annotation

Codecov / codecov/patch

mdsf/src/tools/beautysh.rs#L67

Added line #L67 was not covered by tests
add() {
echo "$1" + "$2"
}
"#;
let file_ext = crate::fttype::get_file_extension("bash");
let file_ext = crate::fttype::get_file_extension("shell");

Check warning on line 73 in mdsf/src/tools/beautysh.rs

View check run for this annotation

Codecov / codecov/patch

mdsf/src/tools/beautysh.rs#L73

Added line #L73 was not covered by tests
let snippet =
crate::execution::setup_snippet(input, &file_ext).expect("it to create a snippet file");
let result = crate::tools::beautysh::run(snippet.path())
Expand Down
52 changes: 26 additions & 26 deletions mdsf/src/tools/biome_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,20 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_biome_format {
#[test_with::executable(npx)]
fn test_biome_format_json_844b1c8732d73ac3() {
fn test_biome_format_javascript_4845e9b01c23667f() {
let input = r#"
{
"key": "value",
"key2": [
"value2",
"value3",
1
, null]
}
"#;
let output = r#"{
"key": "value",
"key2": ["value2", "value3", 1, null]
async function asyncAddition(
a,b
) {
return a+b
}
"#;
let output = r#"async function asyncAddition(a, b) {
return a + b;
}
"#;
let file_ext = crate::fttype::get_file_extension("json");
let file_ext = crate::fttype::get_file_extension("javascript");
let snippet =
crate::execution::setup_snippet(input, &file_ext).expect("it to create a snippet file");
let result = crate::tools::biome_format::run(snippet.path())
Expand All @@ -66,20 +63,23 @@ mod test_biome_format {
}

#[test_with::executable(npx)]
fn test_biome_format_javascript_8a9233d85bad9eb0() {
fn test_biome_format_json_90a326e29048e3cd() {
let input = r#"
async function asyncAddition(
a,b
) {
return a+b
}
"#;
let output = r#"async function asyncAddition(a, b) {
return a + b;
{
"key": "value",
"key2": [
"value2",
"value3",
1
, null]
}
"#;
let output = r#"{
"key": "value",
"key2": ["value2", "value3", 1, null]
}
"#;
let file_ext = crate::fttype::get_file_extension("javascript");
let file_ext = crate::fttype::get_file_extension("json");
let snippet =
crate::execution::setup_snippet(input, &file_ext).expect("it to create a snippet file");
let result = crate::tools::biome_format::run(snippet.path())
Expand All @@ -90,7 +90,7 @@ mod test_biome_format {
}

#[test_with::executable(npx)]
fn test_biome_format_typescript_5d686094d584dd57() {
fn test_biome_format_typescript_8154bfdbd3b72275() {
let input = r#"
async function asyncAddition(
a:number,b:number
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/black.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_black {
#[test_with::executable(black)]
fn test_black_python_8156ff26fe126797() {
fn test_black_python_229ec2b01c2bfe3c() {
let input = r#"def add( a: int , b:int)->int: return a+b"#;
let output = r#"def add(a: int, b: int) -> int:
return a + b
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/blade_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_blade_formatter {
#[test_with::executable(npx)]
fn test_blade_formatter_blade_9d6620376a93202f() {
fn test_blade_formatter_blade_9ddeaf972bfb08c1() {
let input = r#"@extends('frontend.layouts.app')
@section('title') foo
@endsection
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/blue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_blue {
#[test_with::executable(blue)]
fn test_blue_python_8156ff26fe126797() {
fn test_blue_python_229ec2b01c2bfe3c() {
let input = r#"def add( a: int , b:int)->int: return a+b"#;
let output = r#"def add(a: int, b: int) -> int:
return a + b
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/brunette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_brunette {
#[test_with::executable(brunette)]
fn test_brunette_python_8156ff26fe126797() {
fn test_brunette_python_229ec2b01c2bfe3c() {

Check warning on line 37 in mdsf/src/tools/brunette.rs

View check run for this annotation

Codecov / codecov/patch

mdsf/src/tools/brunette.rs#L37

Added line #L37 was not covered by tests
let input = r#"def add( a: int , b:int)->int: return a+b"#;
let output = r#"def add(a: int, b: int) -> int:
return a + b
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/buf_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_buf_format {
#[test_with::executable(npx)]
fn test_buf_format_protobuf_a873b41888af5cc8() {
fn test_buf_format_protobuf_10af516c8a015ab5() {
let input = r#"service SearchService {
rpc Search (SearchRequest) returns (SearchResponse);
}"#;
Expand Down
2 changes: 1 addition & 1 deletion mdsf/src/tools/cabal_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfEr
#[cfg(test)]
mod test_cabal_format {
#[test_with::executable(cabal)]
fn test_cabal_format_cabal_6118c31ea8b76f3b() {
fn test_cabal_format_cabal_38e9e2aad5619a6a() {
let input = r#"cabal-version: 2.4
name: mdsf
version: 0
Expand Down
Loading

0 comments on commit 9cbf265

Please sign in to comment.