Skip to content

Commit f92e082

Browse files
tests refinement
1 parent 327500d commit f92e082

File tree

6 files changed

+376
-28
lines changed

6 files changed

+376
-28
lines changed

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test *args:
88
BIN_CHANNEL="${BIN_CHANNEL:-stable}"
99
BIN_FLAGS="${BIN_FLAGS:-}"
1010

11-
SOPRINTLN=1 cargo "+${BIN_CHANNEL}" build --manifest-path test-crates/samplebin/Cargo.toml "${BIN_FLAGS}"
11+
SOPRINTLN=1 cargo "+${BIN_CHANNEL}" build --manifest-path test-crates/samplebin/Cargo.toml ${BIN_FLAGS}
1212

1313
export DYLD_LIBRARY_PATH=$(rustc "+stable" --print sysroot)/lib
1414
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$(rustc "+nightly" --print sysroot)/lib

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
target

tests/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "tests"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]

tests/index.mjs

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { spawn, execSync } from "child_process";
22
import chalk from "chalk";
33
import os from "os";
44
import { existsSync } from "fs";
5-
import { dirname } from "path";
5+
import { dirname, join } from "path";
66

77
let ENV_VARS = {};
88

@@ -37,16 +37,17 @@ function setEnvVariables() {
3737
// Helper function to run a command and capture output
3838
function runCommand(command) {
3939
try {
40+
let env = {
41+
SOPRINTLN: "1",
42+
PATH: process.env.PATH,
43+
...ENV_VARS,
44+
};
45+
console.log("Running with env: ", env);
4046
const child = spawn(command, [], {
4147
shell: true,
4248
stdio: ["inherit", "pipe", "pipe"],
43-
env: {
44-
SOPRINTLN: "1",
45-
PATH: process.env.PATH,
46-
...ENV_VARS,
47-
},
49+
env,
4850
});
49-
console.log("Set ENV_VARS to: ", ENV_VARS);
5051

5152
let output = "";
5253

@@ -86,67 +87,85 @@ function checkFeatureMismatch(output) {
8687
const testCases = [
8788
{
8889
name: "Tests pass (debug)",
89-
command: "cargo run --manifest-path test-crates/samplebin/Cargo.toml",
90+
buildCommand:
91+
"cargo build --manifest-path test-crates/samplebin/Cargo.toml",
92+
runCommand: "./test-crates/samplebin/target/debug/samplebin",
9093
expectedResult: "success",
9194
},
9295
{
9396
name: "Tests pass (release)",
94-
command:
95-
"cargo run --manifest-path test-crates/samplebin/Cargo.toml --release",
97+
buildCommand:
98+
"cargo build --manifest-path test-crates/samplebin/Cargo.toml --release",
99+
runCommand: "./test-crates/samplebin/target/release/samplebin",
96100
expectedResult: "success",
97101
},
98102
{
99103
name: "Bin stable, mod_a nightly (should fail)",
100-
command:
101-
"cargo +stable run --manifest-path test-crates/samplebin/Cargo.toml -- --channel:mod_a=nightly",
104+
buildCommand:
105+
"cargo +stable build --manifest-path test-crates/samplebin/Cargo.toml",
106+
runCommand:
107+
"./test-crates/samplebin/target/debug/samplebin --channel:mod_a=nightly",
102108
expectedResult: "fail",
103109
checkFeatureMismatch: true,
104110
},
105111
{
106112
name: "Bin nightly, mod_a stable (should fail)",
107-
command:
108-
"cargo +nightly run --manifest-path test-crates/samplebin/Cargo.toml -- --channel:mod_a=stable",
113+
buildCommand:
114+
"cargo +nightly build --manifest-path test-crates/samplebin/Cargo.toml",
115+
runCommand:
116+
"./test-crates/samplebin/target/debug/samplebin --channel:mod_a=stable",
109117
expectedResult: "fail",
110118
checkFeatureMismatch: true,
111119
},
112120
{
113121
name: "All nightly (should work)",
114-
command:
115-
"cargo +nightly run --manifest-path test-crates/samplebin/Cargo.toml -- --channel:mod_a=nightly --channel:mod_b=nightly",
122+
buildCommand:
123+
"cargo +nightly build --manifest-path test-crates/samplebin/Cargo.toml",
124+
runCommand:
125+
"./test-crates/samplebin/target/debug/samplebin --channel:mod_a=nightly --channel:mod_b=nightly",
116126
expectedResult: "success",
117127
},
118128
{
119129
name: "Bin has mokio-timer feature (should fail)",
120-
command:
121-
"cargo run --features=exports/mokio-timer --manifest-path test-crates/samplebin/Cargo.toml",
130+
buildCommand:
131+
"cargo build --features=exports/mokio-timer --manifest-path test-crates/samplebin/Cargo.toml",
132+
runCommand: "./test-crates/samplebin/target/debug/samplebin",
122133
expectedResult: "fail",
123134
checkFeatureMismatch: true,
124135
},
125136
{
126137
name: "mod_a has mokio-timer feature (should fail)",
127-
command:
128-
"cargo run --manifest-path test-crates/mod_a/Cargo.toml -- --features:mod_a=mokio/timer",
138+
buildCommand:
139+
"cargo build --manifest-path test-crates/samplebin/Cargo.toml",
140+
runCommand:
141+
"./test-crates/samplebin/target/debug/samplebin --features:mod_a=mokio/timer",
129142
expectedResult: "fail",
130143
checkFeatureMismatch: true,
131144
},
132145
{
133146
name: "mod_b has mokio-timer feature (should fail)",
134-
command:
135-
"cargo run --manifest-path test-crates/mod_b/Cargo.toml -- --features:mod_b=mokio/timer",
147+
buildCommand:
148+
"cargo build --manifest-path test-crates/samplebin/Cargo.toml",
149+
runCommand:
150+
"./test-crates/samplebin/target/debug/samplebin --features:mod_b=mokio/timer",
136151
expectedResult: "fail",
137152
checkFeatureMismatch: true,
138153
},
139154
{
140155
name: "all mods have mokio-timer feature (should fail)",
141-
command:
142-
"cargo run --manifest-path test-crates/samplebin/Cargo.toml -- --features:mod_a=mokio/timer --features:mod_b=mokio/timer",
156+
buildCommand:
157+
"cargo build --manifest-path test-crates/samplebin/Cargo.toml",
158+
runCommand:
159+
"./test-crates/samplebin/target/debug/samplebin --features:mod_a=mokio/timer --features:mod_b=mokio/timer",
143160
expectedResult: "fail",
144161
checkFeatureMismatch: true,
145162
},
146163
{
147164
name: "bin and mods have mokio-timer feature (should work)",
148-
command:
149-
"cargo run --features=exports/mokio-timer --manifest-path test-crates/samplebin/Cargo.toml -- --features:mod_a=mokio/timer --features:mod_b=mokio/timer",
165+
buildCommand:
166+
"cargo build --features=exports/mokio-timer --manifest-path test-crates/samplebin/Cargo.toml",
167+
runCommand:
168+
"./test-crates/samplebin/target/debug/samplebin --features:mod_a=mokio/timer --features:mod_b=mokio/timer",
150169
expectedResult: "success",
151170
},
152171
];
@@ -182,7 +201,16 @@ async function runTests() {
182201
console.log(chalk.blue("Running tests..."));
183202
for (const [index, test] of testCases.entries()) {
184203
console.log(chalk.yellow(`\nRunning test ${index + 1}: ${test.name}`));
185-
const { success, output } = await runCommand(test.command);
204+
205+
console.log(chalk.cyan("Building..."));
206+
const buildResult = await runCommand(test.buildCommand);
207+
if (!buildResult.success) {
208+
console.log(chalk.red("Build failed. Exiting tests."));
209+
process.exit(1);
210+
}
211+
212+
console.log(chalk.cyan("Running..."));
213+
const { success, output } = await runCommand(test.runCommand);
186214

187215
if (test.expectedResult === "success" && success) {
188216
console.log(chalk.green("Test passed as expected."));
@@ -197,15 +225,19 @@ async function runTests() {
197225
"Test failed, but not with the expected feature mismatch error.",
198226
),
199227
);
228+
process.exit(1);
200229
}
201230
} else {
202231
console.log(
203232
chalk.red(
204233
`Test result unexpected. Expected ${test.expectedResult}, but got ${success ? "success" : "failure"}.`,
205234
),
206235
);
236+
process.exit(1);
207237
}
208238
}
239+
240+
console.log(chalk.green("All tests passed successfully."));
209241
}
210242

211243
// Run the tests

0 commit comments

Comments
 (0)