Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Oct 16, 2024
1 parent 4f1bff7 commit 7a9347f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
14 changes: 10 additions & 4 deletions lychee-bin/src/formatters/response/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ mod tests {
}
}

#[cfg(test)]
/// Helper function to strip ANSI color codes for tests
fn strip_ansi_codes(s: &str) -> String {
console::strip_ansi_codes(s).to_string()
}

#[test]
fn test_format_response_with_ok_status() {
let formatter = ColorFormatter;
let body = mock_response_body(Status::Ok(StatusCode::OK), "https://example.com");
assert_eq!(
formatter.format_response(&body),
"\u{1b}[38;5;2m\u{1b}[1m [200]\u{1b}[0m https://example.com/"
strip_ansi_codes(&formatter.format_response(&body)),
" [200] https://example.com/"
);
}

Expand All @@ -83,8 +89,8 @@ mod tests {
"https://example.com/404",
);
assert_eq!(
formatter.format_response(&body),
"\u{1b}[38;5;197m [ERROR]\u{1b}[0m https://example.com/404"
strip_ansi_codes(&formatter.format_response(&body)),
" [ERROR] https://example.com/404"
);
}

Expand Down
61 changes: 27 additions & 34 deletions lychee-lib/src/checker/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{utils::fragment_checker::FragmentChecker, Base, ErrorKind, Status, Uri};
use http::StatusCode;
use log::warn;
use std::path::{Path, PathBuf};

use crate::{utils::fragment_checker::FragmentChecker, Base, ErrorKind, Status, Uri};

#[derive(Debug, Clone)]
pub(crate) struct FileChecker {
base: Option<Base>,
Expand Down Expand Up @@ -30,23 +31,28 @@ impl FileChecker {
return ErrorKind::InvalidFilePath(uri.clone()).into();
};

if path.is_absolute() {
let resolved_path = self.resolve_absolute_path(&path);
return self.check_resolved_path(&resolved_path, uri).await;
}

self.check_path(&path, uri).await
let resolved_path = self.resolve_path(&path);
self.check_path(&resolved_path, uri).await
}

async fn check_resolved_path(&self, path: &Path, uri: &Uri) -> Status {
if path.exists() {
if self.include_fragments {
self.check_fragment(path, uri).await
fn resolve_path(&self, path: &Path) -> PathBuf {
if let Some(Base::Local(base_path)) = &self.base {
if path.is_absolute() {
let absolute_base_path = if base_path.is_relative() {
std::env::current_dir()
.unwrap_or_else(|_| PathBuf::new())
.join(base_path)
} else {
base_path.clone()
};

let stripped = path.strip_prefix("/").unwrap_or(path);
absolute_base_path.join(stripped)
} else {
Status::Ok(StatusCode::OK)
base_path.join(path)
}
} else {
ErrorKind::InvalidFilePath(uri.clone()).into()
path.to_path_buf()
}
}

Expand All @@ -55,10 +61,6 @@ impl FileChecker {
return self.check_existing_path(path, uri).await;
}

if path.extension().is_some() {
return ErrorKind::InvalidFilePath(uri.clone()).into();
}

self.check_with_fallback_extensions(path, uri).await
}

Expand All @@ -72,30 +74,21 @@ impl FileChecker {

async fn check_with_fallback_extensions(&self, path: &Path, uri: &Uri) -> Status {
let mut path_buf = path.to_path_buf();

// If the path already has an extension, try it first
if path_buf.extension().is_some() && path_buf.exists() {
return self.check_existing_path(&path_buf, uri).await;
}

// Try fallback extensions
for ext in &self.fallback_extensions {
path_buf.set_extension(ext);
if path_buf.exists() {
return self.check_existing_path(&path_buf, uri).await;
}
}
ErrorKind::InvalidFilePath(uri.clone()).into()
}

fn resolve_absolute_path(&self, path: &Path) -> PathBuf {
if let Some(Base::Local(base_path)) = &self.base {
let absolute_base_path = if base_path.is_relative() {
std::env::current_dir()
.unwrap_or_else(|_| PathBuf::new())
.join(base_path)
} else {
base_path.to_path_buf()
};

let stripped = path.strip_prefix("/").unwrap_or(path);
absolute_base_path.join(stripped)
} else {
path.to_path_buf()
}
ErrorKind::InvalidFilePath(uri.clone()).into()
}

async fn check_fragment(&self, path: &Path, uri: &Uri) -> Status {
Expand Down
2 changes: 1 addition & 1 deletion lychee-lib/src/checker/website.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ impl Handler<Request, Status> for Checker {
async fn handle(&mut self, input: Request) -> ChainResult<Request, Status> {
ChainResult::Done(self.retry_request(input).await)
}
}
}

0 comments on commit 7a9347f

Please sign in to comment.