Skip to content

Commit 250b901

Browse files
thunderseethecopybara-github
authored andcommitted
Add tests for cargo project with a dependency.
PiperOrigin-RevId: 895398959
1 parent 1d3a765 commit 250b901

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

cargo/cc_bindings_from_rs/cargo-cpp_api_from_rust/tests/test.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,35 @@ fn test_subcommand_caching() -> Result<(), Box<dyn std::error::Error>> {
180180

181181
Ok(())
182182
}
183+
184+
#[test] // allow_core_test
185+
fn test_subcommand_with_dependency() -> Result<(), Box<dyn std::error::Error>> {
186+
let tmp_dir = tempfile::tempdir()?;
187+
let cwd = std::env::current_dir()?;
188+
let project_dir = cwd.join("tests/test_with_dependency");
189+
190+
let mut cmd = setup_command(&tmp_dir, &project_dir);
191+
cmd.arg("cpp_api_from_rust");
192+
193+
let output = cmd.output().expect("Failed to execute");
194+
195+
if !output.status.success() {
196+
println!("{}", String::from_utf8_lossy(&output.stdout));
197+
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
198+
panic!("cargo-cpp_api_from_rust failed");
199+
}
200+
201+
// Verify output files
202+
let target_dir = tmp_dir.path();
203+
let debug_dir = target_dir.join("debug");
204+
let headers_dir = debug_dir.join("headers");
205+
206+
// We expect to find headers for both the root crate and its dependency.
207+
assert!(headers_dir.join("test_with_dependency.h").exists());
208+
assert!(headers_dir.join("test_dependency.h").exists());
209+
210+
// We expect the final staticlib for the root crate.
211+
assert!(debug_dir.join("libtest_with_dependency.a").exists());
212+
213+
Ok(())
214+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Part of the Crubit project, under the Apache License v2.0 with LLVM
2+
# Exceptions. See /LICENSE for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
[package]
5+
name = "test_with_dependency"
6+
version = "0.1.0"
7+
edition = "2024"
8+
9+
# This is needed to prevent this project from being part of the `crubit/` workspace.
10+
[workspace]
11+
12+
[dependencies]
13+
test_dependency = { path = "test_dependency" }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Part of the Crubit project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
pub fn add_with_dep(left: u64, right: u64) -> u64 {
6+
test_dependency::dependency_add(left, right)
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Part of the Crubit project, under the Apache License v2.0 with LLVM
2+
# Exceptions. See /LICENSE for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
[package]
5+
name = "test_dependency"
6+
version = "0.1.0"
7+
edition = "2024"
8+
9+
[dependencies]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Part of the Crubit project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
pub fn dependency_add(left: u64, right: u64) -> u64 {
6+
left + right
7+
}

cc_bindings_from_rs/cargo-cpp_api_from_rust.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ fn main() -> Result<()> {
192192
let rs_crate_name = crate_name.replace('-', "_");
193193
let hash = &artifact_info.hash;
194194
let intermediate_h = deps_dir.join(format!("{}-{}.h", crate_name, hash));
195+
let final_h = headers_dir.join(format!("{}.h", crate_name));
195196
let intermediate_rs = deps_dir.join(format!("lib{}-{}.rs", crate_name, hash));
196197

197198
if artifact_info.fresh && intermediate_h.exists() && intermediate_rs.exists() {
@@ -200,6 +201,10 @@ fn main() -> Result<()> {
200201
"#[path = {:?}]pub mod r#{};\n",
201202
intermediate_rs, rs_crate_name
202203
));
204+
// Final outputs: copy/rename from deps/ to their final locations.
205+
if !final_h.exists() {
206+
fs::copy(&intermediate_h, &final_h)?;
207+
}
203208
continue;
204209
}
205210

@@ -246,7 +251,7 @@ fn main() -> Result<()> {
246251
pkg_to_header.insert(pkg_id.repr, intermediate_h.to_string());
247252

248253
// Final outputs: copy/rename from deps/ to their final locations.
249-
fs::copy(&intermediate_h, headers_dir.join(format!("{}.h", crate_name)))?;
254+
fs::copy(&intermediate_h, &final_h)?;
250255
}
251256

252257
let root_name = &root.name;

0 commit comments

Comments
 (0)