#![allow(dead_code)]
use std::path::Path;
use std::process::{Command, Output};
use tempfile::TempDir;
pub fn linthis_cmd() -> Command {
Command::new(env!("CARGO_BIN_EXE_linthis"))
}
pub fn create_temp_project() -> TempDir {
let temp = TempDir::new().expect("Failed to create temp directory");
std::fs::create_dir_all(temp.path().join(".linthis"))
.expect("Failed to create .linthis directory");
temp
}
pub fn write_fixture(dir: &Path, name: &str, content: &str) {
let path = dir.join(name);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).expect("Failed to create parent directory");
}
std::fs::write(&path, content).expect("Failed to write fixture file");
}
pub fn assert_exit_code(output: &Output, expected: i32) {
let actual = output.status.code().unwrap_or(-1);
assert_eq!(
actual,
expected,
"Expected exit code {}, got {}.\nstdout: {}\nstderr: {}",
expected,
actual,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
pub fn assert_stdout_contains(output: &Output, expected: &str) {
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains(expected),
"Expected stdout to contain '{}', got:\n{}",
expected,
stdout
);
}
pub fn assert_stderr_contains(output: &Output, expected: &str) {
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(expected),
"Expected stderr to contain '{}', got:\n{}",
expected,
stderr
);
}
pub fn tool_available(cmd: &str, args: &[&str]) -> bool {
Command::new(cmd)
.args(args)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
pub fn has_clippy() -> bool {
tool_available("cargo", &["clippy", "--version"])
}
pub fn has_ruff() -> bool {
tool_available("ruff", &["--version"])
}
pub fn has_rustfmt() -> bool {
tool_available("rustfmt", &["--version"])
}
pub fn has_git() -> bool {
tool_available("git", &["--version"])
}
pub fn has_go() -> bool {
tool_available("go", &["version"])
}
pub fn has_golangci_lint() -> bool {
tool_available("golangci-lint", &["--version"])
}
pub fn has_goimports() -> bool {
tool_available("goimports", &["--help"])
}
pub fn has_eslint() -> bool {
tool_available("eslint", &["--version"])
}
pub fn has_prettier() -> bool {
tool_available("prettier", &["--version"])
}
pub fn has_cpplint() -> bool {
tool_available("cpplint", &["--version"])
}
pub fn has_clang_format() -> bool {
tool_available("clang-format", &["--version"])
}
pub fn has_checkstyle() -> bool {
tool_available("checkstyle", &["--version"])
}
pub fn has_dart() -> bool {
tool_available("dart", &["--version"])
}
pub fn has_ktlint() -> bool {
tool_available("ktlint", &["--version"])
}
pub fn has_luacheck() -> bool {
tool_available("luacheck", &["--version"])
}
pub fn has_stylua() -> bool {
tool_available("stylua", &["--version"])
}
pub fn has_swiftlint() -> bool {
tool_available("swiftlint", &["--version"])
}
pub fn has_swift_format() -> bool {
tool_available("swift-format", &["--version"])
}
pub const PYTHON_GOOD: &str = r#""""A simple module with no lint errors."""
def hello(name: str) -> str:
"""Return a greeting message."""
return f"Hello, {name}!"
if __name__ == "__main__":
print(hello("World"))
"#;
pub const PYTHON_BAD: &str = r#"import os
import sys
x=1
y = 2
def foo():
unused_var = 42
print("hello")
"#;
pub const PYTHON_UNFORMATTED: &str = r#"def foo():print("hello")
x=1
y=2
"#;
pub const RUST_GOOD: &str = r#"fn main() {
println!("Hello, world!");
}
"#;
pub const RUST_BAD: &str = r#"fn main() {
let unused = 42;
println!("hello");
}
"#;
pub const RUST_UNFORMATTED: &str = r#"fn main(){println!("hello");let x=1;let y=2;}
"#;
pub const GO_GOOD: &str = r#"package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
"#;
pub const GO_UNFORMATTED: &str = r#"package main
import "fmt"
func main(){fmt.Println("Hello")}
"#;
pub const TS_GOOD: &str = r#"function hello(name: string): string {
return `Hello, ${name}!`;
}
console.log(hello("World"));
"#;
pub const TS_BAD: &str = r#"var x = 1
let unused = 42
function foo(){console.log("hello")}
"#;
pub const TS_UNFORMATTED: &str = r#"function hello(name:string):string{return `Hello, ${name}!`}
"#;
pub const CPP_GOOD: &str = r#"#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
"#;
pub const CPP_BAD: &str = r#"#include <iostream>
int main(){
int x=1;
std::cout<<"hello"<<std::endl;
return 0;}
"#;
pub const JAVA_GOOD: &str = r#"public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
"#;
pub const JAVA_BAD: &str = r#"public class Hello{
public static void main(String[] args){
int x=1;
System.out.println("hello");}}
"#;
pub const DART_GOOD: &str = r#"void main() {
print('Hello, World!');
}
"#;
pub const DART_BAD: &str = r#"void main(){
var x=1;
print("hello");}
"#;
pub const KOTLIN_GOOD: &str = r#"fun main() {
println("Hello, World!")
}
"#;
pub const KOTLIN_BAD: &str = r#"fun main(){
val x=1
println("hello")}
"#;
pub const LUA_GOOD: &str = r#"local function hello(name)
return "Hello, " .. name .. "!"
end
print(hello("World"))
"#;
pub const LUA_BAD: &str = r#"function hello(name)
local unused = 1
return "Hello, "..name.."!"
end
print(hello("World"))
"#;
pub const SWIFT_GOOD: &str = r#"import Foundation
func hello(name: String) -> String {
return "Hello, \(name)!"
}
print(hello(name: "World"))
"#;
pub const SWIFT_BAD: &str = r#"import Foundation
func hello(name:String)->String{return "Hello, \(name)!"}
print(hello(name:"World"))
"#;