Skip to content

Commit

Permalink
Move root dir check
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Dec 12, 2024
1 parent 83b28c4 commit 909e8c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
8 changes: 1 addition & 7 deletions lychee-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,7 @@ fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {
async fn run(opts: &LycheeOptions) -> Result<i32> {
let inputs = opts.inputs()?;

if let Some(root_dir) = &opts.config.root_dir {
if root_dir.is_relative() {
bail!("`--root_dir` must be an absolute path");
}
}

let mut collector = Collector::new(opts.config.root_dir.clone(), opts.config.base.clone())
let mut collector = Collector::new(opts.config.root_dir.clone(), opts.config.base.clone())?
.skip_missing_inputs(opts.config.skip_missing)
.skip_hidden(!opts.config.hidden)
.skip_ignored(!opts.config.no_ignore)
Expand Down
49 changes: 29 additions & 20 deletions lychee-lib/src/collector.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ErrorKind;
use crate::InputSource;
use crate::{
basic_auth::BasicAuthExtractor, extract::Extractor, types::uri::raw::RawUri, utils::request,
Expand Down Expand Up @@ -43,9 +44,17 @@ impl Default for Collector {

impl Collector {
/// Create a new collector with an empty cache
#[must_use]
pub const fn new(root_dir: Option<PathBuf>, base: Option<Base>) -> Self {
Collector {
///
/// # Errors
///
/// Returns an `Err` if the `root_dir` is not an absolute path
pub fn new(root_dir: Option<PathBuf>, base: Option<Base>) -> Result<Self> {
if let Some(root_dir) = &root_dir {
if root_dir.is_relative() {
return Err(ErrorKind::RootDirMustBeAbsolute(root_dir.clone()));
}
}
Ok(Collector {
basic_auth_extractor: None,
skip_missing_inputs: false,
include_verbatim: false,
Expand All @@ -54,7 +63,7 @@ impl Collector {
skip_ignored: true,
root_dir,
base,
}
})
}

/// Skip missing input files (default is to error if they don't exist)
Expand Down Expand Up @@ -177,21 +186,21 @@ mod tests {
inputs: Vec<Input>,
root_dir: Option<PathBuf>,
base: Option<Base>,
) -> HashSet<Uri> {
let responses = Collector::new(root_dir, base).collect_links(inputs);
responses.map(|r| r.unwrap().uri).collect().await
) -> Result<HashSet<Uri>> {
let responses = Collector::new(root_dir, base)?.collect_links(inputs);
Ok(responses.map(|r| r.unwrap().uri).collect().await)
}

// Helper function for collecting verbatim links
async fn collect_verbatim(
inputs: Vec<Input>,
root_dir: Option<PathBuf>,
base: Option<Base>,
) -> HashSet<Uri> {
let responses = Collector::new(root_dir, base)
) -> Result<HashSet<Uri>> {
let responses = Collector::new(root_dir, base)?
.include_verbatim(true)
.collect_links(inputs);
responses.map(|r| r.unwrap().uri).collect().await
Ok(responses.map(|r| r.unwrap().uri).collect().await)
}

const TEST_STRING: &str = "http://test-string.com";
Expand Down Expand Up @@ -279,7 +288,7 @@ mod tests {
},
];

let links = collect_verbatim(inputs, None, None).await;
let links = collect_verbatim(inputs, None, None).await.ok().unwrap();

let expected_links = HashSet::from_iter([
website(TEST_STRING),
Expand All @@ -302,7 +311,7 @@ mod tests {
file_type_hint: Some(FileType::Markdown),
excluded_paths: None,
};
let links = collect(vec![input], None, Some(base)).await;
let links = collect(vec![input], None, Some(base)).await.ok().unwrap();

let expected_links = HashSet::from_iter([
website("https://endler.dev"),
Expand All @@ -328,7 +337,7 @@ mod tests {
file_type_hint: Some(FileType::Html),
excluded_paths: None,
};
let links = collect(vec![input], None, Some(base)).await;
let links = collect(vec![input], None, Some(base)).await.ok().unwrap();

let expected_links = HashSet::from_iter([
website("https://github.com/lycheeverse/lychee/"),
Expand Down Expand Up @@ -357,7 +366,7 @@ mod tests {
file_type_hint: Some(FileType::Html),
excluded_paths: None,
};
let links = collect(vec![input], None, Some(base)).await;
let links = collect(vec![input], None, Some(base)).await.ok().unwrap();

let expected_links = HashSet::from_iter([
website("https://example.com/static/image.png"),
Expand All @@ -384,7 +393,7 @@ mod tests {
excluded_paths: None,
};

let links = collect(vec![input], None, Some(base)).await;
let links = collect(vec![input], None, Some(base)).await.ok().unwrap();

let expected = HashSet::from_iter([
website("https://localhost.com/@/internal.md"),
Expand All @@ -406,7 +415,7 @@ mod tests {
file_type_hint: Some(FileType::Html),
excluded_paths: None,
};
let links = collect(vec![input], None, Some(base)).await;
let links = collect(vec![input], None, Some(base)).await.ok().unwrap();

let expected_links = HashSet::from_iter([
// the body links wouldn't be present if the file was parsed strictly as XML
Expand Down Expand Up @@ -439,7 +448,7 @@ mod tests {
excluded_paths: None,
};

let links = collect(vec![input], None, None).await;
let links = collect(vec![input], None, None).await.ok().unwrap();

let expected_urls = HashSet::from_iter([
website("https://github.com/lycheeverse/lychee/"),
Expand All @@ -458,7 +467,7 @@ mod tests {
file_type_hint: None,
excluded_paths: None,
};
let links = collect(vec![input], None, None).await;
let links = collect(vec![input], None, None).await.ok().unwrap();

let expected_links = HashSet::from_iter([mail("user@example.com")]);

Expand Down Expand Up @@ -501,7 +510,7 @@ mod tests {
},
];

let links = collect(inputs, None, None).await;
let links = collect(inputs, None, None).await.ok().unwrap();

let expected_links = HashSet::from_iter([
website(&format!(
Expand Down Expand Up @@ -535,7 +544,7 @@ mod tests {
excluded_paths: None,
};

let links = collect(vec![input], None, Some(base)).await;
let links = collect(vec![input], None, Some(base)).await.ok().unwrap();

let expected_links = HashSet::from_iter([
path("/path/to/root/index.html"),
Expand Down
5 changes: 5 additions & 0 deletions lychee-lib/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ pub enum ErrorKind {
#[error("Cannot convert path '{0}' to a URI")]
InvalidPathToUri(String),

/// Root dir must be an absolute path
#[error("Root dir must be an absolute path: '{0}'")]
RootDirMustBeAbsolute(PathBuf),

/// The given URI type is not supported
#[error("Unsupported URI type: '{0}'")]
UnsupportedUriType(String),
Expand Down Expand Up @@ -310,6 +314,7 @@ impl Hash for ErrorKind {
Self::InvalidBase(base, e) => (base, e).hash(state),
Self::InvalidBaseJoin(s) => s.hash(state),
Self::InvalidPathToUri(s) => s.hash(state),
Self::RootDirMustBeAbsolute(s) => s.hash(state),
Self::UnsupportedUriType(s) => s.hash(state),
Self::InvalidUrlRemap(remap) => (remap).hash(state),
Self::InvalidHeader(e) => e.to_string().hash(state),
Expand Down

0 comments on commit 909e8c3

Please sign in to comment.