use std::ffi::OsStr;
use std::fmt;
use std::fs;
use std::path::{Component, Path, PathBuf};
#[cfg(test)]
use crate::build_support::{GoverningLock, LockPackage, CARGO_LOCK_ENV};
#[cfg(not(test))]
use crate::source_identity::{GoverningLock, LockPackage, CARGO_LOCK_ENV};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Refusal {
Empty,
NotAbsolute(PathBuf),
InsideCrate(PathBuf),
}
impl fmt::Display for Refusal {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => write!(formatter, "{CARGO_LOCK_ENV} is set to an empty value"),
Self::NotAbsolute(path) => write!(
formatter,
"{CARGO_LOCK_ENV}={} is not an absolute path (Cargo runs this build script in \
the crate's own directory, so a relative path would name a file there)",
path.display()
),
Self::InsideCrate(path) => write!(
formatter,
"{CARGO_LOCK_ENV}={} lies inside this crate's own directory, which holds no \
lockfile of the build that consumes the crate",
path.display()
),
}
}
}
fn lexical(path: &Path) -> PathBuf {
let mut folded = PathBuf::new();
for component in path.components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
folded.pop();
}
other => folded.push(other.as_os_str()),
}
}
folded
}
fn resolved(path: &Path) -> PathBuf {
fs::canonicalize(path).unwrap_or_else(|_| lexical(path))
}
pub fn refuse_named(value: &OsStr, manifest_dir: &Path) -> Option<Refusal> {
if value.is_empty() {
return Some(Refusal::Empty);
}
let path = Path::new(value);
if !path.is_absolute() {
return Some(Refusal::NotAbsolute(path.to_owned()));
}
let inside = resolved(path).starts_with(resolved(manifest_dir))
|| lexical(path).starts_with(lexical(manifest_dir));
inside.then(|| Refusal::InsideCrate(path.to_owned()))
}
pub fn holds_package(packages: &[LockPackage], name: &str) -> bool {
packages.iter().any(|package| package.name == name)
}
pub fn located_by(governing: &GoverningLock, named: bool, out_dir: &Path) -> &'static str {
match governing {
GoverningLock::Found(_) if named => "named",
GoverningLock::Found(path)
if out_dir
.ancestors()
.any(|ancestor| ancestor.join("Cargo.lock") == *path) =>
{
"out_dir"
}
GoverningLock::Found(_) => "manifest",
GoverningLock::JudgedRecordNamed(_) | GoverningLock::Missing => "none",
}
}
pub fn for_closure_readings(governing: GoverningLock, located: &str) -> GoverningLock {
if located == "manifest" {
GoverningLock::Missing
} else {
governing
}
}
pub fn unread_reason(
refusal: Option<&Refusal>,
named: Option<&Path>,
foreign: Option<&Path>,
governing: &GoverningLock,
) -> String {
if let Some(refusal) = refusal {
return refusal.to_string();
}
if let Some(path) = foreign {
return format!(
"the lockfile {} holds no package of this crate, so it is the lockfile of \
another build",
path.display()
);
}
match (governing, named) {
(GoverningLock::JudgedRecordNamed(path), _) => format!(
"{CARGO_LOCK_ENV} names {}, this package's own record of the judged build rather \
than a lockfile of this build",
path.display()
),
(GoverningLock::Found(path), _) => format!(
"the only lockfile found, {}, lies above this crate's own manifest, and the build \
cannot tell whether it governs this build",
path.display()
),
(GoverningLock::Missing, Some(path)) => format!(
"{CARGO_LOCK_ENV} names {}, which is not a file",
path.display()
),
(GoverningLock::Missing, None) => {
"no lockfile governing this build was located from OUT_DIR".to_owned()
}
}
}
#[cfg(test)]
mod tests {
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use super::{
for_closure_readings, holds_package, located_by, refuse_named, unread_reason,
GoverningLock, LockPackage, Refusal,
};
struct Layout {
_root: tempfile::TempDir,
crate_dir: PathBuf,
project: PathBuf,
}
fn layout() -> Layout {
let root = tempfile::tempdir().unwrap();
let crate_dir = root.path().join("registry/toktier-0.3.3");
let project = root.path().join("project");
fs::create_dir_all(crate_dir.join("data/build")).unwrap();
fs::create_dir_all(&project).unwrap();
for file in [
crate_dir.join("Cargo.lock"),
crate_dir.join("data/build/judged_dependencies.lock"),
project.join("Cargo.lock"),
] {
fs::write(file, "version = 4\n").unwrap();
}
#[cfg(unix)]
std::os::unix::fs::symlink(&crate_dir, project.join("linked-crate")).unwrap();
Layout {
_root: root,
crate_dir,
project,
}
}
fn refuse(value: &str, crate_dir: &Path) -> Option<Refusal> {
refuse_named(OsStr::new(value), crate_dir)
}
#[test]
fn relative_and_empty_values_are_refused() {
let layout = layout();
for value in [
"Cargo.lock",
"./Cargo.lock",
"../../Cargo.lock",
"project/Cargo.lock",
] {
assert_eq!(
refuse(value, &layout.crate_dir),
Some(Refusal::NotAbsolute(PathBuf::from(value))),
"{value}"
);
}
assert_eq!(refuse("", &layout.crate_dir), Some(Refusal::Empty));
for refusal in [
Refusal::Empty,
Refusal::NotAbsolute(PathBuf::from("Cargo.lock")),
] {
assert!(
refusal.to_string().contains("TOKTIER_CARGO_LOCK"),
"{refusal}"
);
}
assert!(Refusal::NotAbsolute(PathBuf::from("Cargo.lock"))
.to_string()
.contains("absolute path"));
}
#[test]
fn absolute_values_inside_the_crate_are_refused() {
let layout = layout();
let crate_dir = &layout.crate_dir;
let inside = [
crate_dir.join("Cargo.lock"),
crate_dir.join("data/build/judged_dependencies.lock"),
crate_dir.join("data/../Cargo.lock"),
layout
.project
.join("../registry/toktier-0.3.3/./Cargo.lock"),
crate_dir.join("missing/Cargo.lock"),
];
for path in inside {
let value = path.to_str().unwrap();
assert_eq!(
refuse(value, crate_dir),
Some(Refusal::InsideCrate(path.clone())),
"{value}"
);
}
#[cfg(unix)]
{
let linked = layout.project.join("linked-crate/Cargo.lock");
assert_eq!(
refuse(linked.to_str().unwrap(), crate_dir),
Some(Refusal::InsideCrate(linked.clone()))
);
}
assert!(Refusal::InsideCrate(crate_dir.join("Cargo.lock"))
.to_string()
.contains("inside this crate's own directory"));
}
#[test]
fn an_absolute_value_elsewhere_is_believed() {
let layout = layout();
for path in [
layout.project.join("Cargo.lock"),
layout.project.join("../project/Cargo.lock"),
layout.project.join("missing.lock"),
PathBuf::from("/nonexistent/Cargo.lock"),
] {
assert_eq!(
refuse(path.to_str().unwrap(), &layout.crate_dir),
None,
"{}",
path.display()
);
}
let sibling = layout
.crate_dir
.with_file_name("toktier-0.3.3-other/Cargo.lock");
assert_eq!(refuse(sibling.to_str().unwrap(), &layout.crate_dir), None);
}
#[test]
fn located_by_names_each_way_a_lockfile_was_found() {
let out_dir = Path::new("/work/app/target/release/build/toktier-0123/out");
let above_out_dir = PathBuf::from("/work/app/Cargo.lock");
let above_manifest = PathBuf::from("/src/toktier/Cargo.lock");
for path in [&above_out_dir, &above_manifest] {
assert_eq!(
located_by(&GoverningLock::Found(path.clone()), true, out_dir),
"named"
);
}
assert_eq!(
located_by(&GoverningLock::Found(above_out_dir), false, out_dir),
"out_dir"
);
assert_eq!(
located_by(&GoverningLock::Found(above_manifest), false, out_dir),
"manifest"
);
for named in [true, false] {
assert_eq!(
located_by(
&GoverningLock::JudgedRecordNamed(PathBuf::from("/x/judged.lock")),
named,
out_dir
),
"none"
);
assert_eq!(located_by(&GoverningLock::Missing, named, out_dir), "none");
}
}
#[test]
fn the_closure_readings_never_read_the_workspace_fallback() {
let path = || PathBuf::from("/src/toktier/Cargo.lock");
assert!(matches!(
for_closure_readings(GoverningLock::Found(path()), "manifest"),
GoverningLock::Missing
));
for located in ["named", "out_dir"] {
assert!(matches!(
for_closure_readings(GoverningLock::Found(path()), located),
GoverningLock::Found(found) if found == path()
));
}
assert!(matches!(
for_closure_readings(GoverningLock::JudgedRecordNamed(path()), "none"),
GoverningLock::JudgedRecordNamed(_)
));
assert!(matches!(
for_closure_readings(GoverningLock::Missing, "none"),
GoverningLock::Missing
));
}
fn package(name: &str, version: &str, source: Option<&str>) -> LockPackage {
LockPackage {
name: name.to_owned(),
version: version.to_owned(),
source: source.map(str::to_owned),
checksum: None,
dependencies: Vec::new(),
}
}
#[test]
fn only_a_lockfile_that_holds_this_crate_is_read() {
let registry = Some("registry+https://github.com/rust-lang/crates.io-index");
let git = Some("git+https://example.invalid/toktier#0123");
let cases = [
(
vec![
package("app", "0.1.0", None),
package("toktier", "0.3.3", registry),
],
true,
),
(vec![package("toktier", "0.3.3", None)], true),
(vec![package("toktier", "0.3.3", git)], true),
(vec![package("toktier", "0.3.3-rc.1+b7", None)], true),
(
vec![
package("toktier", "0.3.2", registry),
package("toktier", "0.3.3", registry),
],
true,
),
(
vec![
package("app", "0.3.3", None),
package("toktier-py", "0.3.3", None),
],
false,
),
(Vec::new(), false),
];
for (packages, expected) in cases {
assert_eq!(
holds_package(&packages, "toktier"),
expected,
"{:?}",
packages
.iter()
.map(|package| (&package.name, &package.version))
.collect::<Vec<_>>()
);
}
}
#[test]
fn each_unread_reading_says_why() {
let refused = Refusal::NotAbsolute(PathBuf::from("Cargo.lock"));
let named = Path::new("/nonexistent/Cargo.lock");
let found = GoverningLock::Found(PathBuf::from("/src/toktier/Cargo.lock"));
let judged = GoverningLock::JudgedRecordNamed(named.to_owned());
let foreign = Some(Path::new("/elsewhere/Cargo.lock"));
let missing = GoverningLock::Missing;
let cases = [
(
unread_reason(
Some(&refused),
Some(Path::new("Cargo.lock")),
None,
&missing,
),
"not an absolute path",
),
(
unread_reason(None, Some(named), None, &missing),
"which is not a file",
),
(
unread_reason(None, None, foreign, &missing),
"holds no package of this crate, so",
),
(
unread_reason(None, Some(named), None, &judged),
"record of the judged build",
),
(
unread_reason(None, None, None, &found),
"above this crate's own manifest",
),
(
unread_reason(None, None, None, &missing),
"no lockfile governing this build was located",
),
];
for (reason, expected) in cases {
assert!(reason.contains(expected), "{reason}");
}
}
}