Skip to content

Commit c8cf3a0

Browse files
authored
Always check files explicitly if specified by user or matched by user glob (#1816)
See: lycheeverse/lychee-action#305
1 parent cef607c commit c8cf3a0

File tree

11 files changed

+152
-23
lines changed

11 files changed

+152
-23
lines changed

fixtures/example_dir/example.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="https://example.com/example_dir/html">This is an HTML file to test glob patterns in lychee.</a>

fixtures/example_dir/example.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example Markdown file to test glob patterns in lychee.
2+
3+
It contains a link: https://example.com/example_dir/md

fixtures/example_dir/example.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This is a TypeScript file to test glob patterns in lychee.
2+
// It contains a link: https://example.com/example_dir/ts

fixtures/example_dir/example.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
3+
const Example: React.FC = () => {
4+
return (
5+
<div>
6+
<h1>Example Page</h1>
7+
<a href="https://example.com/example_dir/tsx" target="_blank" rel="noopener noreferrer">
8+
Visit Example.com
9+
</a>
10+
</div>
11+
);
12+
};
13+
14+
export default Example;

fixtures/glob_dir/example.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="https://example.com/glob_dir/html">This is an HTML file to test glob patterns in lychee.</a>

fixtures/glob_dir/example.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example Markdown file to test glob patterns in lychee.
2+
3+
It contains a link: https://example.com/glob_dir/md

fixtures/glob_dir/example.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This is a TypeScript file to test glob patterns in lychee.
2+
// It contains a link: https://example.com/glob_dir/ts

fixtures/glob_dir/example.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
3+
const Example: React.FC = () => {
4+
return (
5+
<div>
6+
<h1>Example Page</h1>
7+
<a href="https://example.com/glob_dir/tsx" target="_blank" rel="noopener noreferrer">
8+
Visit Example.com
9+
</a>
10+
</div>
11+
);
12+
};
13+
14+
export default Example;

lychee-bin/src/commands/dump_inputs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) async fn dump_inputs(
1616
inputs: HashSet<Input>,
1717
output: Option<&PathBuf>,
1818
excluded_paths: &[String],
19-
valid_extensions: &FileExtensions,
19+
file_extensions: &FileExtensions,
2020
skip_hidden: bool,
2121
skip_gitignored: bool,
2222
) -> Result<ExitCode> {
@@ -34,7 +34,7 @@ pub(crate) async fn dump_inputs(
3434

3535
for input in inputs {
3636
let sources_stream = input.get_sources(
37-
valid_extensions.clone(),
37+
file_extensions.clone(),
3838
skip_hidden,
3939
skip_gitignored,
4040
&excluded_path_filter,

lychee-bin/tests/cli.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,4 +2622,104 @@ mod cli {
26222622
.stdout(contains("fixtures/invalid_utf8/index.html"))
26232623
.stdout(contains("fixtures/invalid_utf8/invalid_utf8.txt"));
26242624
}
2625+
2626+
/// Check that files specified via glob patterns are always checked
2627+
/// no matter their extension. I.e. extensions are ignored for files
2628+
/// explicitly specified by the user.
2629+
///
2630+
/// See https://github.com/lycheeverse/lychee-action/issues/305
2631+
#[test]
2632+
fn test_globbed_files_are_always_checked() {
2633+
let input = fixtures_path().join("glob_dir/**/*.tsx");
2634+
2635+
// The directory contains:
2636+
// - example.ts
2637+
// - example.tsx
2638+
// - example.md
2639+
// - example.html
2640+
// But the user only specified the .tsx file via the glob pattern.
2641+
main_command()
2642+
.arg("--verbose")
2643+
// Only check ts, js, and html files by default.
2644+
// However, all files explicitly specified by the user
2645+
// should always be checked so this should be ignored.
2646+
.arg("--extensions=ts,js,html")
2647+
.arg(input)
2648+
.assert()
2649+
.failure()
2650+
.stdout(contains("1 Total"))
2651+
.stderr(contains("https://example.com/glob_dir/tsx"));
2652+
}
2653+
2654+
#[test]
2655+
fn test_extensions_work_on_glob_files_directory() {
2656+
let input = fixtures_path().join("glob_dir");
2657+
2658+
// Make sure all files matching the given extensions are checked
2659+
// if we specify a directory (and not a glob pattern).
2660+
main_command()
2661+
.arg("--verbose")
2662+
.arg("--extensions=ts,html")
2663+
.arg(input)
2664+
.assert()
2665+
.failure()
2666+
.stdout(contains("2 Total"))
2667+
// Note: The space is intentional to avoid matching tsx.
2668+
.stderr(contains("https://example.com/glob_dir/ts "))
2669+
// TSX files are ignored because we did not specify
2670+
// that extension. So `https://example.com/tsx"` should be missing from the output.
2671+
.stderr(contains("https://example.com/glob_dir/tsx").not())
2672+
// Markdown is also ignored because we did not specify that extension.
2673+
.stderr(contains("https://example.com/glob_dir/md").not())
2674+
.stderr(contains("https://example.com/glob_dir/html"));
2675+
}
2676+
2677+
/// We define two inputs, one being a glob pattern, the other a directory path.
2678+
/// The extensions should only apply to the directory path, not the glob pattern.
2679+
#[test]
2680+
fn test_extensions_apply_to_files_not_globs() {
2681+
let glob_input = fixtures_path().join("glob_dir/**/*.tsx");
2682+
let dir_input = fixtures_path().join("example_dir");
2683+
2684+
main_command()
2685+
.arg("--verbose")
2686+
.arg("--extensions=html,md")
2687+
.arg(glob_input)
2688+
.arg(dir_input)
2689+
.assert()
2690+
.failure()
2691+
.stdout(contains("3 Total"))
2692+
// Only TSX files are matched by the glob pattern.
2693+
.stderr(contains("https://example.com/glob_dir/tsx"))
2694+
.stderr(contains("https://example.com/glob_dir/ts ").not())
2695+
.stderr(contains("https://example.com/glob_dir/md").not())
2696+
.stderr(contains("https://example.com/glob_dir/html").not())
2697+
// For the example_dir, the extensions should apply.
2698+
.stderr(contains("https://example.com/example_dir/html"))
2699+
.stderr(contains("https://example.com/example_dir/md"))
2700+
// TS files in example_dir are ignored because we did not specify that extension.
2701+
.stderr(contains("https://example.com/example_dir/ts ").not())
2702+
// TSX files in examle_dir are ignored because we did not specify that extension.
2703+
.stderr(contains("https://example.com/example_dir/tsx").not());
2704+
}
2705+
2706+
/// Individual files should always be checked, even if their
2707+
/// extension does not match the given extensions.
2708+
#[test]
2709+
fn test_file_inputs_always_get_checked_no_matter_their_extension() {
2710+
let ts_input_file = fixtures_path().join("glob_dir/example.ts");
2711+
let md_input_file = fixtures_path().join("glob_dir/example.md");
2712+
2713+
main_command()
2714+
.arg("--verbose")
2715+
.arg("--dump")
2716+
.arg("--extensions=html,md")
2717+
.arg(ts_input_file)
2718+
.arg(md_input_file)
2719+
.assert()
2720+
.success()
2721+
.stderr("") // Ensure stderr is empty
2722+
.stdout(contains("https://example.com/glob_dir/ts"))
2723+
.stdout(contains("https://example.com/glob_dir/md"));
2724+
}
26252725
}

0 commit comments

Comments
 (0)