Skip to content

Commit 10a4550

Browse files
committed
fix(integ-runner): improve git directory handling for snapshot operations
Fixes issues with git operations by using the -C flag to specify the correct git repository directory. This ensures integ-runner works correctly when executed from outside the repository under test. Also standardizes the directory used for running CDK apps to always be the current working directory.
1 parent 51a1406 commit 10a4550

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

packages/@aws-cdk/integ-runner/lib/runner/integ-test-runner.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { IntegRunner } from './runner-base';
1111
import * as logger from '../logger';
1212
import { chunks, exec, promiseWithResolvers } from '../utils';
1313
import type { DestructiveChange, AssertionResults, AssertionResult } from '../workers/common';
14-
import { DiagnosticReason, formatAssertionResults } from '../workers/common';
14+
import { DiagnosticReason, formatAssertionResults, formatError } from '../workers/common';
1515

1616
export 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
}

packages/@aws-cdk/integ-runner/lib/runner/integration-tests.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,17 @@ export class IntegTest {
106106
this.appCommand = info.appCommand ?? 'node {filePath}';
107107
this.absoluteFileName = path.resolve(info.fileName);
108108
this.fileName = path.relative(process.cwd(), info.fileName);
109-
110-
const parsed = path.parse(this.fileName);
111109
this.discoveryRelativeFileName = path.relative(info.discoveryRoot, info.fileName);
112-
// if `--watch` then we need the directory to be the cwd
113-
this.directory = info.watch ? process.cwd() : parsed.dir;
114110

115-
// if we are running in a package directory then just use the fileName
116-
// as the testname, but if we are running in a parent directory with
117-
// multiple packages then use the directory/filename as the testname
118-
//
119-
// Looks either like `integ.mytest` or `package/test/integ.mytest`.
120-
const relDiscoveryRoot = path.relative(process.cwd(), info.discoveryRoot);
121-
this.testName = this.directory === path.join(relDiscoveryRoot, 'test') || this.directory === path.join(relDiscoveryRoot)
122-
? parsed.name
123-
: path.join(path.relative(this.info.discoveryRoot, parsed.dir), parsed.name);
111+
// for consistency, always run the CDK apps under test from the CWD
112+
// this is especially important for languages that use the CWD to discover assets
113+
// @see https://github.com/aws/aws-cdk-cli/issues/638
114+
this.directory = process.cwd();
124115

116+
// We treat the discovery root as the base for test names
117+
// Looks either like `integ.mytest` or `package/test/integ.mytest`.
118+
const parsed = path.parse(this.fileName);
119+
this.testName = path.join(path.relative(this.info.discoveryRoot, parsed.dir), parsed.name);
125120
this.normalizedTestName = parsed.name;
126121
this.snapshotDir = path.join(parsed.dir, `${parsed.base}.snapshot`);
127122
this.temporaryOutputDir = path.join(parsed.dir, `${CDK_OUTDIR_PREFIX}.${parsed.base}.snapshot`);

0 commit comments

Comments
 (0)