Skip to content

Commit

Permalink
Search for lockfile in manifest-ancestor-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslueg committed Apr 11, 2024
1 parent acc6475 commit bd869d1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ where
res
}

fn find_lockfile(base: &path::Path) -> io::Result<path::PathBuf> {
base.ancestors()
.find_map(|p| {
let lockfile = p.join("Cargo.lock");
lockfile.exists().then(|| lockfile.to_owned())
})
.ok_or(io::Error::other("Cargo.lock not found"))
}

#[cfg(feature = "dependency-tree")]
struct Dependencies {
deps: Vec<(String, String)>,
Expand Down Expand Up @@ -68,7 +77,7 @@ pub fn write_dependencies(manifest_location: &path::Path, mut w: &fs::File) -> i
use io::{Read, Write};

let mut lock_buf = String::new();
fs::File::open(manifest_location.join("Cargo.lock"))?.read_to_string(&mut lock_buf)?;
fs::File::open(find_lockfile(manifest_location)?)?.read_to_string(&mut lock_buf)?;
let lockfile = lock_buf.parse().expect("Failed to parse lockfile");

let dependencies = Dependencies::new(&lockfile);
Expand Down Expand Up @@ -138,7 +147,7 @@ pub fn write_dependencies(manifest_location: &path::Path, mut w: &fs::File) -> i
use io::{Read, Write};

let mut lock_buf = String::new();
fs::File::open(manifest_location.join("Cargo.lock"))?.read_to_string(&mut lock_buf)?;
fs::File::open(find_lockfile(manifest_location)?)?.read_to_string(&mut lock_buf)?;
let lockfile: cargo_lock::Lockfile = lock_buf.parse().expect("Failed to parse lockfile");

let deps = package_names(&lockfile.packages);
Expand Down
39 changes: 39 additions & 0 deletions tests/testbox_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ fn main() {
.unwrap();
for (name, content) in self.files {
let fname = self.root.path().join(name);
assert!(fname.is_absolute());
fs::create_dir_all(fname.parent().unwrap()).unwrap();
let mut file = fs::File::create(fname)?;
file.write_all(&content)?;
}
Expand Down Expand Up @@ -308,6 +310,43 @@ fn main() {
p.create_and_run(&[]);
}

#[test]
fn simple_workspace() {
let mut p = Project::new();
let built_root = get_built_root();

p.add_file("Cargo.toml", "[workspace]\nmembers = ['foobar']");
p.add_file(
"foobar/Cargo.toml",
format!(
r#"[package]
name = "foobar"
version = "5.6.7"
build = "build.rs"
[build-dependencies]
built = {{ path = {:?} }}"#,
&built_root
),
);
p.add_file(
"foobar/build.rs",
"fn main() { built::write_built_file().unwrap(); }",
);
p.add_file(
"foobar/src/main.rs",
r#"
mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
fn main() {
assert_eq!(built_info::PKG_VERSION, "5.6.7");
}
"#,
);
p.create_and_run(&[]);
}

#[test]
fn full_testbox() {
let mut p = Project::new();
Expand Down

0 comments on commit bd869d1

Please sign in to comment.