#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::process::Command;
fn tokf() -> Command {
Command::new(env!("CARGO_BIN_EXE_tokf"))
}
fn rewrite_with_stdlib(command: &str) -> String {
let dir = tempfile::TempDir::new().unwrap();
let output = tokf()
.args(["rewrite", command])
.current_dir(dir.path())
.output()
.unwrap();
assert!(
output.status.success(),
"tokf rewrite failed: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
#[test]
fn rewrite_git_status() {
let result = rewrite_with_stdlib("git status");
assert_eq!(result, "tokf run git status");
}
#[test]
fn rewrite_git_status_with_args() {
let result = rewrite_with_stdlib("git status --short");
assert_eq!(result, "tokf run git status --short");
}
#[test]
fn rewrite_cargo_test() {
let result = rewrite_with_stdlib("cargo test");
assert_eq!(result, "tokf run cargo test");
}
#[test]
fn rewrite_cargo_test_with_args() {
let result = rewrite_with_stdlib("cargo test --lib");
assert_eq!(result, "tokf run cargo test --lib");
}
#[test]
fn rewrite_git_push() {
let result = rewrite_with_stdlib("git push");
assert_eq!(result, "tokf run git push");
}
#[test]
fn rewrite_git_diff() {
let result = rewrite_with_stdlib("git diff");
assert_eq!(result, "tokf run git diff");
}
#[test]
fn rewrite_git_log() {
let result = rewrite_with_stdlib("git log");
assert_eq!(result, "tokf run git log");
}
#[test]
fn rewrite_git_add() {
let result = rewrite_with_stdlib("git add");
assert_eq!(result, "tokf run git add");
}
#[test]
fn rewrite_git_commit() {
let result = rewrite_with_stdlib("git commit");
assert_eq!(result, "tokf run git commit");
}
#[test]
fn rewrite_cargo_build() {
let result = rewrite_with_stdlib("cargo build");
assert_eq!(result, "tokf run cargo build");
}
#[test]
fn rewrite_cargo_clippy() {
let result = rewrite_with_stdlib("cargo clippy");
assert_eq!(result, "tokf run cargo clippy");
}
#[test]
fn rewrite_ls() {
let result = rewrite_with_stdlib("ls -la");
assert_eq!(result, "tokf run ls -la");
}
#[test]
fn rewrite_tokf_command_unchanged() {
let result = rewrite_with_stdlib("tokf run git status");
assert_eq!(result, "tokf run git status");
}
#[test]
fn rewrite_heredoc_unchanged() {
let result = rewrite_with_stdlib("cat <<EOF");
assert_eq!(result, "cat <<EOF");
}
#[test]
fn rewrite_unknown_command_passthrough() {
let result = rewrite_with_stdlib("unknown-cmd foo bar");
assert_eq!(result, "unknown-cmd foo bar");
}
#[test]
fn rewrite_user_override() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(
filters_dir.join("git-status.toml"),
"command = \"git status\"",
)
.unwrap();
std::fs::write(
dir.path().join(".tokf/rewrites.toml"),
r#"
[[rewrite]]
match = "^git status"
replace = "custom-wrapper {0}"
"#,
)
.unwrap();
let output = tokf()
.args(["rewrite", "git status"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout.trim(), "custom-wrapper git status");
}
#[test]
fn rewrite_user_skip_pattern() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(
filters_dir.join("git-status.toml"),
"command = \"git status\"",
)
.unwrap();
std::fs::write(
dir.path().join(".tokf/rewrites.toml"),
r#"
[skip]
patterns = ["^git status"]
"#,
)
.unwrap();
let output = tokf()
.args(["rewrite", "git status"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout.trim(), "git status");
}
#[test]
fn rewrite_multiple_patterns_first_variant() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(
filters_dir.join("test-runner.toml"),
r#"command = ["pnpm test", "npm test"]"#,
)
.unwrap();
let output = tokf()
.args(["rewrite", "pnpm test"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"tokf run pnpm test"
);
}
#[test]
fn rewrite_multiple_patterns_second_variant() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(
filters_dir.join("test-runner.toml"),
r#"command = ["pnpm test", "npm test"]"#,
)
.unwrap();
let output = tokf()
.args(["rewrite", "npm test"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"tokf run npm test"
);
}
#[test]
fn rewrite_multiple_patterns_non_variant_passthrough() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(
filters_dir.join("test-runner.toml"),
r#"command = ["pnpm test", "npm test"]"#,
)
.unwrap();
let output = tokf()
.args(["rewrite", "tokf_test_sentinel_cmd test"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"tokf_test_sentinel_cmd test"
);
}
#[test]
fn rewrite_wildcard_pattern_matches_any_subcommand() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(filters_dir.join("npm-run.toml"), r#"command = "npm run *""#).unwrap();
let output = tokf()
.args(["rewrite", "npm run build"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"tokf run npm run build"
);
}
#[test]
fn rewrite_wildcard_pattern_no_match_without_wildcard_arg() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(filters_dir.join("npm-run.toml"), r#"command = "npm run *""#).unwrap();
let output = tokf()
.args(["rewrite", "npm run"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(String::from_utf8_lossy(&output.stdout).trim(), "npm run");
}
#[test]
fn rewrite_golangci_lint_run_matches() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(
filters_dir.join("golangci-lint.toml"),
r#"command = "golangci-lint run""#,
)
.unwrap();
let output = tokf()
.args(["rewrite", "golangci-lint run"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"tokf run golangci-lint run"
);
}
#[test]
fn rewrite_golangci_lint_alone_passthrough() {
let dir = tempfile::TempDir::new().unwrap();
let filters_dir = dir.path().join(".tokf/filters");
std::fs::create_dir_all(&filters_dir).unwrap();
std::fs::write(
filters_dir.join("golangci-lint.toml"),
r#"command = "golangci-lint run""#,
)
.unwrap();
let output = tokf()
.args(["rewrite", "golangci-lint"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
"golangci-lint"
);
}
#[test]
fn rewrite_pipe_grep_stripped() {
let result = rewrite_with_stdlib("cargo test | grep FAILED");
assert_eq!(result, "tokf run --baseline-pipe 'grep FAILED' cargo test");
}
#[test]
fn rewrite_pipe_head_stripped() {
let result = rewrite_with_stdlib("git diff HEAD | head -5");
assert_eq!(result, "tokf run --baseline-pipe 'head -5' git diff HEAD");
}
#[test]
fn rewrite_pipe_tail_stripped() {
let result = rewrite_with_stdlib("cargo test | tail -n 5");
assert_eq!(result, "tokf run --baseline-pipe 'tail -n 5' cargo test");
}
#[test]
fn rewrite_pipe_grep_pattern_stripped() {
let result = rewrite_with_stdlib("git status | grep modified");
assert_eq!(
result,
"tokf run --baseline-pipe 'grep modified' git status"
);
}
#[test]
fn rewrite_pipe_tail_follow_passes_through() {
let result = rewrite_with_stdlib("cargo test | tail -f");
assert_eq!(result, "cargo test | tail -f");
}
#[test]
fn rewrite_pipe_wc_passes_through() {
let result = rewrite_with_stdlib("git status | wc -l");
assert_eq!(result, "git status | wc -l");
}
#[test]
fn rewrite_pipe_no_filter_preserves_pipe() {
let result = rewrite_with_stdlib("unknown-cmd | tail -5");
assert_eq!(result, "unknown-cmd | tail -5");
}
#[test]
fn rewrite_multi_pipe_chain_passes_through() {
let result = rewrite_with_stdlib("git status | grep M | wc -l");
assert_eq!(result, "git status | grep M | wc -l");
}
#[test]
fn rewrite_multi_pipe_with_tail_passes_through() {
let result = rewrite_with_stdlib("cargo test | grep x | tail -5");
assert_eq!(result, "cargo test | grep x | tail -5");
}
#[test]
fn rewrite_compound_then_tail_stripped() {
let result = rewrite_with_stdlib("git add . && cargo test | tail -5");
assert_eq!(
result,
"tokf run git add . && tokf run --baseline-pipe 'tail -5' cargo test"
);
}
#[test]
fn rewrite_logical_or_still_rewritten_integration() {
let result = rewrite_with_stdlib("cargo test || echo failed");
assert_eq!(result, "tokf run cargo test || echo failed");
}
#[test]
fn rewrite_logical_or_then_pipe_stripped() {
let result = rewrite_with_stdlib("cargo test || cargo test | grep ok");
assert_eq!(
result,
"tokf run cargo test || tokf run --baseline-pipe 'grep ok' cargo test"
);
}
#[test]
fn rewrite_quoted_pipe_not_treated_as_pipe() {
let result = rewrite_with_stdlib("git log --grep='feat|fix'");
assert_eq!(result, "tokf run git log --grep='feat|fix'");
}
#[test]
fn rewrite_pipe_head_bytes_passes_through() {
let result = rewrite_with_stdlib("cargo test | head -c 50");
assert_eq!(result, "cargo test | head -c 50");
}
#[test]
fn rewrite_compound_non_strippable_pipe_passes_through() {
let result = rewrite_with_stdlib("git add . && cargo test | wc -l");
assert_eq!(result, "tokf run git add . && cargo test | wc -l");
}
#[test]
fn rewrite_compound_pipe_no_filter_preserves_pipe() {
let result = rewrite_with_stdlib("git add . && unknown-cmd | tail -5");
assert_eq!(result, "tokf run git add . && unknown-cmd | tail -5");
}
#[test]
fn rewrite_semicolon_compound() {
let result = rewrite_with_stdlib("git add .; git status");
assert_eq!(result, "tokf run git add .; tokf run git status");
}
#[test]
fn rewrite_pipe_grep_quoted_pattern_escaped() {
let result = rewrite_with_stdlib("cargo test | grep -E 'fail|error'");
assert_eq!(
result,
"tokf run --baseline-pipe 'grep -E '\\''fail|error'\\''' cargo test"
);
}
#[test]
fn rewrite_variant_parent_rewrites_npm_test() {
let result = rewrite_with_stdlib("npm test");
assert_eq!(result, "tokf run npm test");
}
#[test]
fn rewrite_variant_parent_rewrites_pnpm_test() {
let result = rewrite_with_stdlib("pnpm test");
assert_eq!(result, "tokf run pnpm test");
}
#[test]
fn rewrite_variant_parent_rewrites_yarn_test() {
let result = rewrite_with_stdlib("yarn test");
assert_eq!(result, "tokf run yarn test");
}
#[test]
fn which_shows_variant_info() {
let dir = tempfile::TempDir::new().unwrap();
let output = tokf()
.args(["which", "npm test"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("variant") || stdout.contains("npm/test"),
"expected variant info or filter name, got: {stdout}"
);
}
#[test]
fn which_variant_file_match() {
let dir = tempfile::TempDir::new().unwrap();
std::fs::write(dir.path().join("vitest.config.ts"), "").unwrap();
let output = tokf()
.args(["which", "npm test", "--verbose"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let combined = format!("{stdout}{stderr}");
assert!(
combined.contains("vitest") || combined.contains("variant"),
"expected vitest variant resolution, got stdout={stdout}, stderr={stderr}"
);
}
#[test]
fn rewrite_make_check() {
let r = rewrite_with_stdlib("make check");
assert_eq!(r, "make SHELL=tokf check");
}
#[test]
fn rewrite_make_bare() {
let r = rewrite_with_stdlib("make");
assert_eq!(r, "make SHELL=tokf");
}
#[test]
fn rewrite_just_test() {
let r = rewrite_with_stdlib("just test");
assert_eq!(r, "just --shell tokf --shell-arg -cu test");
}
#[test]
fn rewrite_just_bare() {
let r = rewrite_with_stdlib("just");
assert_eq!(r, "just --shell tokf --shell-arg -cu");
}
#[test]
fn rewrite_cmake_not_wrapper() {
let r = rewrite_with_stdlib("cmake --build .");
assert_eq!(r, "cmake --build .");
}
#[test]
fn rewrite_make_compound_with_git() {
let r = rewrite_with_stdlib("make check && git status");
assert_eq!(r, "make SHELL=tokf check && tokf run git status");
}
#[test]
fn rewrite_always_exits_zero() {
let output = tokf().args(["rewrite", "anything"]).output().unwrap();
assert!(output.status.success());
assert_eq!(output.status.code(), Some(0));
}