Skip to content

Commit bc6dc7a

Browse files
authored
chore: tweak git tests to handle default branch name (#10827)
1 parent 7d47fa0 commit bc6dc7a

2 files changed

Lines changed: 58 additions & 24 deletions

File tree

e2e/__tests__/jestChangedFiles.test.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import {tmpdir} from 'os';
99
import * as path from 'path';
1010
import {wrap} from 'jest-snapshot-serializer-raw';
11-
import slash from 'slash';
11+
import semver = require('semver');
12+
import slash = require('slash');
1213
import {findRepos, getChangedFilesForRoots} from 'jest-changed-files';
1314
import {cleanup, run, testIfHg, writeFiles} from '../Utils';
1415
import runJest from '../runJest';
@@ -18,6 +19,23 @@ const DIR = path.resolve(tmpdir(), 'jest-changed-files-test-dir');
1819
const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com';
1920
const HG = 'hg --config ui.username=jest_test';
2021

22+
const gitVersionSupportsInitialBranch = (() => {
23+
const {stdout} = run(`${GIT} --version`);
24+
const gitVersion = stdout.split(' ').slice(-1)[0];
25+
26+
return semver.gte(gitVersion, '2.28.0');
27+
})();
28+
29+
const mainBranchName = gitVersionSupportsInitialBranch ? 'main' : 'master';
30+
31+
function gitInit(dir: string) {
32+
const initCommand = gitVersionSupportsInitialBranch
33+
? `${GIT} init --initial-branch=${mainBranchName}`
34+
: `${GIT} init`;
35+
36+
run(initCommand, dir);
37+
}
38+
2139
beforeEach(() => cleanup(DIR));
2240
afterEach(() => cleanup(DIR));
2341

@@ -70,8 +88,8 @@ test('gets git SCM roots and dedupes them', async () => {
7088
'second-repo/nested-dir/second-nested-dir/file3.txt': 'file3',
7189
});
7290

73-
run(`${GIT} init`, path.resolve(DIR, 'first-repo'));
74-
run(`${GIT} init`, path.resolve(DIR, 'second-repo'));
91+
gitInit(path.resolve(DIR, 'first-repo'));
92+
gitInit(path.resolve(DIR, 'second-repo'));
7593

7694
const roots = [
7795
'',
@@ -108,7 +126,7 @@ testIfHg('gets mixed git and hg SCM roots and dedupes them', async () => {
108126
'second-repo/nested-dir/second-nested-dir/file3.txt': 'file3',
109127
});
110128

111-
run(`${GIT} init`, path.resolve(DIR, 'first-repo'));
129+
gitInit(path.resolve(DIR, 'first-repo'));
112130
run(`${HG} init`, path.resolve(DIR, 'second-repo'));
113131

114132
const roots = [
@@ -142,7 +160,7 @@ test('gets changed files for git', async () => {
142160
'nested-dir/second-nested-dir/file3.txt': 'file3',
143161
});
144162

145-
run(`${GIT} init`, DIR);
163+
gitInit(DIR);
146164

147165
const roots = [
148166
'',
@@ -235,9 +253,9 @@ test('gets changed files for git', async () => {
235253
run(`${GIT} commit --no-gpg-sign -m "test5"`, DIR);
236254

237255
({changedFiles: files} = await getChangedFilesForRoots(roots, {
238-
changedSince: 'master',
256+
changedSince: mainBranchName,
239257
}));
240-
// Returns files from this branch but not ones that only exist on master
258+
// Returns files from this branch but not ones that only exist on mainBranchName
241259
expect(
242260
Array.from(files)
243261
.map(filePath => path.basename(filePath))
@@ -252,7 +270,7 @@ test('monitors only root paths for git', async () => {
252270
'nested-dir/second-nested-dir/file3.txt': 'file3',
253271
});
254272

255-
run(`${GIT} init`, DIR);
273+
gitInit(DIR);
256274

257275
const roots = [path.resolve(DIR, 'nested-dir')];
258276

@@ -267,11 +285,9 @@ test('monitors only root paths for git', async () => {
267285
it('does not find changes in files with no diff, for git', async () => {
268286
const roots = [path.resolve(DIR)];
269287

270-
// create an empty file, commit it to "master"
271-
writeFiles(DIR, {
272-
'file1.txt': '',
273-
});
274-
run(`${GIT} init`, DIR);
288+
// create an empty file, commit it to "mainBranchName"
289+
writeFiles(DIR, {'file1.txt': ''});
290+
gitInit(DIR);
275291
run(`${GIT} add file1.txt`, DIR);
276292
run(`${GIT} commit --no-gpg-sign -m "initial"`, DIR);
277293

@@ -315,7 +331,7 @@ test('handles a bad revision for "changedSince", for git', async () => {
315331
'package.json': '{}',
316332
});
317333

318-
run(`${GIT} init`, DIR);
334+
gitInit(DIR);
319335
run(`${GIT} add .`, DIR);
320336
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
321337

e2e/__tests__/onlyChanged.test.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@
77

88
import {tmpdir} from 'os';
99
import * as path from 'path';
10+
import semver = require('semver');
1011
import {cleanup, run, testIfHg, writeFiles} from '../Utils';
1112
import runJest from '../runJest';
1213

1314
const DIR = path.resolve(tmpdir(), 'jest_only_changed');
1415
const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com';
1516
const HG = 'hg --config ui.username=jest_test';
1617

18+
const gitVersionSupportsInitialBranch = (() => {
19+
const {stdout} = run(`${GIT} --version`);
20+
const gitVersion = stdout.split(' ').slice(-1)[0];
21+
22+
return semver.gte(gitVersion, '2.28.0');
23+
})();
24+
25+
const mainBranchName = gitVersionSupportsInitialBranch ? 'main' : 'master';
26+
27+
function gitInit(dir: string) {
28+
const initCommand = gitVersionSupportsInitialBranch
29+
? `${GIT} init --initial-branch=${mainBranchName}`
30+
: `${GIT} init`;
31+
32+
run(initCommand, dir);
33+
}
34+
1735
beforeEach(() => cleanup(DIR));
1836
afterEach(() => cleanup(DIR));
1937

@@ -25,7 +43,7 @@ test('run for "onlyChanged" and "changedSince"', () => {
2543
'package.json': '{}',
2644
});
2745

28-
run(`${GIT} init`, DIR);
46+
gitInit(DIR);
2947
run(`${GIT} add .`, DIR);
3048
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
3149

@@ -34,9 +52,9 @@ test('run for "onlyChanged" and "changedSince"', () => {
3452
/No tests found related to files changed since last commit./,
3553
);
3654

37-
stdout = runJest(DIR, ['--changedSince=master']).stdout;
55+
stdout = runJest(DIR, [`--changedSince=${mainBranchName}`]).stdout;
3856
expect(stdout).toMatch(
39-
/No tests found related to files changed since "master"./,
57+
`No tests found related to files changed since "${mainBranchName}".`,
4058
);
4159
});
4260

@@ -53,7 +71,7 @@ test('run only changed files', () => {
5371
({stdout} = runJest(DIR, ['-o']));
5472
expect(stdout).toMatch(/Jest can only find uncommitted changed files/);
5573

56-
run(`${GIT} init`, DIR);
74+
gitInit(DIR);
5775
run(`${GIT} add .`, DIR);
5876
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
5977

@@ -114,7 +132,7 @@ test('report test coverage for only changed files', () => {
114132
}),
115133
});
116134

117-
run(`${GIT} init`, DIR);
135+
gitInit(DIR);
118136
run(`${GIT} add .`, DIR);
119137
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
120138

@@ -153,7 +171,7 @@ test('report test coverage of source on test file change under only changed file
153171
}),
154172
});
155173

156-
run(`${GIT} init`, DIR);
174+
gitInit(DIR);
157175
run(`${GIT} add .`, DIR);
158176
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
159177

@@ -177,7 +195,7 @@ test('do not pickup non-tested files when reporting coverage on only changed fil
177195
'package.json': JSON.stringify({name: 'original name'}),
178196
});
179197

180-
run(`${GIT} init`, DIR);
198+
gitInit(DIR);
181199
run(`${GIT} add .`, DIR);
182200
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
183201

@@ -204,7 +222,7 @@ test('collect test coverage when using onlyChanged', () => {
204222
}),
205223
});
206224

207-
run(`${GIT} init`, DIR);
225+
gitInit(DIR);
208226
run(`${GIT} add .`, DIR);
209227
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
210228
run(`${GIT} checkout -b new-branch`, DIR);
@@ -233,7 +251,7 @@ test('onlyChanged in config is overwritten by --all or testPathPattern', () => {
233251
({stdout} = runJest(DIR));
234252
expect(stdout).toMatch(/Jest can only find uncommitted changed files/);
235253

236-
run(`${GIT} init`, DIR);
254+
gitInit(DIR);
237255
run(`${GIT} add .`, DIR);
238256
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
239257

@@ -351,7 +369,7 @@ test('path on Windows is case-insensitive', () => {
351369
'package.json': '{}',
352370
});
353371

354-
run(`${GIT} init`, modifiedDIR);
372+
gitInit(modifiedDIR);
355373
run(`${GIT} add .`, modifiedDIR);
356374
run(`${GIT} commit --no-gpg-sign -m "first"`, modifiedDIR);
357375

0 commit comments

Comments
 (0)