Skip to content

Commit

Permalink
chore: tweak git tests to handle default branch name (#10827)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 15, 2020
1 parent 7d47fa0 commit bc6dc7a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
44 changes: 30 additions & 14 deletions e2e/__tests__/jestChangedFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import {tmpdir} from 'os';
import * as path from 'path';
import {wrap} from 'jest-snapshot-serializer-raw';
import slash from 'slash';
import semver = require('semver');
import slash = require('slash');
import {findRepos, getChangedFilesForRoots} from 'jest-changed-files';
import {cleanup, run, testIfHg, writeFiles} from '../Utils';
import runJest from '../runJest';
Expand All @@ -18,6 +19,23 @@ const DIR = path.resolve(tmpdir(), 'jest-changed-files-test-dir');
const GIT = 'git -c user.name=jest_test -c user.email=jest_test@test.com';
const HG = 'hg --config ui.username=jest_test';

const gitVersionSupportsInitialBranch = (() => {
const {stdout} = run(`${GIT} --version`);
const gitVersion = stdout.split(' ').slice(-1)[0];

return semver.gte(gitVersion, '2.28.0');
})();

const mainBranchName = gitVersionSupportsInitialBranch ? 'main' : 'master';

function gitInit(dir: string) {
const initCommand = gitVersionSupportsInitialBranch
? `${GIT} init --initial-branch=${mainBranchName}`
: `${GIT} init`;

run(initCommand, dir);
}

beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));

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

run(`${GIT} init`, path.resolve(DIR, 'first-repo'));
run(`${GIT} init`, path.resolve(DIR, 'second-repo'));
gitInit(path.resolve(DIR, 'first-repo'));
gitInit(path.resolve(DIR, 'second-repo'));

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

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

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

run(`${GIT} init`, DIR);
gitInit(DIR);

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

({changedFiles: files} = await getChangedFilesForRoots(roots, {
changedSince: 'master',
changedSince: mainBranchName,
}));
// Returns files from this branch but not ones that only exist on master
// Returns files from this branch but not ones that only exist on mainBranchName
expect(
Array.from(files)
.map(filePath => path.basename(filePath))
Expand All @@ -252,7 +270,7 @@ test('monitors only root paths for git', async () => {
'nested-dir/second-nested-dir/file3.txt': 'file3',
});

run(`${GIT} init`, DIR);
gitInit(DIR);

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

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

// create an empty file, commit it to "master"
writeFiles(DIR, {
'file1.txt': '',
});
run(`${GIT} init`, DIR);
// create an empty file, commit it to "mainBranchName"
writeFiles(DIR, {'file1.txt': ''});
gitInit(DIR);
run(`${GIT} add file1.txt`, DIR);
run(`${GIT} commit --no-gpg-sign -m "initial"`, DIR);

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

run(`${GIT} init`, DIR);
gitInit(DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

Expand Down
38 changes: 28 additions & 10 deletions e2e/__tests__/onlyChanged.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,31 @@

import {tmpdir} from 'os';
import * as path from 'path';
import semver = require('semver');
import {cleanup, run, testIfHg, writeFiles} from '../Utils';
import runJest from '../runJest';

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

const gitVersionSupportsInitialBranch = (() => {
const {stdout} = run(`${GIT} --version`);
const gitVersion = stdout.split(' ').slice(-1)[0];

return semver.gte(gitVersion, '2.28.0');
})();

const mainBranchName = gitVersionSupportsInitialBranch ? 'main' : 'master';

function gitInit(dir: string) {
const initCommand = gitVersionSupportsInitialBranch
? `${GIT} init --initial-branch=${mainBranchName}`
: `${GIT} init`;

run(initCommand, dir);
}

beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));

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

run(`${GIT} init`, DIR);
gitInit(DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

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

stdout = runJest(DIR, ['--changedSince=master']).stdout;
stdout = runJest(DIR, [`--changedSince=${mainBranchName}`]).stdout;
expect(stdout).toMatch(
/No tests found related to files changed since "master"./,
`No tests found related to files changed since "${mainBranchName}".`,
);
});

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

run(`${GIT} init`, DIR);
gitInit(DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

Expand Down Expand Up @@ -114,7 +132,7 @@ test('report test coverage for only changed files', () => {
}),
});

run(`${GIT} init`, DIR);
gitInit(DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

Expand Down Expand Up @@ -153,7 +171,7 @@ test('report test coverage of source on test file change under only changed file
}),
});

run(`${GIT} init`, DIR);
gitInit(DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

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

run(`${GIT} init`, DIR);
gitInit(DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

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

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

run(`${GIT} init`, DIR);
gitInit(DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);

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

run(`${GIT} init`, modifiedDIR);
gitInit(modifiedDIR);
run(`${GIT} add .`, modifiedDIR);
run(`${GIT} commit --no-gpg-sign -m "first"`, modifiedDIR);

Expand Down

0 comments on commit bc6dc7a

Please sign in to comment.