11//! <https://github.com/webpack/enhanced-resolve/blob/main/test/alias.test.js>
22
3- use crate :: { AliasValue , Resolution , ResolveError , ResolveOptions , Resolver } ;
3+ use normalize_path:: NormalizePath ;
4+ use std:: path:: Path ;
5+
6+ use crate :: { AliasValue , Resolution , ResolveContext , ResolveError , ResolveOptions , Resolver } ;
47
58#[ test]
69#[ cfg( not( target_os = "windows" ) ) ] // MemoryFS's path separator is always `/` so the test will not pass in windows.
@@ -129,29 +132,33 @@ fn absolute_path() {
129132 assert_eq ! ( resolution, Err ( ResolveError :: Ignored ( f. join( "foo" ) ) ) ) ;
130133}
131134
135+ fn check_slash ( path : & Path ) {
136+ let s = path. to_string_lossy ( ) . to_string ( ) ;
137+ #[ cfg( target_os = "windows" ) ]
138+ {
139+ assert ! ( !s. contains( '/' ) , "{s}" ) ;
140+ assert ! ( s. contains( '\\' ) , "{s}" ) ;
141+ }
142+ #[ cfg( not( target_os = "windows" ) ) ]
143+ {
144+ assert ! ( s. contains( '/' ) , "{s}" ) ;
145+ assert ! ( !s. contains( '\\' ) , "{s}" ) ;
146+ }
147+ }
148+
132149#[ test]
133150fn system_path ( ) {
134151 let f = super :: fixture ( ) ;
135152 let resolver = Resolver :: new ( ResolveOptions {
136153 alias : vec ! [ (
137154 "@app" . into( ) ,
138- vec![ AliasValue :: Path ( f. join( "alias" ) . to_str ( ) . unwrap ( ) . to_string( ) ) ] ,
155+ vec![ AliasValue :: Path ( f. join( "alias" ) . to_string_lossy ( ) . to_string( ) ) ] ,
139156 ) ] ,
140157 ..ResolveOptions :: default ( )
141158 } ) ;
142- let resolution = resolver. resolve ( & f, "@app/files/a" ) . map ( Resolution :: into_path_buf) ;
143- assert_eq ! ( resolution, Ok ( f. join( "alias/files/a.js" ) ) ) ;
144- let string = resolution. unwrap ( ) . to_string_lossy ( ) . to_string ( ) ;
145- #[ cfg( target_os = "windows" ) ]
146- {
147- assert ! ( !string. contains( '/' ) ) ;
148- assert ! ( string. contains( '\\' ) ) ;
149- }
150- #[ cfg( not( target_os = "windows" ) ) ]
151- {
152- assert ! ( string. contains( '/' ) ) ;
153- assert ! ( !string. contains( '\\' ) ) ;
154- }
159+ let path = resolver. resolve ( & f, "@app/files/a" ) . map ( Resolution :: into_path_buf) . unwrap ( ) ;
160+ assert_eq ! ( path, f. join( "alias/files/a.js" ) ) ;
161+ check_slash ( & path) ;
155162}
156163
157164// Not part of enhanced-resolve
@@ -168,3 +175,43 @@ fn infinite_recursion() {
168175 let resolution = resolver. resolve ( f, "./a" ) ;
169176 assert_eq ! ( resolution, Err ( ResolveError :: Recursion ) ) ;
170177}
178+
179+ #[ test]
180+ fn alias_is_full_path ( ) {
181+ let f = super :: fixture ( ) ;
182+ let dir = f. join ( "foo" ) ;
183+ let dir_str = dir. to_string_lossy ( ) . to_string ( ) ;
184+
185+ let resolver = Resolver :: new ( ResolveOptions {
186+ alias : vec ! [ ( "@" . into( ) , vec![ AliasValue :: Path ( dir_str. clone( ) ) ] ) ] ,
187+ ..ResolveOptions :: default ( )
188+ } ) ;
189+
190+ let mut ctx = ResolveContext :: default ( ) ;
191+
192+ let specifiers = [
193+ "@/index" . to_string ( ) ,
194+ // specifier has multiple `/` for reasons we'll never know
195+ "@////index" . to_string ( ) ,
196+ // specifier is a full path
197+ dir_str,
198+ ] ;
199+
200+ for specifier in specifiers {
201+ let resolution = resolver. resolve_with_context ( & f, & specifier, & mut ctx) ;
202+ assert_eq ! ( resolution. map( |r| r. full_path( ) ) , Ok ( dir. join( "index.js" ) ) ) ;
203+ }
204+
205+ for path in ctx. file_dependencies {
206+ assert_eq ! ( path, path. normalize( ) , "{path:?}" ) ;
207+ check_slash ( & path) ;
208+ }
209+
210+ for path in ctx. missing_dependencies {
211+ assert_eq ! ( path, path. normalize( ) , "{path:?}" ) ;
212+ check_slash ( & path) ;
213+ if let Some ( path) = path. parent ( ) {
214+ assert ! ( !path. is_file( ) , "{path:?} must not be a file" ) ;
215+ }
216+ }
217+ }
0 commit comments