|
5 | 5 |
|
6 | 6 | use crate::{ResolveOptions, Resolver, TsconfigDiscovery, TsconfigOptions, TsconfigReferences}; |
7 | 7 |
|
8 | | -/// Main test for include/exclude/files pattern matching |
9 | | -/// Tests basic glob patterns, exclude filtering, and files field priority |
| 8 | +/// Test include/exclude/files patterns via actual path resolution |
| 9 | +/// Tests that tsconfig path mappings are applied when importer is included, |
| 10 | +/// and not applied when importer is excluded |
10 | 11 | #[test] |
11 | 12 | fn tsconfig_include_exclude_patterns() { |
12 | 13 | let f = super::fixture_root().join("tsconfig/cases"); |
13 | 14 |
|
14 | | - // (fixture_dir, file_path, should_match, description) |
| 15 | + // (fixture_dir, importer_file, specifier, should_resolve, description) |
15 | 16 | #[rustfmt::skip] |
16 | 17 | let test_cases = [ |
17 | 18 | // Include basic - Pattern: src/**/*.ts |
18 | | - ("include_basic", "src/index.ts", true, "include pattern matches file in src/"), |
19 | | - ("include_basic", "src/utils/helper.ts", true, "include pattern matches nested file"), |
20 | | - ("include_basic", "test.ts", false, "file outside include pattern"), |
21 | | - ("include_basic", "dist/output.js", false, "non-ts file not included"), |
| 19 | + // Files in src/ can use path mappings, files outside cannot |
| 20 | + ("include_basic", "src/index.ts", "@/utils/helper", true, "file in src/ can use path mapping"), |
| 21 | + ("include_basic", "test.ts", "@/utils/helper", false, "file outside include pattern cannot use path mapping"), |
22 | 22 |
|
23 | 23 | // Exclude basic - Include: **/*.ts, Exclude: **/*.test.ts |
24 | | - ("exclude_basic", "src/index.ts", true, "file matches include and not exclude"), |
25 | | - ("exclude_basic", "src/index.test.ts", false, "file excluded by exclude pattern"), |
26 | | - ("exclude_basic", "node_modules/foo.ts", false, "node_modules excluded by default"), |
27 | | - |
28 | | - // Files priority - Files: [test.ts], Exclude: [test.ts] |
29 | | - ("files_priority", "test.ts", true, "files field overrides exclude"), |
30 | | - ("files_priority", "other.ts", false, "file not in files array"), |
| 24 | + // Test files are excluded from using path mappings |
| 25 | + ("exclude_basic", "src/index.ts", "@/helper", true, "non-test file can use path mapping"), |
| 26 | + ("exclude_basic", "src/index.test.ts", "@/helper", false, "test file excluded from using path mapping"), |
31 | 27 |
|
32 | 28 | // Default include (no include specified, defaults to **/*) - Exclude: [dist] |
33 | | - ("with_baseurl", "index.ts", true, "default include matches all files"), |
34 | | - ("with_baseurl", "log.ts", true, "default include matches all files"), |
35 | | - ("with_baseurl", "dist/output.js", false, "dist directory excluded"), |
36 | | - |
37 | | - // Default exclude (node_modules, bower_components, jspm_packages) - Exclude: [dist] |
38 | | - ("without_baseurl", "index.ts", true, "regular files included"), |
39 | | - ("without_baseurl", "log.ts", true, "regular files included"), |
40 | | - ("without_baseurl", "node_modules/package/index.ts", false, "node_modules excluded by default"), |
41 | | - ("without_baseurl", "bower_components/lib.ts", false, "bower_components excluded by default"), |
42 | | - ("without_baseurl", "jspm_packages/mod.ts", false, "jspm_packages excluded by default"), |
43 | | - ("without_baseurl", "dist/output.js", false, "custom exclude pattern works"), |
44 | | - |
45 | | - // Template variable ${configDir} - Include: ${configDir}/*.ts |
46 | | - ("configdir_syntax", "index.ts", true, "${configDir} matches root level .ts files"), |
47 | | - ("configdir_syntax", "log.ts", true, "${configDir} matches root level .ts files"), |
48 | | - ("configdir_syntax", "dist/output.js", false, "dist excluded"), |
49 | | - ("configdir_syntax", "src/index.ts", false, "${configDir}/*.ts doesn't match subdirectories"), |
50 | | - |
51 | | - // Extends inheritance - Base: include **/*.ts, exclude **/*.test.ts; Child: include src/**/*.ts |
52 | | - ("include_exclude_extends", "src/index.ts", true, "child include pattern matches"), |
53 | | - ("include_exclude_extends", "lib/utils.ts", false, "child include overrides parent (lib not in src)"), |
54 | | - |
55 | | - // Globstar patterns - Include: **/*.ts |
56 | | - ("globstar_patterns", "index.ts", true, "globstar matches root level"), |
57 | | - ("globstar_patterns", "src/index.ts", true, "globstar matches one level deep"), |
58 | | - ("globstar_patterns", "src/utils/helper.ts", true, "globstar matches two levels deep"), |
59 | | - ("globstar_patterns", "src/utils/deep/nested/file.ts", true, "globstar matches deeply nested"), |
60 | | - ("globstar_patterns", "index.js", false, "globstar doesn't match .js files"), |
61 | | - ("globstar_patterns", "src/index.js", false, "globstar doesn't match .js files in subdirs"), |
62 | | - |
63 | | - // Wildcard patterns - Include: src/*.ts |
64 | | - ("wildcard_patterns", "src/index.ts", true, "wildcard matches files in src/"), |
65 | | - ("wildcard_patterns", "src/helper.ts", true, "wildcard matches files in src/"), |
66 | | - ("wildcard_patterns", "src/utils/helper.ts", false, "wildcard doesn't match subdirectories"), |
67 | | - ("wildcard_patterns", "index.ts", false, "wildcard doesn't match parent directory"), |
68 | | - |
69 | | - // Character set patterns - Include: [A-Z]*.ts |
70 | | - ("character_set_patterns", "App.ts", true, "character set matches uppercase start"), |
71 | | - ("character_set_patterns", "Button.ts", true, "character set matches uppercase start"), |
72 | | - |
73 | | - // Monorepo patterns - Include: packages/*/src/**/* |
74 | | - ("monorepo_patterns", "packages/pkg-a/src/index.ts", true, "monorepo pattern matches pkg-a"), |
75 | | - ("monorepo_patterns", "packages/pkg-b/src/utils.ts", true, "monorepo pattern matches pkg-b"), |
76 | | - ("monorepo_patterns", "packages/pkg-c/src/deep/nested/file.ts", true, "monorepo pattern matches nested"), |
77 | | - ("monorepo_patterns", "packages/pkg-a/dist/index.js", false, "dist not in src directory"), |
78 | | - ("monorepo_patterns", "shared/utils.ts", false, "shared not in packages/*/src"), |
79 | | - ("monorepo_patterns", "packages/pkg-a/test.ts", false, "test.ts not in src directory"), |
80 | | - |
81 | | - // outDir auto-exclude - Include: **/*.ts, CompilerOptions.outDir: dist |
82 | | - ("outdir_exclude", "src/index.ts", true, "source files included"), |
83 | | - ("outdir_exclude", "dist/index.js", false, "outDir automatically excluded"), |
84 | | - ("outdir_exclude", "dist/index.d.ts", false, "outDir automatically excluded"), |
85 | | - |
86 | | - // Complex patterns - Include: src/**/test/**/*.spec.ts |
87 | | - ("complex_patterns", "src/test/unit.spec.ts", true, "complex pattern matches src/test/*.spec.ts"), |
88 | | - ("complex_patterns", "src/utils/test/helper.spec.ts", true, "complex pattern matches src/**/test/*.spec.ts"), |
89 | | - ("complex_patterns", "src/deep/nested/test/component.spec.ts", true, "complex pattern matches deeply nested"), |
90 | | - ("complex_patterns", "src/index.ts", false, "file not in test directory"), |
91 | | - ("complex_patterns", "src/utils/helper.ts", false, "file not in test directory"), |
92 | | - ("complex_patterns", "src/test/unit.test.ts", false, "wrong extension (.test.ts not .spec.ts)"), |
93 | | - |
94 | | - // Absolute patterns - Include: src/**/*.ts, Exclude: excluded |
95 | | - ("absolute_patterns", "src/index.ts", true, "absolute pattern matches"), |
96 | | - ("absolute_patterns", "excluded/file.ts", false, "excluded directory"), |
| 29 | + // All files except dist/ can use path mappings |
| 30 | + ("with_baseurl", "index.ts", "~/log", true, "file in root can use path mapping"), |
| 31 | + ("with_baseurl", "index.ts", "log", true, "file in root can use baseUrl"), |
97 | 32 | ]; |
98 | 33 |
|
99 | | - for (fixture, file_path, should_match, comment) in test_cases { |
| 34 | + for (fixture, importer, specifier, should_resolve, comment) in test_cases { |
100 | 35 | let fixture_dir = f.join(fixture); |
101 | 36 | let resolver = Resolver::new(ResolveOptions { |
102 | 37 | tsconfig: Some(TsconfigDiscovery::Manual(TsconfigOptions { |
103 | 38 | config_file: fixture_dir.join("tsconfig.json"), |
104 | 39 | references: TsconfigReferences::Auto, |
105 | 40 | })), |
| 41 | + extensions: vec![".ts".into(), ".js".into()], |
106 | 42 | ..ResolveOptions::default() |
107 | 43 | }); |
108 | 44 |
|
109 | | - let tsconfig = resolver.resolve_tsconfig(&fixture_dir).unwrap(); |
110 | | - let result = tsconfig.matches_file(&fixture_dir.join(file_path)); |
111 | | - |
112 | | - assert_eq!(result, should_match, "{comment}: fixture={fixture} file={file_path}"); |
| 45 | + let importer_path = fixture_dir.join(importer); |
| 46 | + let result = resolver.resolve(&importer_path, specifier); |
| 47 | + |
| 48 | + if should_resolve { |
| 49 | + assert!( |
| 50 | + result.is_ok(), |
| 51 | + "{comment}: fixture={fixture} importer={importer} specifier={specifier} - expected success but got: {:?}", |
| 52 | + result.err() |
| 53 | + ); |
| 54 | + } else { |
| 55 | + assert!( |
| 56 | + result.is_err(), |
| 57 | + "{comment}: fixture={fixture} importer={importer} specifier={specifier} - expected failure but got: {:?}", |
| 58 | + result.ok() |
| 59 | + ); |
| 60 | + } |
113 | 61 | } |
114 | 62 | } |
115 | 63 |
|
|
0 commit comments