Skip to content

Commit 66a53f5

Browse files
committed
extract and test cache checking
1 parent 76f63ae commit 66a53f5

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

lychee-bin/src/commands/check.rs

+70-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use reqwest::Url;
1010
use tokio::sync::mpsc;
1111
use tokio_stream::wrappers::ReceiverStream;
1212

13-
use lychee_lib::{Client, Request, Response};
13+
use lychee_lib::{Client, Request, Response, Uri};
1414
use lychee_lib::{InputSource, Result};
1515
use lychee_lib::{ResponseBody, Status};
1616

@@ -275,22 +275,25 @@ async fn handle(
275275
// - Skip caching excluded links; they might not be excluded in the next run.
276276
// - Skip caching links for which the status code has been explicitly excluded from the cache.
277277
let status = response.status();
278+
if ignore_cache(&uri, status, cache_exclude_status) {
279+
return response;
280+
}
281+
282+
cache.insert(uri, status.into());
283+
response
284+
}
285+
286+
fn ignore_cache(uri: &Uri, status: &Status, cache_exclude_status: HashSet<u16>) -> bool {
278287
let status_code = match status.code() {
279288
None => 0,
280289
Some(code) => code.as_u16(),
281290
};
282291

283-
if uri.is_file()
292+
return uri.is_file()
284293
|| status.is_excluded()
285294
|| status.is_unsupported()
286295
|| status.is_unknown()
287-
|| cache_exclude_status.contains(&status_code)
288-
{
289-
return response;
290-
}
291-
292-
cache.insert(uri, status.into());
293-
response
296+
|| cache_exclude_status.contains(&status_code);
294297
}
295298

296299
fn show_progress(
@@ -338,9 +341,10 @@ fn get_failed_urls(stats: &mut ResponseStats) -> Vec<(InputSource, Url)> {
338341

339342
#[cfg(test)]
340343
mod tests {
344+
use http::StatusCode;
341345
use log::info;
342346

343-
use lychee_lib::{CacheStatus, InputSource, ResponseBody, Uri};
347+
use lychee_lib::{CacheStatus, ErrorKind, InputSource, ResponseBody, Uri};
344348

345349
use crate::formatters;
346350

@@ -389,4 +393,60 @@ mod tests {
389393
let buf = String::from_utf8_lossy(&buf);
390394
assert_eq!(buf, "↻ [200] http://127.0.0.1/ | Cached: OK (cached)\n");
391395
}
396+
397+
#[test]
398+
fn test_ignore_cache() {
399+
let mut exclude = HashSet::new();
400+
401+
// Cache is not ignored
402+
assert_eq!(
403+
false,
404+
ignore_cache(
405+
&Uri::try_from("https://[::1]").unwrap(),
406+
&Status::Ok(StatusCode::OK),
407+
exclude.clone()
408+
)
409+
);
410+
411+
// Cache is ignored for file URLs
412+
assert_eq!(
413+
true,
414+
ignore_cache(
415+
&Uri::try_from("file:///home").unwrap(),
416+
&Status::Ok(StatusCode::OK),
417+
exclude.clone()
418+
)
419+
);
420+
421+
// Cache is ignored for unsupported status
422+
assert_eq!(
423+
true,
424+
ignore_cache(
425+
&Uri::try_from("https://[::1]").unwrap(),
426+
&Status::Unsupported(ErrorKind::EmptyUrl),
427+
exclude.clone()
428+
)
429+
);
430+
431+
// Cache is ignored for unknown status
432+
assert_eq!(
433+
true,
434+
ignore_cache(
435+
&Uri::try_from("https://[::1]").unwrap(),
436+
&Status::UnknownStatusCode(StatusCode::IM_A_TEAPOT),
437+
exclude.clone()
438+
)
439+
);
440+
441+
// Cache is ignored for excluded status codes
442+
exclude.insert(200);
443+
assert_eq!(
444+
true,
445+
ignore_cache(
446+
&Uri::try_from("https://[::1]").unwrap(),
447+
&Status::Ok(StatusCode::OK),
448+
exclude.clone()
449+
)
450+
);
451+
}
392452
}

0 commit comments

Comments
 (0)