@@ -11,7 +11,7 @@ import { IntegRunner } from './runner-base';
11
11
import * as logger from '../logger' ;
12
12
import { chunks , exec , promiseWithResolvers } from '../utils' ;
13
13
import type { DestructiveChange , AssertionResults , AssertionResult } from '../workers/common' ;
14
- import { DiagnosticReason , formatAssertionResults } from '../workers/common' ;
14
+ import { DiagnosticReason , formatAssertionResults , formatError } from '../workers/common' ;
15
15
16
16
export interface CommonOptions {
17
17
/**
@@ -114,15 +114,19 @@ export class IntegTestRunner extends IntegRunner {
114
114
* all branches and we then search for one that starts with `HEAD branch: `
115
115
*/
116
116
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 ] ;
118
124
119
125
// https://git-scm.com/docs/git-merge-base
120
126
let baseBranch : string | undefined = undefined ;
121
127
// try to find the base branch that the working branch was created from
122
128
try {
123
- const origin : string = exec ( [ 'git' , 'remote' , 'show' , 'origin' ] , {
124
- cwd,
125
- } ) ;
129
+ const origin : string = exec ( [ ...git , 'remote' , 'show' , 'origin' ] ) ;
126
130
const originLines = origin . split ( '\n' ) ;
127
131
for ( const line of originLines ) {
128
132
if ( line . trim ( ) . startsWith ( 'HEAD branch: ' ) ) {
@@ -141,22 +145,18 @@ export class IntegTestRunner extends IntegRunner {
141
145
// if we found the base branch then get the merge-base (most recent common commit)
142
146
// and checkout the snapshot using that commit
143
147
if ( baseBranch ) {
144
- const relativeSnapshotDir = path . relative ( this . directory , this . snapshotDir ) ;
148
+ const relativeSnapshotDir = path . relative ( gitCwd , this . snapshotDir ) ;
145
149
146
150
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 ] ) ;
153
153
} catch ( e ) {
154
154
logger . warning ( '%s\n%s' ,
155
155
`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 } ` ,
157
157
'' ,
158
158
) ;
159
- logger . warning ( 'error: %s' , e ) ;
159
+ logger . warning ( 'error: %s' , formatError ( e ) ) ;
160
160
}
161
161
}
162
162
}
0 commit comments