Skip to content

Commit 73249e0

Browse files
authored
feat: improve InvalidIndexFile error to mention index file names (#1859)
* feat: improve InvalidIndexFile error to mention index file names this makes it clearer which files are causing the link check to fail. it also lets us customise the message in the special case of `--index-files ''`, which could otherwise be confusing with a generic message. the messages are: ``` [ERROR] file:///h | Cannot find index file within directory: An index file (index.html) is required [ERROR] file:///h | Cannot find index file within directory: An index file (index.html, or index.htm) is required [ERROR] file:///h | Cannot find index file within directory: An index file (index.html, index.htm, or index.md) is required ``` and for the `--index-files ''` case: ``` [ERROR] file:///h | Cannot find index file within directory: No directory links are allowed because index_files is defined and empty ``` i do wonder if this last error message is still confusing because of the "Cannot find index file within directory" text. maybe it should be changed to a more general "Cannot resolve link to local directory"? for comparison, the old message was always: ``` [ERROR] file:///h | Cannot find index file within directory: Index file not found in directory. Check if index.html or other index files exist ``` * move dropping into file.rs * add some tests * revert spurious
1 parent 216d4bc commit 73249e0

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

lychee-bin/tests/cli.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,12 +2546,12 @@ mod cli {
25462546
fn test_index_files_specified() {
25472547
let input = fixtures_path().join("filechecker/dir_links.md");
25482548

2549-
// passing `--index-files index.html` should reject all links
2549+
// passing `--index-files index.html,index.htm` should reject all links
25502550
// to /empty_dir because it doesn't have the index file
25512551
let result = main_command()
2552-
.arg(input)
2552+
.arg(&input)
25532553
.arg("--index-files")
2554-
.arg("index.html")
2554+
.arg("index.html,index.htm")
25552555
.arg("--verbose")
25562556
.assert()
25572557
.failure();
@@ -2561,7 +2561,18 @@ mod cli {
25612561
result
25622562
.stdout(contains("Cannot find index file").count(empty_dir_links))
25632563
.stdout(contains("/empty_dir").count(empty_dir_links))
2564+
.stdout(contains("(index.html, or index.htm)").count(empty_dir_links))
25642565
.stdout(contains(format!("{index_dir_links} OK")));
2566+
2567+
// within the error message, formatting of the index file name list should
2568+
// omit empty names.
2569+
main_command()
2570+
.arg(&input)
2571+
.arg("--index-files")
2572+
.arg(",index.html,,,index.htm,")
2573+
.assert()
2574+
.failure()
2575+
.stdout(contains("(index.html, or index.htm)").count(empty_dir_links));
25652576
}
25662577

25672578
#[test]
@@ -2601,7 +2612,7 @@ mod cli {
26012612
// passing an empty list to --index-files should reject /all/
26022613
// directory links.
26032614
let result = main_command()
2604-
.arg(input)
2615+
.arg(&input)
26052616
.arg("--index-files")
26062617
.arg("")
26072618
.assert()
@@ -2610,6 +2621,17 @@ mod cli {
26102621
let num_dir_links = 4;
26112622
result
26122623
.stdout(contains("Cannot find index file").count(num_dir_links))
2624+
.stdout(contains("No directory links are allowed").count(num_dir_links))
2625+
.stdout(contains("0 OK"));
2626+
2627+
// ... as should passing a number of empty index file names
2628+
main_command()
2629+
.arg(&input)
2630+
.arg("--index-files")
2631+
.arg(",,,,,")
2632+
.assert()
2633+
.failure()
2634+
.stdout(contains("No directory links are allowed").count(num_dir_links))
26132635
.stdout(contains("0 OK"));
26142636
}
26152637

lychee-lib/src/checker/file.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ impl FileChecker {
229229
None => &[".".to_owned()],
230230
};
231231

232+
let invalid_index_error = || {
233+
// Drop empty index file names. These will never be accepted as valid
234+
// index files, and doing this makes cleaner error reporting.
235+
let mut names = index_names_to_try.to_vec();
236+
names.retain(|x| !x.is_empty());
237+
238+
ErrorKind::InvalidIndexFile(names)
239+
};
240+
232241
index_names_to_try
233242
.iter()
234243
.find_map(|filename| {
@@ -242,7 +251,7 @@ impl FileChecker {
242251
let path = dir_path.join(filename);
243252
exists(&path).then_some(path)
244253
})
245-
.ok_or_else(|| ErrorKind::InvalidIndexFile(dir_path.to_path_buf()))
254+
.ok_or_else(invalid_index_error)
246255
}
247256

248257
/// Checks a resolved file, optionally verifying fragments for HTML files.

lychee-lib/src/types/error.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ pub enum ErrorKind {
7070
#[error("Cannot find fragment")]
7171
InvalidFragment(Uri),
7272

73-
/// The given directory is missing a required index file
73+
/// Cannot resolve local directory link using the configured index files
7474
#[error("Cannot find index file within directory")]
75-
InvalidIndexFile(PathBuf),
75+
InvalidIndexFile(Vec<String>),
7676

7777
/// The given path cannot be converted to a URI
7878
#[error("Invalid path to URL conversion: {0}")]
@@ -330,9 +330,11 @@ impl ErrorKind {
330330
ErrorKind::StatusCodeSelectorError(status_code_selector_error) => Some(format!(
331331
"Status code selector error: {status_code_selector_error}. Check accept configuration",
332332
)),
333-
ErrorKind::InvalidIndexFile(_path) => Some(
334-
"Index file not found in directory. Check if index.html or other index files exist".to_string()
335-
),
333+
ErrorKind::InvalidIndexFile(index_files) => match &index_files[..] {
334+
[] => "No directory links are allowed because index_files is defined and empty".to_string(),
335+
[name] => format!("An index file ({name}) is required"),
336+
[init @ .., tail] => format!("An index file ({}, or {}) is required", init.join(", "), tail),
337+
}.into()
336338
}
337339
}
338340

0 commit comments

Comments
 (0)