@@ -11,7 +11,7 @@ import { IntegRunner } from './runner-base';
1111import * as logger from '../logger' ;
1212import { chunks , exec , promiseWithResolvers } from '../utils' ;
1313import type { DestructiveChange , AssertionResults , AssertionResult } from '../workers/common' ;
14- import { DiagnosticReason , formatAssertionResults } from '../workers/common' ;
14+ import { DiagnosticReason , formatAssertionResults , formatError } from '../workers/common' ;
1515
1616export interface CommonOptions {
1717 /**
@@ -114,15 +114,19 @@ export class IntegTestRunner extends IntegRunner {
114114 * all branches and we then search for one that starts with `HEAD branch: `
115115 */
116116 private checkoutSnapshot ( ) : void {
117- const cwd = this . directory ;
117+ // We use the directory that contains the snapshot to run git commands in
118+ // We don't change the cwd for executing git, but instead use the -C flag
119+ // @see https://git-scm.com/docs/git#Documentation/git.txt--Cltpathgt
120+ // This way we are guaranteed to operate under the correct git repo, even
121+ // when executing integ-runner from outside the repo under test.
122+ const gitCwd = path . dirname ( this . snapshotDir ) ;
123+ const git = [ 'git' , '-C' , gitCwd ] ;
118124
119125 // https://git-scm.com/docs/git-merge-base
120126 let baseBranch : string | undefined = undefined ;
121127 // try to find the base branch that the working branch was created from
122128 try {
123- const origin : string = exec ( [ 'git' , 'remote' , 'show' , 'origin' ] , {
124- cwd,
125- } ) ;
129+ const origin : string = exec ( [ ...git , 'remote' , 'show' , 'origin' ] ) ;
126130 const originLines = origin . split ( '\n' ) ;
127131 for ( const line of originLines ) {
128132 if ( line . trim ( ) . startsWith ( 'HEAD branch: ' ) ) {
@@ -141,22 +145,18 @@ export class IntegTestRunner extends IntegRunner {
141145 // if we found the base branch then get the merge-base (most recent common commit)
142146 // and checkout the snapshot using that commit
143147 if ( baseBranch ) {
144- const relativeSnapshotDir = path . relative ( this . directory , this . snapshotDir ) ;
148+ const relativeSnapshotDir = path . relative ( gitCwd , this . snapshotDir ) ;
145149
146150 try {
147- const base = exec ( [ 'git' , 'merge-base' , 'HEAD' , baseBranch ] , {
148- cwd,
149- } ) ;
150- exec ( [ 'git' , 'checkout' , base , '--' , relativeSnapshotDir ] , {
151- cwd,
152- } ) ;
151+ const base = exec ( [ ...git , 'merge-base' , 'HEAD' , baseBranch ] ) ;
152+ exec ( [ ...git , 'checkout' , base , '--' , relativeSnapshotDir ] ) ;
153153 } catch ( e ) {
154154 logger . warning ( '%s\n%s' ,
155155 `Could not checkout snapshot directory '${ this . snapshotDir } '. Please verify the following command completes correctly:` ,
156- `git checkout $(git merge-base HEAD ${ baseBranch } ) -- ${ relativeSnapshotDir } ` ,
156+ `git -C ${ gitCwd } checkout $(git -C ${ gitCwd } merge-base HEAD ${ baseBranch } ) -- ${ relativeSnapshotDir } ` ,
157157 '' ,
158158 ) ;
159- logger . warning ( 'error: %s' , e ) ;
159+ logger . warning ( 'error: %s' , formatError ( e ) ) ;
160160 }
161161 }
162162 }
0 commit comments