Skip to content

Commit

Permalink
Improve sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Feb 6, 2022
1 parent 4b0c831 commit be68ad9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 64 deletions.
68 changes: 35 additions & 33 deletions src/services/git.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { resolve } from 'path';
import del from 'del';
import makeDir from 'make-dir';
import { ValidConfigOptions } from '../options/options';
import { mockGqlRequest } from '../test/nockHelpers';
import { getSandboxPath, resetSandbox } from '../test/sandbox';
import { SpyHelper } from '../types/SpyHelper';
import * as childProcess from './child-process-promisified';
import {
Expand All @@ -14,18 +13,8 @@ import {
import { getShortSha } from './github/commitFormatters';
import { RepoOwnerAndNameResponse } from './github/v4/getRepoOwnerAndName';

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

const GIT_SANDBOX_DIR_PATH = resolve(
`${__dirname}/_tmp_sandbox_/git.integration.test`
);

async function resetGitSandbox(specName: string) {
await del(GIT_SANDBOX_DIR_PATH);
await makeDir(`${GIT_SANDBOX_DIR_PATH}/${specName}`);
}

async function createAndCommitFile({
filename,
content,
Expand Down Expand Up @@ -61,10 +50,14 @@ describe('git.integration', () => {
describe('getIsCommitInBranch', () => {
let firstSha: string;
let secondSha: string;
const sandboxPath = getSandboxPath({
filename: __filename,
specname: 'getIsCommitInBranch',
});

beforeEach(async () => {
await resetGitSandbox('getIsCommitInBranch');
const execOpts = { cwd: GIT_SANDBOX_DIR_PATH };
await resetSandbox(sandboxPath);
const execOpts = { cwd: sandboxPath };

// create and commit first file
await childProcess.exec('git init', execOpts);
Expand All @@ -90,7 +83,7 @@ describe('git.integration', () => {

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

Expand All @@ -99,7 +92,7 @@ describe('git.integration', () => {

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

Expand All @@ -108,7 +101,7 @@ describe('git.integration', () => {

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

Expand All @@ -121,10 +114,14 @@ describe('git.integration', () => {
let secondSha: string;
let fourthSha: string;
let execOpts: { cwd: string };
const sandboxPath = getSandboxPath({
filename: __filename,
specname: 'cherrypick',
});

beforeEach(async () => {
await resetGitSandbox('cherrypick');
execOpts = { cwd: GIT_SANDBOX_DIR_PATH };
await resetSandbox(sandboxPath);
execOpts = { cwd: sandboxPath };

// create and commit first file
await childProcess.exec('git init', execOpts);
Expand Down Expand Up @@ -165,10 +162,7 @@ describe('git.integration', () => {
it('should not cherrypick commit that already exists', async () => {
const shortSha = getShortSha(firstSha);
return expect(() =>
cherrypick(
{ dir: GIT_SANDBOX_DIR_PATH } as ValidConfigOptions,
firstSha
)
cherrypick({ dir: sandboxPath } as ValidConfigOptions, firstSha)
).rejects.toThrowError(
`Cherrypick failed because the selected commit (${shortSha}) is empty. Did you already backport this commit?`
);
Expand All @@ -178,7 +172,7 @@ describe('git.integration', () => {
const res = await cherrypick(
{
cherrypickRef: false,
dir: GIT_SANDBOX_DIR_PATH,
dir: sandboxPath,
} as ValidConfigOptions,
secondSha
);
Expand All @@ -197,7 +191,7 @@ describe('git.integration', () => {
const res = await cherrypick(
{
cherrypickRef: true,
dir: GIT_SANDBOX_DIR_PATH,
dir: sandboxPath,
} as ValidConfigOptions,
secondSha
);
Expand All @@ -216,29 +210,33 @@ describe('git.integration', () => {

it('should cherrypick commit with conflicts', async () => {
const res = await cherrypick(
{ dir: GIT_SANDBOX_DIR_PATH } as ValidConfigOptions,
{ dir: sandboxPath } as ValidConfigOptions,
fourthSha
);
expect(res).toEqual({
needsResolving: true,
conflictingFiles: [
{
absolute: `${GIT_SANDBOX_DIR_PATH}/foo.md`,
absolute: `${sandboxPath}/foo.md`,
relative: 'foo.md',
},
],
unstagedFiles: [`${GIT_SANDBOX_DIR_PATH}/foo.md`],
unstagedFiles: [`${sandboxPath}/foo.md`],
});
});
});

describe('cloneRepo', () => {
const sourceRepo = `${GIT_SANDBOX_DIR_PATH}/clone/source-repo`;
const backportRepo = `${GIT_SANDBOX_DIR_PATH}/clone/backport-repo`;
const sandboxPath = getSandboxPath({
filename: __filename,
specname: 'cloneRepo',
});
const sourceRepo = `${sandboxPath}/source-repo`;
const backportRepo = `${sandboxPath}/backport-repo`;
let execSpy: SpyHelper<typeof childProcess.execAsCallback>;

beforeEach(async () => {
await del(GIT_SANDBOX_DIR_PATH);
await resetSandbox(sandboxPath);
await makeDir(sourceRepo);

const execOpts = { cwd: sourceRepo };
Expand Down Expand Up @@ -269,10 +267,14 @@ describe('git.integration', () => {
});

describe('getSourceRepoPath', () => {
const sourceRepo = `${GIT_SANDBOX_DIR_PATH}/clone/source-repo`;
const sandboxPath = getSandboxPath({
filename: __filename,
specname: 'getSourceRepoPath',
});
const sourceRepo = `${sandboxPath}/source-repo`;

beforeEach(async () => {
await del(GIT_SANDBOX_DIR_PATH);
await resetSandbox(sandboxPath);
await makeDir(sourceRepo);

const execOpts = { cwd: sourceRepo };
Expand Down
26 changes: 5 additions & 21 deletions src/services/github/v4/getRepoOwnerAndName.private.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
import { resolve } from 'path';
import del from 'del';
import makeDir from 'make-dir';
import * as childProcess from '../../../services/child-process-promisified';
import { getDevAccessToken } from '../../../test/private/getDevAccessToken';
import { getSandboxPath, resetSandbox } from '../../../test/sandbox';
import { getRepoOwnerAndName } from './getRepoOwnerAndName';

jest.unmock('make-dir');
jest.unmock('del');
const sandboxPath = getSandboxPath({ filename: __filename });

describe('fetchRemoteProjectConfig', () => {
let devAccessToken: string;

async function resetSandbox() {
const GIT_SANDBOX_DIR_PATH = resolve(
`${__dirname}/_tmp_sandbox_/getRepoOwnerAndName.private.test`
);

console.log(__dirname, GIT_SANDBOX_DIR_PATH);

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

return GIT_SANDBOX_DIR_PATH;
}

beforeEach(async () => {
devAccessToken = await getDevAccessToken();
});

describe('when the remote is a fork', () => {
it('retrives the original owner from github', async () => {
const sandboxPath = await resetSandbox();
await resetSandbox(sandboxPath);
const execOpts = { cwd: sandboxPath };
await childProcess.exec(`git init`, execOpts);
await childProcess.exec(
Expand All @@ -52,7 +36,7 @@ describe('fetchRemoteProjectConfig', () => {

describe('when none of the git remotes are found', () => {
it('swallows the error and returns empty', async () => {
const sandboxPath = await resetSandbox();
await resetSandbox(sandboxPath);
const execOpts = { cwd: sandboxPath };
await childProcess.exec(`git init`, execOpts);
await childProcess.exec(
Expand All @@ -76,7 +60,7 @@ describe('fetchRemoteProjectConfig', () => {

describe('when there are no git remotes', () => {
it('returns empty', async () => {
const sandboxPath = await resetSandbox();
await resetSandbox(sandboxPath);
const execOpts = { cwd: sandboxPath };
await childProcess.exec(`git init`, execOpts);

Expand Down
12 changes: 4 additions & 8 deletions src/test/backport-e2e.mutation.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
/* eslint-disable jest/no-commented-out-tests */
import os from 'os';
import { resolve } from 'path';
import { Octokit } from '@octokit/rest';
import del = require('del');
import nock from 'nock';
import { getOptions } from '../options/options';
import { runSequentially } from '../runSequentially';
import { getCommits } from '../ui/getCommits';
import { mockConfigFiles } from './mockConfigFiles';
import { listenForCallsToNockScope } from './nockHelpers';
import { getDevAccessToken } from './private/getDevAccessToken';
import { getSandboxPath, resetSandbox } from './sandbox';

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

const E2E_TEST_DATA_PATH = resolve(`${__dirname}/_tmp_sandbox_/backport-e2e`);
const HOMEDIR_PATH = resolve(`${__dirname}/_tmp_sandbox_/backport-e2e/homedir`);
const sandboxPath = getSandboxPath({ filename: __filename });
const REPO_OWNER = 'backport-org';
const REPO_NAME = 'integration-test';
const BRANCH_WITH_ONE_COMMIT = 'backport/7.x/commit-5bf29b7d';
Expand All @@ -31,7 +27,7 @@ describe('backport e2e', () => {

beforeAll(async () => {
// set alternative homedir
jest.spyOn(os, 'homedir').mockReturnValue(HOMEDIR_PATH);
jest.spyOn(os, 'homedir').mockReturnValue(`${sandboxPath}/homedir`);
accessToken = await getDevAccessToken();

mockConfigFiles({
Expand Down Expand Up @@ -393,5 +389,5 @@ async function resetState(accessToken: string) {
branchName: BRANCH_WITH_TWO_COMMITS,
});

await del(E2E_TEST_DATA_PATH);
await resetSandbox(sandboxPath);
}
6 changes: 4 additions & 2 deletions src/test/handleUnbackportedPullRequests.private.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { resolve } from 'path';
import { getCommits, backportRun } from '../entrypoint.module';
import { getDevAccessToken } from './private/getDevAccessToken';
import { getSandboxPath } from './sandbox';

jest.unmock('find-up');
jest.setTimeout(15000);

const sandboxPath = getSandboxPath({ filename: __filename });

describe('Handle unbackported pull requests', () => {
it('shows missing backports for PR number 8', async () => {
const accessToken = await getDevAccessToken();
Expand Down Expand Up @@ -34,7 +36,7 @@ describe('Handle unbackported pull requests', () => {
repoName: 'repo-with-conflicts',
pullNumber: 12,
targetBranches: ['7.x'],
dir: resolve(`${__dirname}/_tmp_sandbox_/handleUnbackportedPullRequests`),
dir: sandboxPath,
ci: true,
publishStatusComment: false,
});
Expand Down
32 changes: 32 additions & 0 deletions src/test/sandbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path, { resolve } from 'path';
// import del from 'del';
import makeDir from 'make-dir';

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

export function getSandboxPath({
filename,
specname,
}: {
filename: string;
specname?: string;
}) {
const baseFilename = getFilenameWithoutExtension(filename);
return resolve(
`${__dirname}/_tmp_sandbox_/${baseFilename}${
specname ? `/${specname}` : ''
}`
);
}

export async function resetSandbox(sandboxPath: string) {
// await del(sandboxPath);
// eslint-disable-next-line no-console
console.log('deleting', sandboxPath);
await makeDir(sandboxPath);
}

function getFilenameWithoutExtension(filename: string) {
return path.basename(filename, path.extname(filename));
}

0 comments on commit be68ad9

Please sign in to comment.