|
| 1 | +#![cfg(not(windows))] |
| 2 | +#![allow(missing_docs)] |
| 3 | + |
| 4 | +use std::io::BufReader; |
| 5 | +use std::path::PathBuf; |
| 6 | +use std::process::{Command, Stdio}; |
| 7 | + |
| 8 | +use cargo_metadata::Message; |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn hello_world_stubs() { |
| 12 | + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 13 | + let workspace_root = manifest_dir |
| 14 | + .parent() |
| 15 | + .and_then(|p| p.parent()) |
| 16 | + .expect("Failed to find workspace root"); |
| 17 | + |
| 18 | + // Use a separate target directory to avoid polluting the main target/ |
| 19 | + // with artifacts built under different feature sets (which causes |
| 20 | + // "multiple candidates for rmeta dependency" errors in other tests). |
| 21 | + let target_dir = workspace_root.join("target").join("tests"); |
| 22 | + |
| 23 | + // Build the hello_world example as cdylib |
| 24 | + let build_output = Command::new("cargo") |
| 25 | + .current_dir(workspace_root) |
| 26 | + .env("CARGO_TARGET_DIR", &target_dir) |
| 27 | + .args([ |
| 28 | + "build", |
| 29 | + "--example", |
| 30 | + "hello_world", |
| 31 | + "--message-format=json-render-diagnostics", |
| 32 | + ]) |
| 33 | + .stdout(Stdio::piped()) |
| 34 | + .stderr(Stdio::inherit()) |
| 35 | + .output() |
| 36 | + .expect("Failed to run cargo build"); |
| 37 | + |
| 38 | + assert!( |
| 39 | + build_output.status.success(), |
| 40 | + "Failed to build hello_world example: {}", |
| 41 | + String::from_utf8_lossy(&build_output.stdout) |
| 42 | + ); |
| 43 | + |
| 44 | + let reader = BufReader::new(build_output.stdout.as_slice()); |
| 45 | + let lib_path = Message::parse_stream(reader) |
| 46 | + .filter_map(Result::ok) |
| 47 | + .find_map(|msg| match msg { |
| 48 | + Message::CompilerArtifact(artifact) if artifact.target.name == "hello_world" => { |
| 49 | + artifact |
| 50 | + .filenames |
| 51 | + .into_iter() |
| 52 | + .find(|f| f.extension() == Some(std::env::consts::DLL_EXTENSION)) |
| 53 | + } |
| 54 | + _ => None, |
| 55 | + }) |
| 56 | + .expect("Failed to find hello_world cdylib artifact in cargo output"); |
| 57 | + |
| 58 | + // Run cargo-php stubs --stdout against the built cdylib |
| 59 | + let stubs_output = Command::new("cargo") |
| 60 | + .current_dir(workspace_root) |
| 61 | + .env("CARGO_TARGET_DIR", &target_dir) |
| 62 | + .args(["run", "-p", "cargo-php", "--", "stubs", "--stdout"]) |
| 63 | + .arg(lib_path.as_str()) |
| 64 | + .output() |
| 65 | + .expect("Failed to run cargo-php stubs"); |
| 66 | + |
| 67 | + assert!( |
| 68 | + stubs_output.status.success(), |
| 69 | + "cargo-php stubs failed: {}", |
| 70 | + String::from_utf8_lossy(&stubs_output.stderr) |
| 71 | + ); |
| 72 | + |
| 73 | + let stubs = String::from_utf8(stubs_output.stdout).expect("Invalid UTF-8 in stubs output"); |
| 74 | + insta::assert_snapshot!(stubs); |
| 75 | +} |
0 commit comments