Skip to content

Commit e64e312

Browse files
committed
fix: use assert_cmd for CLI integration tests
Fixes Windows CI failures by using assert_cmd which properly builds the binary before running tests, instead of manually looking for it.
1 parent 4787e1d commit e64e312

2 files changed

Lines changed: 23 additions & 46 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ urlencoding = "2"
3939

4040
[dev-dependencies]
4141
tempfile = "3.24.0"
42+
assert_cmd = "2"
43+
predicates = "3"
4244

4345
[package.metadata.deb]
4446
maintainer = "Chris Bednarczyk <tmpltool@bordeux.net>"

tests/test_cli_ide_flag.rs

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,17 @@
22
//!
33
//! These tests verify the IDE metadata export functionality works correctly.
44
5-
use std::process::Command;
6-
7-
fn get_binary_path() -> String {
8-
// Try release binary first, then debug
9-
// Use EXE_SUFFIX for cross-platform compatibility (.exe on Windows, empty on Unix)
10-
let exe_suffix = std::env::consts::EXE_SUFFIX;
11-
let release_path = format!("target/release/tmpltool{}", exe_suffix);
12-
let debug_path = format!("target/debug/tmpltool{}", exe_suffix);
13-
14-
if std::path::Path::new(&release_path).exists() {
15-
release_path
16-
} else if std::path::Path::new(&debug_path).exists() {
17-
debug_path
18-
} else {
19-
panic!("Binary not found. Please run 'cargo build' first.");
20-
}
5+
use assert_cmd::Command;
6+
use predicates::prelude::*;
7+
8+
#[allow(deprecated)]
9+
fn tmpltool() -> Command {
10+
Command::cargo_bin("tmpltool").unwrap()
2111
}
2212

2313
#[test]
2414
fn test_ide_json_outputs_valid_json() {
25-
let output = Command::new(get_binary_path())
15+
let output = tmpltool()
2616
.args(["--ide", "json"])
2717
.output()
2818
.expect("Failed to execute command");
@@ -44,7 +34,7 @@ fn test_ide_json_outputs_valid_json() {
4434

4535
#[test]
4636
fn test_ide_yaml_outputs_valid_yaml() {
47-
let output = Command::new(get_binary_path())
37+
let output = tmpltool()
4838
.args(["--ide", "yaml"])
4939
.output()
5040
.expect("Failed to execute command");
@@ -65,7 +55,7 @@ fn test_ide_yaml_outputs_valid_yaml() {
6555

6656
#[test]
6757
fn test_ide_toml_outputs_valid_toml() {
68-
let output = Command::new(get_binary_path())
58+
let output = tmpltool()
6959
.args(["--ide", "toml"])
7060
.output()
7161
.expect("Failed to execute command");
@@ -86,7 +76,7 @@ fn test_ide_toml_outputs_valid_toml() {
8676

8777
#[test]
8878
fn test_ide_json_structure() {
89-
let output = Command::new(get_binary_path())
79+
let output = tmpltool()
9080
.args(["--ide", "json"])
9181
.output()
9282
.expect("Failed to execute command");
@@ -139,41 +129,26 @@ fn test_ide_json_structure() {
139129
#[test]
140130
fn test_ide_exits_without_rendering() {
141131
// --ide should exit before trying to render a template
142-
let output = Command::new(get_binary_path())
132+
tmpltool()
143133
.args(["--ide", "json", "nonexistent_template.tmpl"])
144-
.output()
145-
.expect("Failed to execute command");
146-
147-
// Should succeed because --ide exits early
148-
assert!(
149-
output.status.success(),
150-
"Command should succeed even with nonexistent template"
151-
);
152-
153-
let stdout = String::from_utf8_lossy(&output.stdout);
154-
let parsed: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
155-
assert!(parsed.is_ok(), "Should still output valid JSON");
134+
.assert()
135+
.success();
156136
}
157137

158138
#[test]
159139
fn test_ide_invalid_format() {
160-
let output = Command::new(get_binary_path())
140+
tmpltool()
161141
.args(["--ide", "invalid_format"])
162-
.output()
163-
.expect("Failed to execute command");
164-
165-
assert!(!output.status.success(), "Command should fail");
166-
167-
let stderr = String::from_utf8_lossy(&output.stderr);
168-
assert!(
169-
stderr.contains("invalid") || stderr.contains("possible values"),
170-
"Error should indicate invalid format"
171-
);
142+
.assert()
143+
.failure()
144+
.stderr(
145+
predicate::str::contains("invalid").or(predicate::str::contains("possible values")),
146+
);
172147
}
173148

174149
#[test]
175150
fn test_ide_json_contains_expected_functions() {
176-
let output = Command::new(get_binary_path())
151+
let output = tmpltool()
177152
.args(["--ide", "json"])
178153
.output()
179154
.expect("Failed to execute command");
@@ -201,7 +176,7 @@ fn test_ide_json_contains_expected_functions() {
201176

202177
#[test]
203178
fn test_ide_json_categories_present() {
204-
let output = Command::new(get_binary_path())
179+
let output = tmpltool()
205180
.args(["--ide", "json"])
206181
.output()
207182
.expect("Failed to execute command");

0 commit comments

Comments
 (0)