@@ -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 ( )
@@ -1576,6 +1613,8 @@ async fn transform_import_map() {
15761613 . await
15771614 . unwrap ( ) ;
15781615
1616+ // the import map is used as the config file, so nothing was auto-discovered
1617+ assert_eq ! ( result. discovered_config_file, None ) ;
15791618 assert_files ! (
15801619 result. main. files,
15811620 & [
@@ -1826,6 +1865,8 @@ async fn transform_config_file() {
18261865 . await
18271866 . unwrap ( ) ;
18281867
1868+ // it was provided, so it wasn't auto-discovered
1869+ assert_eq ! ( result. discovered_config_file, None ) ;
18291870 assert_files ! (
18301871 result. main. files,
18311872 & [
@@ -1836,6 +1877,58 @@ async fn transform_config_file() {
18361877 ) ;
18371878}
18381879
1880+ #[ tokio:: test]
1881+ async fn transform_auto_discovered_config_file ( ) {
1882+ let result = TestBuilder :: new ( )
1883+ . with_loader ( |loader| {
1884+ add_config_discovery_files ( loader) ;
1885+ } )
1886+ . entry_point ( "file:///sub_dir/mod.ts" )
1887+ . transform ( )
1888+ . await
1889+ . unwrap ( ) ;
1890+
1891+ assert_eq ! (
1892+ result. discovered_config_file,
1893+ Some ( PathBuf :: from( if cfg!( windows) {
1894+ "C:\\ deno.json"
1895+ } else {
1896+ "/deno.json"
1897+ } ) )
1898+ ) ;
1899+ assert_files ! (
1900+ result. main. files,
1901+ & [
1902+ ( "sub_dir/mod.ts" , "import * as remote from '../other.js';" , ) ,
1903+ ( "other.ts" , "export function test() {}" , )
1904+ ]
1905+ ) ;
1906+ }
1907+
1908+ #[ tokio:: test]
1909+ async fn transform_no_config ( ) {
1910+ let err_message = TestBuilder :: new ( )
1911+ . with_loader ( |loader| {
1912+ add_config_discovery_files ( loader) ;
1913+ } )
1914+ . entry_point ( "file:///sub_dir/mod.ts" )
1915+ . set_no_config ( true )
1916+ . transform ( )
1917+ . await
1918+ . err ( )
1919+ . unwrap ( ) ;
1920+
1921+ // the config file should not have been discovered, so its
1922+ // import mapping should not have been applied
1923+ assert_eq ! (
1924+ err_message. to_string( ) ,
1925+ normalize_urls(
1926+ "Import \" localhost/mod.ts\" not a dependency
1927+ at file:///sub_dir/mod.ts:1:25"
1928+ )
1929+ ) ;
1930+ }
1931+
18391932#[ tokio:: test]
18401933async fn transform_multiple_entry_points ( ) {
18411934 let result = TestBuilder :: new ( )
@@ -2851,3 +2944,22 @@ fn get_shim_file_text(mut text: String) -> String {
28512944 ) ;
28522945 text
28532946}
2947+
2948+ /// Adds a config file in a parent directory of the `/sub_dir/mod.ts`
2949+ /// entry point so that it's only found by searching upwards from it.
2950+ fn add_config_discovery_files ( loader : & mut InMemoryLoader ) {
2951+ loader
2952+ . add_local_file (
2953+ "/sub_dir/mod.ts" ,
2954+ "import * as remote from 'localhost/mod.ts';" ,
2955+ )
2956+ . add_local_file (
2957+ "/deno.json" ,
2958+ r#"{
2959+ "imports": {
2960+ "localhost/mod.ts": "./other.ts"
2961+ }
2962+ }"# ,
2963+ )
2964+ . add_local_file ( "/other.ts" , "export function test() {}" ) ;
2965+ }
0 commit comments