gh-action-bump-version/tests/end-to-end/index.test.js

141 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-08-20 23:51:35 +00:00
const dotenv = require('dotenv');
const setupTestRepo = require('./setupTestRepo');
const yaml = require('js-yaml');
const { readFileSync } = require('fs');
const { writeFile, readFile, mkdir } = require('fs/promises');
const { resolve, join } = require('path');
const { cwd } = require('process');
2021-09-09 04:25:16 +00:00
const git = require('./git');
2021-08-20 23:51:35 +00:00
const { getMostRecentWorkflowRun, getWorkflowRun } = require('./actionsApi');
dotenv.config();
const config = getTestConfig();
beforeAll(() => setupTestRepo(config.actionFiles));
config.suites.forEach((suite) => {
const suiteYaml = yaml.dump(suite.yaml);
describe(suite.name, () => {
beforeAll(async () => {
const pushYamlPath = join('.github', 'workflows', 'push.yml');
await mkdir(join(cwd(), '.github', 'workflows'), { recursive: true });
await writeFile(join(cwd(), pushYamlPath), suiteYaml);
await git('add', pushYamlPath);
});
suite.tests.forEach((commit) => {
test(commit.message, async () => {
await generateReadMe(commit, suiteYaml);
await git('commit', '--message', commit.message);
const mostRecentDate = await getMostRecentWorkflowRunDate();
2021-09-09 04:25:16 +00:00
await git('push');
2021-08-20 23:51:35 +00:00
const completedRun = await getCompletedRunAfter(mostRecentDate);
expect(completedRun.conclusion).toBe('success');
await assertExpectation(commit.expected);
});
});
});
});
function getTestConfig() {
const path = resolve(__dirname, './config.yaml');
const buffer = readFileSync(path);
const contents = buffer.toString();
const config = yaml.load(contents);
return config;
}
async function generateReadMe(commit, suiteYaml) {
const readmePath = 'README.md';
const readMeContents = [
'# Test Details',
'## .github/workflows/push.yml',
'```YAML',
yaml.dump(suiteYaml),
'```',
'## Message',
commit.message,
'## Expectation',
2021-09-09 04:25:16 +00:00
generateExpectationText(commit.expected),
2021-08-20 23:51:35 +00:00
].join('\n');
await writeFile(join(cwd(), readmePath), readMeContents);
await git('add', readmePath);
}
async function getCompletedRunAfter(date) {
const run = await pollFor(getMostRecentWorkflowRun, (run) => run !== null && new Date(run.created_at) > date);
const completedRun = await pollFor(
() => getWorkflowRun(run.id),
(run) => run.status === 'completed',
);
return completedRun;
}
function pollFor(getResult, validateResult) {
return new Promise((resolve, reject) => {
pollAndRetry();
async function pollAndRetry() {
try {
const result = await getResult();
if (validateResult(result)) {
resolve(result);
} else {
setTimeout(pollAndRetry, 1000);
}
} catch (error) {
reject(error);
}
}
});
}
async function getMostRecentWorkflowRunDate() {
const run = await getMostRecentWorkflowRun();
const date = run === null ? new Date(0) : new Date(run.created_at);
return date;
}
2021-09-09 04:25:16 +00:00
function generateExpectationText({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) {
const results = [`- **Version:** ${expectedVersion}`];
if (expectedTag) {
results.push(`- **Tag:** ${expectedTag}`);
}
if (expectedBranch) {
results.push(`- **Branch:** ${expectedBranch}`);
}
return results.join('\n');
}
async function assertExpectation({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) {
2021-08-20 23:51:35 +00:00
if (expectedTag === undefined) {
expectedTag = expectedVersion;
}
2021-09-09 04:25:16 +00:00
if (expectedBranch) {
await git('fetch', 'origin', expectedBranch);
await git('checkout', expectedBranch);
}
await git('pull');
2021-08-20 23:51:35 +00:00
const [packageVersion, latestTag] = await Promise.all([getPackageJsonVersion(), getLatestTag()]);
expect(packageVersion).toBe(expectedVersion);
expect(latestTag).toBe(expectedTag);
2021-09-09 04:25:16 +00:00
if (expectedBranch) {
await git('checkout', 'main');
}
2021-08-20 23:51:35 +00:00
}
async function getPackageJsonVersion() {
const path = join(cwd(), 'package.json');
const contents = await readFile(path);
const json = JSON.parse(contents);
return json.version;
}
async function getLatestTag() {
const result = await git({ suppressOutput: true }, 'describe', '--tags', '--abbrev=0');
return result.stdout;
}