Skip to content

Commit

Permalink
Split integration and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Dec 21, 2021
1 parent 0b612ac commit 116217c
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 238 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
.DS_Store
/src/test/integration/mock-environment
/src/test/private/accessToken.txt
/src/services/git-test-sandbox
/src/services/git-test-temp
204 changes: 204 additions & 0 deletions src/services/git.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { resolve } from 'path';
import del from 'del';
import makeDir from 'make-dir';
import { ValidConfigOptions } from '../options/options';
import * as childProcess from './child-process-promisified';
import * as env from './env';
import { cherrypick, getIsCommitInBranch } from './git';
import { getShortSha } from './github/commitFormatters';

jest.unmock('make-dir');
jest.unmock('del');

async function resetGitSandbox() {
const GIT_SANDBOX_DIR_PATH = resolve(`${__dirname}/git-test-temp`);

await del(GIT_SANDBOX_DIR_PATH);
await makeDir(GIT_SANDBOX_DIR_PATH);

// mock repo path to point to git-sandbox dir
jest.spyOn(env, 'getRepoPath').mockReturnValue(GIT_SANDBOX_DIR_PATH);

return {
sandboxDir: GIT_SANDBOX_DIR_PATH,
};
}

async function createAndCommitFile({
filename,
content,
execOpts,
}: {
filename: string;
content: string;
execOpts: Record<string, string>;
}) {
await childProcess.exec(`echo "${content}" > "${filename}"`, execOpts);
await childProcess.exec(
`git add -A && git commit -m 'Update ${filename}'`,
execOpts
);

return getCurrentSha(execOpts);
}

async function getCurrentSha(execOpts: Record<string, string>) {
const { stdout } = await childProcess.exec('git rev-parse HEAD', execOpts);
return stdout.trim();
}

async function getCurrentMessage(execOpts: Record<string, string>) {
const { stdout } = await childProcess.exec(
'git --no-pager log -1 --pretty=%B',
execOpts
);
return stdout.trim();
}

describe('git.integration', () => {
describe('getIsCommitInBranch', () => {
let firstSha: string;
let secondSha: string;

beforeEach(async () => {
const { sandboxDir } = await resetGitSandbox();

const execOpts = { cwd: sandboxDir };

// create and commit first file
await childProcess.exec('git init', execOpts);
firstSha = await createAndCommitFile({
filename: 'foo.md',
content: 'My first file',
execOpts,
});

// create 7.x branch (but stay on `main` branch)
await childProcess.exec('git branch 7.x', execOpts);

// create and commit second file
secondSha = await createAndCommitFile({
filename: 'bar.md',
content: 'My second file',
execOpts,
});

// checkout 7.x
await childProcess.exec('git checkout 7.x', execOpts);
});

it('should contain the first commit', async () => {
const isFirstCommitInBranch = await getIsCommitInBranch(
{} as ValidConfigOptions,
firstSha
);

expect(isFirstCommitInBranch).toEqual(true);
});

it('should not contain the second commit', async () => {
const isSecondCommitInBranch = await getIsCommitInBranch(
{} as ValidConfigOptions,
secondSha
);

expect(isSecondCommitInBranch).toEqual(false);
});

it('should not contain a random commit', async () => {
const isSecondCommitInBranch = await getIsCommitInBranch(
{} as ValidConfigOptions,
'abcdefg'
);

expect(isSecondCommitInBranch).toEqual(false);
});
});

describe('cherrypick', () => {
let firstSha: string;
let secondSha: string;
let fourthSha: string;
let execOpts: Record<string, string>;

beforeEach(async () => {
const { sandboxDir } = await resetGitSandbox();
execOpts = { cwd: sandboxDir };

// create and commit first file
await childProcess.exec('git init', execOpts);
firstSha = await createAndCommitFile({
filename: 'foo.md',
content: 'Creating first file',
execOpts,
});

// create 7.x branch (but stay on `main` branch)
await childProcess.exec('git branch 7.x', execOpts);

// create and commit second file
secondSha = await createAndCommitFile({
filename: 'bar.md',
content: 'Creating second file\nHello',
execOpts,
});

// edit first file
await createAndCommitFile({
filename: 'foo.md',
content: 'Changing first file',
execOpts,
});

// edit first file
fourthSha = await createAndCommitFile({
filename: 'foo.md',
content: 'Some more changes to the first file',
execOpts,
});

// checkout 7.x
await childProcess.exec('git checkout 7.x', execOpts);
});

it('should not cherrypick commit that already exists', async () => {
const shortSha = getShortSha(firstSha);
return expect(() =>
cherrypick({} as ValidConfigOptions, firstSha)
).rejects.toThrowError(
`Cherrypick failed because the selected commit (${shortSha}) is empty. Did you already backport this commit?`
);
});

it('should cherrypick commit cleanly', async () => {
const res = await cherrypick({} as ValidConfigOptions, secondSha);
expect(res).toEqual({
conflictingFiles: [],
needsResolving: false,
unstagedFiles: [],
});

const message = await getCurrentMessage(execOpts);

expect(message).toEqual(`Update bar.md`);
});

it('should cherrypick commit with conflicts', async () => {
const res = await cherrypick({} as ValidConfigOptions, fourthSha);
expect(res).toEqual({
needsResolving: true,
conflictingFiles: [
{
absolute:
'/Users/sqren/elastic/backport/src/services/git-test-temp/foo.md',
relative: 'foo.md',
},
],

unstagedFiles: [
'/Users/sqren/elastic/backport/src/services/git-test-temp/foo.md',
],
});
});
});
});
Loading

0 comments on commit 116217c

Please sign in to comment.