Skip to content

Commit

Permalink
feat: fetch mock data for repos in repos.json and set up them for tes…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
sudiptog81 authored and ljharb committed Aug 22, 2021
1 parent de9befd commit d4f5589
Show file tree
Hide file tree
Showing 11 changed files with 785 additions and 847 deletions.
3 changes: 2 additions & 1 deletion bin/can-merge
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require('dotenv').config();
const getRepo = require('../utils/getRepo');
const getSHA = require('../utils/getSHA');

const buildQuery = require('../utils/buildQuery');
const runQuery = require('../utils/runQuery');
const evaluatePullRequest = require('../utils/evaluatePullRequest');

Expand Down Expand Up @@ -43,7 +44,7 @@ const evaluatePullRequest = require('../utils/evaluatePullRequest');

const token = process.env.GITHUB_TOKEN || args.token;

const response = await runQuery(args.repo, args.pr, token);
const response = await runQuery(buildQuery(args.repo, args.pr), token);

if (process.env.NODE_ENV === 'DEBUG') {
console.log(JSON.stringify(response, null, 2));
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
},
"scripts": {
"lint": "eslint --ext=js,mjs .",
"fetch-mocks": "node ./test/fetchMockdata.js",
"pretest": "npm run lint",
"tests-only": "nyc tape 'test/**/*.js'| tap-spec",
"test": "npm run tests-only",
"tests-only": "nyc tape 'test/**/*.test.js'| tap-spec",
"test": "NODE_ENV=TEST npm run tests-only",
"posttest": "aud --production"
},
"repository": {
Expand Down
66 changes: 66 additions & 0 deletions test/fetchMockData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const path = require('path');
const { writeFile } = require('fs/promises');

const buildQuery = require('../utils/buildQuery');
const evaluatePullRequest = require('../utils/evaluatePullRequest');
const runQuery = require('../utils/runQuery');

const { repositories } = require('./repos.json');

const buildTestQuery = (repo, lastN = 1) => {
const [owner, name] = repo.split('/');
return `
{
repository(owner: "${owner}", name: "${name}") {
pullRequests(last: ${lastN}) {
edges {
node {
state
url
title
number
}
}
}
}
rateLimit {
cost
remaining
}
}
`;
};

(async () => {
const mocks = [];

const repoResponses = repositories.map(async (repo) => {
const token = process.env.GITHUB_TOKEN;
const response = await runQuery(buildTestQuery(repo), token);

if (process.env.NODE_ENV === 'DEBUG') {
console.log(JSON.stringify(response, null, 2));
}

const pullRequests = response.repository.pullRequests.edges.map(({ node }) => node.number);

const prResponses = pullRequests.map(async (pr) => {
const res = await runQuery(buildQuery(repo, pr), token);
console.log(`Fetching status of last pull request for ${repo}...`);
const expected = evaluatePullRequest(res);
const { repository: { pullRequest: { commits: { nodes: [{ commit: { statusCheckRollup } }] } } } } = res;
mocks.push({
description: `evaluation of a PR with ${statusCheckRollup.state} should return ${expected}`,
expected,
res,
});
});

await Promise.all(prResponses);
});

await Promise.all(repoResponses);
await writeFile(path.join(__dirname, 'mocks.json'), JSON.stringify(mocks, null, 2), 'utf8');
})();
Loading

0 comments on commit d4f5589

Please sign in to comment.