@@ -41,6 +41,7 @@ import {
4141 initAllState ,
4242 callee ,
4343 SAMPLE_DOTCOM_API_DETAILS ,
44+ AssertableTarget ,
4445} from "./testing-utils" ;
4546import {
4647 GitHubVariant ,
@@ -2605,18 +2606,104 @@ test.serial(
26052606 await withTmpDir ( async ( tmpDir ) => {
26062607 const getRemoteConfig = sinon . stub ( file , "getRemoteConfig" ) . resolves ( { } ) ;
26072608
2608- const remoteAddress = "repo:file" ;
2609- await callee ( configUtils . loadUserConfig )
2610- . withArgs ( remoteAddress , tmpDir , SAMPLE_DOTCOM_API_DETAILS , tmpDir )
2611- . passes ( t . deepEqual , { } ) ;
2609+ // Construct the basic test target.
2610+ const target = callee ( configUtils . loadUserConfig )
2611+ . withDefaultActionsEnv ( )
2612+ . withFeatures ( [ Feature . NewRemoteFileAddresses ] ) ;
2613+
2614+ // Utility function to assert that `targetWithArgs` has identified
2615+ // the input as a remote file address.
2616+ const checkIsRemote =
2617+ ( address : string ) =>
2618+ async < R > ( targetWithArgs : AssertableTarget < R > ) => {
2619+ // We have stubbed `getRemoteConfig` to resolve to `{}`, so we
2620+ // expect that result.
2621+ await targetWithArgs . passes ( t . deepEqual , { } ) ;
2622+
2623+ // And `getRemoteConfig` should have been called exactly once.
2624+ t . is ( getRemoteConfig . callCount , 1 ) ;
2625+
2626+ // Get the arguments for the call and check that there were three.
2627+ // We don't care about the first, but check that the other two
2628+ // match our expectations. We break it down like this to get
2629+ // more useful test output.
2630+ const args = getRemoteConfig . getCalls ( ) [ 0 ] . args ;
2631+ t . is ( args . length , 3 ) ;
2632+ t . deepEqual ( args [ 1 ] , address ) ;
2633+ t . deepEqual ( args [ 2 ] , SAMPLE_DOTCOM_API_DETAILS ) ;
2634+ } ;
26122635
2613- t . true (
2614- getRemoteConfig . calledOnceWithExactly (
2615- sinon . match . any ,
2616- remoteAddress ,
2636+ // Utility function to assert that `targetWithArgs` has not identified
2637+ // the input as a remote file address.
2638+ const checkIsNotRemote = async < R > (
2639+ targetWithArgs : AssertableTarget < R > ,
2640+ ) => {
2641+ // We expect `loadUserConfig` to have thrown if it thinks the path is local,
2642+ // since the inputs we provide aren't for files that exist.
2643+ await targetWithArgs . throws ( t ) ;
2644+
2645+ // Additionally, we expect that `getRemoteConfig` wasn't called.
2646+ t . is ( getRemoteConfig . callCount , 0 ) ;
2647+ } ;
2648+
2649+ // Utility function to add the explicit `REMOTE_PATH_PREFIX` to the input.
2650+ const withExplicitPrefix = ( str : string ) =>
2651+ `${ file . REMOTE_PATH_PREFIX } ${ str } ` ;
2652+
2653+ // Utility to set up a call to `loadUserConfig` with the provided `address`
2654+ // and pass it to `assertion`.
2655+ const testTargetWith = async (
2656+ address : string ,
2657+ assertion : (
2658+ targetWithArgs : AssertableTarget < Promise < UserConfig > > ,
2659+ ) => Promise < any > ,
2660+ ) => {
2661+ // Reset the stub's history since we re-use it.
2662+ getRemoteConfig . resetHistory ( ) ;
2663+
2664+ // Log the input we are testing so that, in the event of a failure,
2665+ // it is easier to see which input was responsible.
2666+ t . log ( `testTargetWith("${ address } ")` ) ;
2667+
2668+ // Prepare the test call to `loadUserConfig`.
2669+ const targetWithArgs = target . withArgs (
2670+ address ,
2671+ tmpDir ,
26172672 SAMPLE_DOTCOM_API_DETAILS ,
2618- ) ,
2673+ tmpDir ,
2674+ ) ;
2675+
2676+ // Pass it to the provided assertion function.
2677+ await assertion ( targetWithArgs ) ;
2678+ } ;
2679+
2680+ // Since this input contains an '@' character, it is treated as a remote path
2681+ // by the old logic even without the explicit prefix.
2682+ const remoteWithoutPrefix = "repo@main" ;
2683+ await testTargetWith (
2684+ remoteWithoutPrefix ,
2685+ checkIsRemote ( remoteWithoutPrefix ) ,
2686+ ) ;
2687+ await testTargetWith (
2688+ withExplicitPrefix ( remoteWithoutPrefix ) ,
2689+ checkIsRemote ( remoteWithoutPrefix ) ,
26192690 ) ;
2691+ // It is only treated as a local path with the corresponding prefix.
2692+ await testTargetWith ( `./${ remoteWithoutPrefix } ` , checkIsNotRemote ) ;
2693+
2694+ // The following test inputs are examples of ambiguous paths. They could refer to
2695+ // valid local or remote paths. For each, we check that they are treated as remote
2696+ // paths if the explicit remote file prefix is used and as local paths otherwise.
2697+ const testInputs = [ "repo:file" , "input" , "../input" ] ;
2698+
2699+ for ( const testInput of testInputs ) {
2700+ for ( const addPrefix of [ true , false ] ) {
2701+ await testTargetWith (
2702+ addPrefix ? withExplicitPrefix ( testInput ) : testInput ,
2703+ addPrefix ? checkIsRemote ( testInput ) : checkIsNotRemote ,
2704+ ) ;
2705+ }
2706+ }
26202707 } ) ;
26212708 } ,
26222709) ;
0 commit comments