@@ -15,6 +15,7 @@ use pretty_assertions::assert_eq;
1515#[ macro_use]
1616mod integration;
1717
18+ use integration:: InMemoryLoader ;
1819use integration:: TestBuilder ;
1920
2021use crate :: integration:: assert_identity_transforms;
@@ -1244,7 +1245,7 @@ async fn transform_jsr_specifier_mappings() {
12441245}
12451246
12461247#[ tokio:: test]
1247- async fn transform_jsr_specifier_mapping_via_import_map ( ) {
1248+ async fn transform_jsr_specifier_mapping_via_config_file ( ) {
12481249 let result = TestBuilder :: new ( )
12491250 . with_loader ( |loader| {
12501251 loader
@@ -1259,6 +1260,42 @@ async fn transform_jsr_specifier_mapping_via_import_map() {
12591260 ) ;
12601261 } )
12611262 . set_config_file ( "file:///deno.json" )
1263+ // the version requirement comes from the config file
1264+ . add_package_specifier_mapping ( "jsr:@scope/name" , "scope-name" , None , None )
1265+ . transform ( )
1266+ . await
1267+ . unwrap ( ) ;
1268+
1269+ assert_files ! (
1270+ result. main. files,
1271+ & [ ( "mod.ts" , "import * as pkg from 'scope-name';" ) ]
1272+ ) ;
1273+ assert_eq ! (
1274+ result. main. dependencies,
1275+ & [ Dependency {
1276+ name: "scope-name" . to_string( ) ,
1277+ version: "^1.0.0" . to_string( ) ,
1278+ peer_dependency: false ,
1279+ } ]
1280+ ) ;
1281+ }
1282+
1283+ #[ tokio:: test]
1284+ async fn transform_jsr_specifier_mapping_via_import_map ( ) {
1285+ let result = TestBuilder :: new ( )
1286+ . with_loader ( |loader| {
1287+ loader
1288+ . add_local_file ( "/mod.ts" , "import * as pkg from '@scope/name';" )
1289+ . add_local_file (
1290+ "/import_map.json" ,
1291+ r#"{
1292+ "imports": {
1293+ "@scope/name": "jsr:@scope/name@^1.0.0"
1294+ }
1295+ }"# ,
1296+ ) ;
1297+ } )
1298+ . set_import_map ( "file:///import_map.json" )
12621299 // the version requirement comes from the import map
12631300 . add_package_specifier_mapping ( "jsr:@scope/name" , "scope-name" , None , None )
12641301 . transform ( )
@@ -1746,6 +1783,8 @@ async fn transform_import_map() {
17461783 . await
17471784 . unwrap ( ) ;
17481785
1786+ // the import map is used as the config file, so nothing was auto-discovered
1787+ assert_eq ! ( result. discovered_config_file, None ) ;
17491788 assert_files ! (
17501789 result. main. files,
17511790 & [
@@ -1996,6 +2035,8 @@ async fn transform_config_file() {
19962035 . await
19972036 . unwrap ( ) ;
19982037
2038+ // it was provided, so it wasn't auto-discovered
2039+ assert_eq ! ( result. discovered_config_file, None ) ;
19992040 assert_files ! (
20002041 result. main. files,
20012042 & [
@@ -2006,6 +2047,58 @@ async fn transform_config_file() {
20062047 ) ;
20072048}
20082049
2050+ #[ tokio:: test]
2051+ async fn transform_auto_discovered_config_file ( ) {
2052+ let result = TestBuilder :: new ( )
2053+ . with_loader ( |loader| {
2054+ add_config_discovery_files ( loader) ;
2055+ } )
2056+ . entry_point ( "file:///sub_dir/mod.ts" )
2057+ . transform ( )
2058+ . await
2059+ . unwrap ( ) ;
2060+
2061+ assert_eq ! (
2062+ result. discovered_config_file,
2063+ Some ( PathBuf :: from( if cfg!( windows) {
2064+ "C:\\ deno.json"
2065+ } else {
2066+ "/deno.json"
2067+ } ) )
2068+ ) ;
2069+ assert_files ! (
2070+ result. main. files,
2071+ & [
2072+ ( "sub_dir/mod.ts" , "import * as remote from '../other.js';" , ) ,
2073+ ( "other.ts" , "export function test() {}" , )
2074+ ]
2075+ ) ;
2076+ }
2077+
2078+ #[ tokio:: test]
2079+ async fn transform_no_config ( ) {
2080+ let err_message = TestBuilder :: new ( )
2081+ . with_loader ( |loader| {
2082+ add_config_discovery_files ( loader) ;
2083+ } )
2084+ . entry_point ( "file:///sub_dir/mod.ts" )
2085+ . set_no_config ( true )
2086+ . transform ( )
2087+ . await
2088+ . err ( )
2089+ . unwrap ( ) ;
2090+
2091+ // the config file should not have been discovered, so its
2092+ // import mapping should not have been applied
2093+ assert_eq ! (
2094+ err_message. to_string( ) ,
2095+ normalize_urls(
2096+ "Import \" localhost/mod.ts\" not a dependency
2097+ at file:///sub_dir/mod.ts:1:25"
2098+ )
2099+ ) ;
2100+ }
2101+
20092102#[ tokio:: test]
20102103async fn transform_multiple_entry_points ( ) {
20112104 let result = TestBuilder :: new ( )
@@ -3021,3 +3114,22 @@ fn get_shim_file_text(mut text: String) -> String {
30213114 ) ;
30223115 text
30233116}
3117+
3118+ /// Adds a config file in a parent directory of the `/sub_dir/mod.ts`
3119+ /// entry point so that it's only found by searching upwards from it.
3120+ fn add_config_discovery_files ( loader : & mut InMemoryLoader ) {
3121+ loader
3122+ . add_local_file (
3123+ "/sub_dir/mod.ts" ,
3124+ "import * as remote from 'localhost/mod.ts';" ,
3125+ )
3126+ . add_local_file (
3127+ "/deno.json" ,
3128+ r#"{
3129+ "imports": {
3130+ "localhost/mod.ts": "./other.ts"
3131+ }
3132+ }"# ,
3133+ )
3134+ . add_local_file ( "/other.ts" , "export function test() {}" ) ;
3135+ }
0 commit comments