Skip to content

Commit

Permalink
Generate fixtures resource file
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Buck committed Jul 4, 2018
1 parent b5afde3 commit 0d0bfb5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
packages/**/node_modules
e2e/fixtures/
75 changes: 75 additions & 0 deletions e2e/generate-fixtures-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const fs = require('fs');
const path = require('path');
const util = require('util');
const recursive = require('recursive-readdir');
const async = require('async');

// This will generate a json file which is consumed by automated.test.js in the following format.
// Checkout out https://github.com/OctoLinker/OctoLinker/blob/master/e2e/README.md for details.
//
// [
// {
// "url": "https://github.com/OctoLinker/OctoLinker/blob/master/e2e/fixtures/javascript/nodejs/gentle-resonance-3436.js",
// "content": "require('./proud-tooth-7361');",
// "targetUrl": "/javascript/nodejs/proud-tooth-7361.js",
// "lineNumber": 2
// },
// ...
// ]

const repoSlug = process.env.TRAVIS_PULL_REQUEST_SLUG || 'OctoLinker/OctoLinker';
const branch = process.env.TRAVIS_BRANCH || 'master';
const fixturesRoot = `https://github.com/${repoSlug}/blob/${branch}/e2e`;

async function readContent(files) {
const readFile = util.promisify(fs.readFile);
const mapLimit = util.promisify(async.mapLimit);

return mapLimit(files, 10, async file => {
const content = await readFile(file, 'utf8');

return {
file,
content,
};
});
}

function findTests(contents) {
return contents.reduce((memo, { file, content }) => {
const lines = content.split('\n');

lines.forEach((line, index) => {
if (line.includes('@OctoLinkerResolve')) {
const lineNumber = index + 2;
const targetUrl = line
.match(/@OctoLinkerResolve\((.*?)\)/)[1]
.replace('<root>', '');

const filePath = file.replace(__dirname, '');

memo.push({
url: `${fixturesRoot}${filePath}`,
content: lines[index + 1].trim(),
targetUrl,
lineNumber,
});
}
});

return memo;
}, []);
}

(async function init() {
const files = await recursive(path.join(__dirname, 'fixtures'));
const contents = await readContent(files);
const out = findTests(contents);

console.log(out); // eslint-disable-line

fs.writeFileSync(
path.join(__dirname, 'fixtures.json'),
JSON.stringify(out, null, ' '),
);
})();
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint": "eslint .",
"pretest": "yarn lint",
"test": "jest",
"e2e": "curl https://raw.githubusercontent.com/OctoLinker/e2e/master/resource.json > e2e/fixtures.json && jest --config=e2e/jest-e2e.config.js",
"e2e": "node ./e2e/generate-fixtures-map.js && jest --config=e2e/jest-e2e.config.js",
"postinstall": "yarn lerna bootstrap",
"test:watch": "jest --watch",
"version": "json -I -f assets/manifest.json -e \"this.version='`json -f package.json version`'\" && git add assets/manifest.json",
Expand All @@ -22,6 +22,7 @@
"firefox-launch": "web-ext run --source-dir dist --pref startup.homepage_welcome_url=https://github.com/OctoLinker/OctoLinker/blob/master/package.json"
},
"devDependencies": {
"async": "^2.6.1",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
Expand All @@ -48,6 +49,7 @@
"npm-run-all": "^4.1.1",
"prettier": "^1.7.4",
"puppeteer": "^1.5.0",
"recursive-readdir": "^2.2.2",
"sinon": "^4.0.1",
"style-loader": "^0.19.0",
"web-ext": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ async@^1.4.0, async@^1.5.0, async@^1.5.2, async@~1.5:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"

async@^2.0.0, async@~2.6.0:
async@^2.0.0, async@^2.6.1, async@~2.6.0:
version "2.6.1"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.1.tgz#b245a23ca71930044ec53fa46aa00a3e87c6a610"
dependencies:
Expand Down

0 comments on commit 0d0bfb5

Please sign in to comment.