11//! <https://github.com/webpack/enhanced-resolve/blob/main/test/restrictions.test.js>
22
3+ use std:: sync:: Arc ;
4+
5+ use regex:: Regex ;
6+
37use crate :: { ResolveError , ResolveOptions , Resolver , Restriction } ;
48
5- // TODO: regex
6- // * should respect RegExp restriction
7- // * should try to find alternative #1
8- // * should try to find alternative #2
9- // * should try to find alternative #3
9+ #[ test]
10+ fn should_respect_regexp_restriction ( ) {
11+ let f = super :: fixture ( ) . join ( "restrictions" ) ;
12+
13+ let re = Regex :: new ( r"\.(sass|scss|css)$" ) . unwrap ( ) ;
14+ let resolver1 = Resolver :: new ( ResolveOptions {
15+ extensions : vec ! [ ".js" . into( ) ] ,
16+ restrictions : vec ! [ Restriction :: Fn ( Arc :: new( move |path| {
17+ path. as_os_str( ) . to_str( ) . is_some_and( |s| re. is_match( s) )
18+ } ) ) ] ,
19+ ..ResolveOptions :: default ( )
20+ } ) ;
21+
22+ let resolution = resolver1. resolve ( & f, "pck1" ) . map ( |r| r. full_path ( ) ) ;
23+ assert_eq ! ( resolution, Err ( ResolveError :: NotFound ( "pck1" . to_string( ) ) ) ) ;
24+ }
25+
26+ #[ test]
27+ fn should_try_to_find_alternative_1 ( ) {
28+ let f = super :: fixture ( ) . join ( "restrictions" ) ;
29+
30+ let re = Regex :: new ( r"\.(sass|scss|css)$" ) . unwrap ( ) ;
31+ let resolver1 = Resolver :: new ( ResolveOptions {
32+ extensions : vec ! [ ".js" . into( ) , ".css" . into( ) ] ,
33+ main_files : vec ! [ "index" . into( ) ] ,
34+ restrictions : vec ! [ Restriction :: Fn ( Arc :: new( move |path| {
35+ path. as_os_str( ) . to_str( ) . is_some_and( |s| re. is_match( s) )
36+ } ) ) ] ,
37+ ..ResolveOptions :: default ( )
38+ } ) ;
39+
40+ let resolution = resolver1. resolve ( & f, "pck1" ) . map ( |r| r. full_path ( ) ) ;
41+ assert_eq ! ( resolution, Ok ( f. join( "node_modules/pck1/index.css" ) ) ) ;
42+ }
1043
11- // should respect string restriction
1244#[ test]
13- fn restriction1 ( ) {
45+ fn should_respect_string_restriction ( ) {
1446 let fixture = super :: fixture ( ) ;
1547 let f = fixture. join ( "restrictions" ) ;
1648
@@ -21,5 +53,41 @@ fn restriction1() {
2153 } ) ;
2254
2355 let resolution = resolver. resolve ( & f, "pck2" ) ;
24- assert_eq ! ( resolution, Err ( ResolveError :: Restriction ( fixture. join( "c.js" ) , f) ) ) ;
56+ assert_eq ! ( resolution, Err ( ResolveError :: NotFound ( "pck2" . to_string( ) ) ) ) ;
57+ }
58+
59+ #[ test]
60+ fn should_try_to_find_alternative_2 ( ) {
61+ let f = super :: fixture ( ) . join ( "restrictions" ) ;
62+
63+ let re = Regex :: new ( r"\.(sass|scss|css)$" ) . unwrap ( ) ;
64+ let resolver1 = Resolver :: new ( ResolveOptions {
65+ extensions : vec ! [ ".js" . into( ) , ".css" . into( ) ] ,
66+ main_fields : vec ! [ "main" . into( ) , "style" . into( ) ] ,
67+ restrictions : vec ! [ Restriction :: Fn ( Arc :: new( move |path| {
68+ path. as_os_str( ) . to_str( ) . is_some_and( |s| re. is_match( s) )
69+ } ) ) ] ,
70+ ..ResolveOptions :: default ( )
71+ } ) ;
72+
73+ let resolution = resolver1. resolve ( & f, "pck2" ) . map ( |r| r. full_path ( ) ) ;
74+ assert_eq ! ( resolution, Ok ( f. join( "node_modules/pck2/index.css" ) ) ) ;
75+ }
76+
77+ #[ test]
78+ fn should_try_to_find_alternative_3 ( ) {
79+ let f = super :: fixture ( ) . join ( "restrictions" ) ;
80+
81+ let re = Regex :: new ( r"\.(sass|scss|css)$" ) . unwrap ( ) ;
82+ let resolver1 = Resolver :: new ( ResolveOptions {
83+ extensions : vec ! [ ".js" . into( ) ] ,
84+ main_fields : vec ! [ "main" . into( ) , "module" . into( ) , "style" . into( ) ] ,
85+ restrictions : vec ! [ Restriction :: Fn ( Arc :: new( move |path| {
86+ path. as_os_str( ) . to_str( ) . is_some_and( |s| re. is_match( s) )
87+ } ) ) ] ,
88+ ..ResolveOptions :: default ( )
89+ } ) ;
90+
91+ let resolution = resolver1. resolve ( & f, "pck2" ) . map ( |r| r. full_path ( ) ) ;
92+ assert_eq ! ( resolution, Ok ( f. join( "node_modules/pck2/index.css" ) ) ) ;
2593}
0 commit comments