tests.nixpkgs-check-by-name: Intermediate Symlink errors

This commit is contained in:
Silvan Mosberger 2023-10-19 23:07:54 +02:00
parent 9a3abc4383
commit 4897b63ae6
2 changed files with 47 additions and 18 deletions

View File

@ -6,6 +6,15 @@ use std::io;
use std::path::PathBuf;
pub enum CheckError {
OutsideSymlink {
relative_package_dir: PathBuf,
subpath: PathBuf,
},
UnresolvableSymlink {
relative_package_dir: PathBuf,
subpath: PathBuf,
io_error: io::Error,
},
CouldNotParseNix {
relative_package_dir: PathBuf,
subpath: PathBuf,
@ -47,6 +56,20 @@ impl CheckError {
impl fmt::Display for CheckError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CheckError::OutsideSymlink { relative_package_dir, subpath } =>
write!(
f,
"{}: Path {} is a symlink pointing to a path outside the directory of that package.",
relative_package_dir.display(),
subpath.display(),
),
CheckError::UnresolvableSymlink { relative_package_dir, subpath, io_error } =>
write!(
f,
"{}: Path {} is a symlink which cannot be resolved: {io_error}.",
relative_package_dir.display(),
subpath.display(),
),
CheckError::CouldNotParseNix { relative_package_dir, subpath, error } =>
write!(
f,

View File

@ -52,26 +52,28 @@ fn check_path<W: io::Write>(context: &mut PackageContext<W>, subpath: &Path) ->
if path.is_symlink() {
// Check whether the symlink resolves to outside the package directory
match path.canonicalize() {
let check_result = match path.canonicalize() {
Ok(target) => {
// No need to handle the case of it being inside the directory, since we scan through the
// entire directory recursively anyways
if let Err(_prefix_error) = target.strip_prefix(context.absolute_package_dir) {
context.error_writer.write(&format!(
"{}: Path {} is a symlink pointing to a path outside the directory of that package.",
context.relative_package_dir.display(),
subpath.display(),
))?;
CheckError::OutsideSymlink {
relative_package_dir: context.relative_package_dir.clone(),
subpath: subpath.to_path_buf(),
}
.into_result()
} else {
pass(())
}
}
Err(e) => {
context.error_writer.write(&format!(
"{}: Path {} is a symlink which cannot be resolved: {e}.",
context.relative_package_dir.display(),
subpath.display(),
))?;
Err(io_error) => CheckError::UnresolvableSymlink {
relative_package_dir: context.relative_package_dir.clone(),
subpath: subpath.to_path_buf(),
io_error,
}
}
.into_result(),
};
write_check_result(context.error_writer, check_result)?;
} else if path.is_dir() {
// Recursively check each entry
for entry in utils::read_dir_sorted(&path)? {
@ -81,15 +83,19 @@ fn check_path<W: io::Write>(context: &mut PackageContext<W>, subpath: &Path) ->
}
} else if path.is_file() {
// Only check Nix files
if let Some(ext) = path.extension() {
let check_result = if let Some(ext) = path.extension() {
if ext == OsStr::new("nix") {
let check_result = check_nix_file(context, subpath).context(format!(
check_nix_file(context, subpath).context(format!(
"Error while checking Nix file {}",
subpath.display()
));
write_check_result(context.error_writer, check_result)?;
))
} else {
pass(())
}
}
} else {
pass(())
};
write_check_result(context.error_writer, check_result)?;
} else {
// This should never happen, git doesn't support other file types
anyhow::bail!("Unsupported file type for path {}", subpath.display());