fix: treat a fragment in an empty directory as an error (#1756)

* fix: treat a fragment in an empty directory as an error
* test: add more fragment tests
This commit is contained in:
ocavue 2025-07-04 18:25:57 +10:00 committed by GitHub
parent 6bcb37c2dc
commit 81f2605118
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 11 deletions

View file

@ -17,6 +17,7 @@ This is a test file for the fragment loader.
[Link to missing fragment in file2](file2.md#missing-fragment)
### `Code` ``Heading
[Link to code heading](#code-heading)
## HTML Fragments
@ -66,7 +67,27 @@ without related HTML element. Browser will scroll to the top of the page.
A link to the non-existing fragment: [try](https://github.com/lycheeverse/lychee#non-existent-anchor).
Skip the fragment check for directories like: [empty](empty_dir/).
# Sub directory
- Link to a sub directory
- Good: [With trailing slash](sub_dir/)
- Good: [Without trailing slash](sub_dir)
- Link to a fragment to index.html in sub directory
- Good: [With trailing slash](sub_dir/#a-link-inside-index-html-inside-sub-dir)
- Good: [Without trailing slash](sub_dir#a-link-inside-index-html-inside-sub-dir)
- Link to a non-existing fragment in a sub directory
- Bad: [With trailing slash](sub_dir/#non-existing-fragment-1)
- Bad: [Without trailing slash](sub_dir#non-existing-fragment-2)
- Link to a non-existing sub directory
- Bad: [With trailing slash](sub_dir_non_existing_1/)
- Bad: [Without trailing slash](sub_dir_non_existing_2)
- Link to a empty directory
- Bad: [With trailing slash](empty_dir/)
- Bad: [Without trailing slash](empty_dir)
- Link to a fragment in a non-existing sub directory
- Bad: [With trailing slash](empty_dir/#non-existing-fragment-3)
- Bad: [Without trailing slash](empty_dir#non-existing-fragment-4)
# Binary data URLs checks

10
fixtures/fragments/sub_dir/index.html vendored Normal file
View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>For Testing Fragments</title>
</head>
<body>
<p id="a-link-inside-index-html-inside-sub-dir">Foo</p>
</body>
</html>

View file

@ -1895,6 +1895,13 @@ mod cli {
.stderr(contains(
"https://github.com/lycheeverse/lychee#non-existent-anchor",
))
.stderr(contains("fixtures/fragments/sub_dir#non-existing-fragment-1"))
.stderr(contains("fixtures/fragments/sub_dir#non-existing-fragment-2"))
.stderr(contains("fixtures/fragments/sub_dir_non_existing_1"))
.stderr(contains("fixtures/fragments/sub_dir_non_existing_2"))
.stderr(contains("fixtures/fragments/empty_dir"))
.stderr(contains("fixtures/fragments/empty_dir#non-existing-fragment-3"))
.stderr(contains("fixtures/fragments/empty_dir#non-existing-fragment-4"))
.stderr(contains("fixtures/fragments/zero.bin"))
.stderr(contains("fixtures/fragments/zero.bin#"))
.stderr(contains(
@ -1907,10 +1914,10 @@ mod cli {
.stderr(contains(
"https://raw.githubusercontent.com/lycheeverse/lychee/master/fixtures/fragments/zero.bin#fragment",
))
.stdout(contains("34 Total"))
.stdout(contains("28 OK"))
.stdout(contains("42 Total"))
.stdout(contains("29 OK"))
// Failures because of missing fragments or failed binary body scan
.stdout(contains("6 Errors"));
.stdout(contains("13 Errors"));
}
#[test]

View file

@ -106,18 +106,11 @@ impl FileChecker {
/// Returns a `Status` indicating the result of the check.
async fn check_path(&self, path: &Path, uri: &Uri) -> Status {
let file_path = self.resolve_file_path(path);
let has_fragment = uri.url.fragment().is_some_and(|x| !x.is_empty());
// If file_path exists, check this file
if file_path.is_some() {
return self.check_file(&file_path.unwrap(), uri).await;
}
// If path is a directory, and we cannot find an index file inside it,
// and we don't have a fragment, just return success. This is for
// backward compatibility.
else if path.is_dir() && !has_fragment {
return Status::Ok(StatusCode::OK);
}
ErrorKind::InvalidFilePath(uri.clone()).into()
}