|
| 1 | +import { spawn, execSync } from "child_process"; |
| 2 | +import chalk from "chalk"; |
| 3 | +import os from "os"; |
| 4 | +import { existsSync } from "fs"; |
| 5 | +import { dirname } from "path"; |
| 6 | + |
| 7 | +let ENV_VARS = {}; |
| 8 | + |
| 9 | +// Helper function to set environment variables |
| 10 | +function setEnvVariables() { |
| 11 | + const rustSysroot = execSync("rustc --print sysroot").toString().trim(); |
| 12 | + const rustNightlySysroot = execSync("rustc +nightly --print sysroot") |
| 13 | + .toString() |
| 14 | + .trim(); |
| 15 | + |
| 16 | + const platform = os.platform(); |
| 17 | + if (platform === "darwin") { |
| 18 | + console.log("🍎 Detected macOS"); |
| 19 | + ENV_VARS.DYLD_LIBRARY_PATH = `${rustSysroot}/lib:${rustNightlySysroot}/lib`; |
| 20 | + } else if (platform === "win32") { |
| 21 | + console.log("🪟 Detected Windows"); |
| 22 | + ENV_VARS.PATH += `;${process.env.PATH};${rustSysroot}/lib;${rustNightlySysroot}/lib`; |
| 23 | + } else if (platform === "linux") { |
| 24 | + console.log("🐧 Detected Linux"); |
| 25 | + ENV_VARS.LD_LIBRARY_PATH = `${rustSysroot}/lib:${rustNightlySysroot}/lib`; |
| 26 | + } else { |
| 27 | + console.log(`❌ Unsupported platform: ${platform}`); |
| 28 | + process.exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + console.log("\nEnvironment Variables Summary:"); |
| 32 | + for (const [key, value] of Object.entries(ENV_VARS)) { |
| 33 | + console.log(`${key}: ${value}`); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Helper function to run a command and capture output |
| 38 | +function runCommand(command) { |
| 39 | + try { |
| 40 | + const child = spawn(command, [], { |
| 41 | + shell: true, |
| 42 | + stdio: ["inherit", "pipe", "pipe"], |
| 43 | + env: { |
| 44 | + SOPRINTLN: "1", |
| 45 | + PATH: process.env.PATH, |
| 46 | + ...ENV_VARS, |
| 47 | + }, |
| 48 | + }); |
| 49 | + console.log("Set ENV_VARS to: ", ENV_VARS); |
| 50 | + |
| 51 | + let output = ""; |
| 52 | + |
| 53 | + child.stdout.on("data", (data) => { |
| 54 | + process.stdout.write(data); |
| 55 | + output += data; |
| 56 | + }); |
| 57 | + |
| 58 | + child.stderr.on("data", (data) => { |
| 59 | + process.stderr.write(data); |
| 60 | + output += data; |
| 61 | + }); |
| 62 | + |
| 63 | + return new Promise((resolve) => { |
| 64 | + child.on("close", (code) => { |
| 65 | + resolve({ |
| 66 | + success: code === 0, |
| 67 | + output: output, |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + } catch (error) { |
| 72 | + process.stderr.write(chalk.red(error.toString())); |
| 73 | + return Promise.resolve({ |
| 74 | + success: false, |
| 75 | + output: error.toString(), |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Helper function to check for feature mismatch |
| 81 | +function checkFeatureMismatch(output) { |
| 82 | + return output.includes("feature mismatch for crate"); |
| 83 | +} |
| 84 | + |
| 85 | +// Test cases |
| 86 | +const testCases = [ |
| 87 | + { |
| 88 | + name: "Tests pass (debug)", |
| 89 | + command: "cargo run --manifest-path test-crates/samplebin/Cargo.toml", |
| 90 | + expectedResult: "success", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "Tests pass (release)", |
| 94 | + command: |
| 95 | + "cargo run --manifest-path test-crates/samplebin/Cargo.toml --release", |
| 96 | + expectedResult: "success", |
| 97 | + }, |
| 98 | + { |
| 99 | + 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", |
| 102 | + expectedResult: "fail", |
| 103 | + checkFeatureMismatch: true, |
| 104 | + }, |
| 105 | + { |
| 106 | + 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", |
| 109 | + expectedResult: "fail", |
| 110 | + checkFeatureMismatch: true, |
| 111 | + }, |
| 112 | + { |
| 113 | + 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", |
| 116 | + expectedResult: "success", |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "Bin has mokio-timer feature (should fail)", |
| 120 | + command: |
| 121 | + "cargo run --features=exports/mokio-timer --manifest-path test-crates/samplebin/Cargo.toml", |
| 122 | + expectedResult: "fail", |
| 123 | + checkFeatureMismatch: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + 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", |
| 129 | + expectedResult: "fail", |
| 130 | + checkFeatureMismatch: true, |
| 131 | + }, |
| 132 | + { |
| 133 | + 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", |
| 136 | + expectedResult: "fail", |
| 137 | + checkFeatureMismatch: true, |
| 138 | + }, |
| 139 | + { |
| 140 | + 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", |
| 143 | + expectedResult: "fail", |
| 144 | + checkFeatureMismatch: true, |
| 145 | + }, |
| 146 | + { |
| 147 | + 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", |
| 150 | + expectedResult: "success", |
| 151 | + }, |
| 152 | +]; |
| 153 | + |
| 154 | +// Main function to run tests |
| 155 | +async function runTests() { |
| 156 | + console.log(chalk.blue("Changing working directory to Git root...")); |
| 157 | + let currentDir = process.cwd(); |
| 158 | + |
| 159 | + while (!existsSync(`${currentDir}/.git`)) { |
| 160 | + const parentDir = dirname(currentDir); |
| 161 | + if (parentDir === currentDir) { |
| 162 | + console.log(chalk.red("Git root not found. Exiting.")); |
| 163 | + process.exit(1); |
| 164 | + } |
| 165 | + currentDir = parentDir; |
| 166 | + } |
| 167 | + process.chdir(currentDir); |
| 168 | + console.log(chalk.green(`Changed working directory to: ${currentDir}`)); |
| 169 | + console.log(chalk.blue("Checking Rust version and toolchain...")); |
| 170 | + console.log(chalk.yellow("rustc --version:")); |
| 171 | + await runCommand("rustc --version"); |
| 172 | + console.log(chalk.yellow("\nrustup which rustc:")); |
| 173 | + await runCommand("rustup which rustc"); |
| 174 | + console.log(""); |
| 175 | + |
| 176 | + console.log(chalk.blue("Setting up environment variables...")); |
| 177 | + setEnvVariables(); |
| 178 | + |
| 179 | + console.log(chalk.blue("Installing nightly Rust...")); |
| 180 | + await runCommand("rustup toolchain add nightly"); |
| 181 | + |
| 182 | + console.log(chalk.blue("Running tests...")); |
| 183 | + for (const [index, test] of testCases.entries()) { |
| 184 | + console.log(chalk.yellow(`\nRunning test ${index + 1}: ${test.name}`)); |
| 185 | + const { success, output } = await runCommand(test.command); |
| 186 | + |
| 187 | + if (test.expectedResult === "success" && success) { |
| 188 | + console.log(chalk.green("Test passed as expected.")); |
| 189 | + } else if (test.expectedResult === "fail" && !success) { |
| 190 | + if (test.checkFeatureMismatch && checkFeatureMismatch(output)) { |
| 191 | + console.log( |
| 192 | + chalk.green("Test failed with feature mismatch as expected."), |
| 193 | + ); |
| 194 | + } else { |
| 195 | + console.log( |
| 196 | + chalk.red( |
| 197 | + "Test failed, but not with the expected feature mismatch error.", |
| 198 | + ), |
| 199 | + ); |
| 200 | + } |
| 201 | + } else { |
| 202 | + console.log( |
| 203 | + chalk.red( |
| 204 | + `Test result unexpected. Expected ${test.expectedResult}, but got ${success ? "success" : "failure"}.`, |
| 205 | + ), |
| 206 | + ); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +// Run the tests |
| 212 | +runTests().catch((error) => { |
| 213 | + console.error(chalk.red(`An error occurred: ${error.message}`)); |
| 214 | + process.exit(1); |
| 215 | +}); |
0 commit comments