Skip to content

Commit

Permalink
Added testing with mocha, refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkrishnar committed Aug 8, 2020
1 parent b476e25 commit 372e5d6
Show file tree
Hide file tree
Showing 12 changed files with 1,129 additions and 509 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,3 @@ out
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

test/README.md
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"node": true,
"esversion": 8,
"esversion": 9,
"globals": {}
}
15 changes: 1 addition & 14 deletions blog-post-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const process = require('process');
let Parser = require('rss-parser');
const core = require('@actions/core');
const fs = require('fs');
const {spawn} = require('child_process');
const exec = require('./exec');

/**
* Builds the new readme by replacing the readme's <!-- BLOG-POST-LIST:START --><!-- BLOG-POST-LIST:END --> tags
Expand Down Expand Up @@ -48,19 +48,6 @@ const buildReadme = (previousContent, newContent) => {
* @return {Promise<void>}
*/
const commitReadme = async () => {
const exec = (cmd, args = []) => new Promise((resolve, reject) => {
console.log(`Started: ${cmd} ${args.join(' ')}`);
const app = spawn(cmd, args, {stdio: ['inherit', 'inherit', 'inherit']});
app.on('close', (code) => {
if (code !== 0) {
const err = new Error(`Invalid status code: ${code}`);
err.code = code;
return reject(err);
}
return resolve(code);
});
app.on('error', reject);
});
// Getting config
const committerUsername = core.getInput('committer_username');
const committerEmail = core.getInput('committer_email');
Expand Down
21 changes: 21 additions & 0 deletions exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const {spawn} = require('child_process');

const exec = (cmd, args = [], options = {}, stdio = ['inherit', 'inherit', 'inherit']) => new Promise((resolve, reject) => {
console.log(`Started: ${cmd} ${args.join(' ')}`);
const optionsToCLI = {
...options,
stdio
};
const app = spawn(cmd, args, optionsToCLI);
app.on('close', (code) => {
if (code !== 0) {
const err = new Error(`Invalid status code: ${code}`);
err.code = code;
return reject(err);
}
return resolve(code);
});
app.on('error', reject);
});

module.exports = exec;
24 changes: 24 additions & 0 deletions local-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const process = require('process');
const path = require('path');
const fs = require('fs');
// language=markdown
const template = `# Readme test
Post list example:
<!-- BLOG-POST-LIST:START -->
<!-- BLOG-POST-LIST:END -->
# Other contents
Test content
`;
fs.writeFile(path.join(__dirname, 'test', 'Readme.md'), template, () => {
console.log("Written test file....");
process.env.INPUT_MAX_POST_COUNT = "5";
process.env.INPUT_FEED_LIST = "http://localhost:8080";
process.env.INPUT_README_PATH = path.join(__dirname, 'test', 'Readme.md');
process.env.INPUT_DISABLE_SORT = "false";
process.env.INPUT_TEMPLATE = "default";
process.env.TEST_MODE = "true";
const testFile = process.env.DIST ? './dist/blog-post-workflow' :'./blog-post-workflow';
console.log('Testing: ', testFile);
require(testFile);
});
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"description": "Allows you to show your latest blog posts on your github profile or project readme",
"main": "blog-post-workflow.js",
"scripts": {
"start": "node blog-post-workflow.js",
"local-run": "start-server-and-test start-server http://localhost:8080 start-test",
"build": "parcel build --no-source-maps --target=node --bundle-node-modules blog-post-workflow.js",
"lint": "jshint --exclude='node_modules/' *.js",
"start-server": "node test/test-server.js",
"test-run": "node test.js && diff test/Readme.md test/Readme.md.snap",
"local-run-script": "node local-run.js",
"local-run": "start-server-and-test start-server http://localhost:8080 local-run-script",
"test-run": "mocha test.js",
"test": "npm run lint && start-server-and-test start-server http://localhost:8080 test-run",
"test-run-dist": "DIST=true node test.js && diff test/Readme.md test/Readme.md.snap",
"test-dist": "npm run lint && start-server-and-test start-server http://localhost:8080 test-run-dist",
"lint": "jshint --exclude='node_modules/' *.js",
"build": "parcel build --no-source-maps --target=node --bundle-node-modules blog-post-workflow.js"
"test-run-dist": "DIST=true mocha test.js",
"test-dist": "npm run lint && start-server-and-test start-server http://localhost:8080 test-run-dist"
},
"repository": {
"type": "git",
Expand All @@ -36,6 +36,7 @@
"rss-parser": "^3.8.0"
},
"devDependencies": {
"mocha": "^8.1.1",
"jshint": "^2.11.1",
"parcel-bundler": "^1.12.4",
"start-server-and-test": "^1.11.2"
Expand Down
75 changes: 62 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
const assert = require('assert');
const process = require('process');
const path = require('path');
const fs = require('fs');
const exec = require('./exec');

// language=markdown
const template = `# Readme test
const TEMPLATE = `# Readme test
Post list example:
<!-- BLOG-POST-LIST:START -->
<!-- BLOG-POST-LIST:END -->
# Other contents
Test content
`;
fs.writeFile(path.join(__dirname, 'test', 'Readme.md'), template, () => {
console.log("Written test file....");
process.env.INPUT_MAX_POST_COUNT = "5";
process.env.INPUT_FEED_LIST = "http://localhost:8080";
process.env.INPUT_README_PATH = path.join(__dirname, 'test', 'Readme.md');
process.env.INPUT_DISABLE_SORT = "false";
process.env.INPUT_TEMPLATE = "default";
process.env.TEST_MODE = "true";
const testFile = process.env.DIST ? './dist/blog-post-workflow' :'./blog-post-workflow';
console.log('Testing: ', testFile);
require(testFile);
});
const TEST_FILE = process.env.DIST ? './blog-post-workflow' :'./blog-post-workflow';
console.log('Testing: ', TEST_FILE);

// Test block
describe('Blog post workflow tests', function () {
it('Default template readme generated should match the snapshot',async function () {
const README_FILE = 'Readme.md';
fs.writeFileSync(path.join(__dirname, 'test', README_FILE), TEMPLATE);
const envObj = {
...process.env,
INPUT_MAX_POST_COUNT: "5",
INPUT_FEED_LIST: "http://localhost:8080",
INPUT_README_PATH: path.join(__dirname, 'test', README_FILE),
INPUT_DISABLE_SORT: "false",
INPUT_TEMPLATE: "default",
TEST_MODE: "true"
};
await exec('node', ['blog-post-workflow.js'],{env: envObj});
const snapshot = fs.readFileSync(path.join(__dirname, 'test' , README_FILE + '.snap'), "utf-8");
const newReadme = fs.readFileSync(path.join(__dirname, 'test' , README_FILE), "utf-8");
assert.equal(snapshot, newReadme);
});

it('Sorting disabled readme should be equal to the saved snapshot',async function () {
const README_FILE = 'Readme.sort.md';
fs.writeFileSync(path.join(__dirname, 'test', README_FILE), TEMPLATE);
const envObj = {
...process.env,
INPUT_MAX_POST_COUNT: "5",
INPUT_FEED_LIST: "http://localhost:8080",
INPUT_README_PATH: path.join(__dirname, 'test', README_FILE),
INPUT_DISABLE_SORT: "true",
INPUT_TEMPLATE: "default",
TEST_MODE: "true"
};
await exec('node', ['blog-post-workflow.js'],{env: envObj});
const snapshot = fs.readFileSync(path.join(__dirname, 'test' , README_FILE + '.snap'), "utf-8");
const newReadme = fs.readFileSync(path.join(__dirname, 'test' , README_FILE), "utf-8");
assert.equal(snapshot, newReadme);
});

it('Custom template readme generated should match the snapshot',async function () {
const README_FILE = 'Readme.custom.md';
fs.writeFileSync(path.join(__dirname, 'test', README_FILE), TEMPLATE);
const envObj = {
...process.env,
INPUT_MAX_POST_COUNT: "5",
INPUT_FEED_LIST: "http://localhost:8080",
INPUT_README_PATH: path.join(__dirname, 'test', README_FILE),
INPUT_DISABLE_SORT: "false",
INPUT_TEMPLATE: "$newline[$title]($url) $newline",
TEST_MODE: "true",
};
await exec('node', ['blog-post-workflow.js'],{env: envObj});
const snapshot = fs.readFileSync(path.join(__dirname, 'test' , README_FILE + '.snap'), "utf-8");
const newReadme = fs.readFileSync(path.join(__dirname, 'test' , README_FILE), "utf-8");
assert.equal(snapshot, newReadme);
});
});
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.md
16 changes: 16 additions & 0 deletions test/Readme.custom.md.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Readme test
Post list example:
<!-- BLOG-POST-LIST:START -->
[God Mode in browsers: document.designMode = "on"](https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo)

[Skipping the Chrome "Your connection is not private" warning](https://dev.to/gautamkrishnar/quickbits-1-skipping-the-chrome-your-connection-is-not-private-warning-4kp1)

[Microsoft Student PartnersGeek is the new rockstar](https://dev.to/gautamkrishnar/microsoft-student-partners--geek-is-the-new-rockstar)

[An Introduction to NumPy](https://dev.to/gautamkrishnar/an-introduction-to-numpy)

[Hi, I'm Gautam krishna.R](https://dev.to/gautamkrishnar/hi-im-gautam-krishnar)
<!-- BLOG-POST-LIST:END -->

# Other contents
Test content
12 changes: 12 additions & 0 deletions test/Readme.sort.md.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Readme test
Post list example:
<!-- BLOG-POST-LIST:START -->
- [Hi, I'm Gautam krishna.R](https://dev.to/gautamkrishnar/hi-im-gautam-krishnar)
- [God Mode in browsers: document.designMode = "on"](https://dev.to/gautamkrishnar/god-mode-in-browsers-document-designmode-on-2pmo)
- [Microsoft Student PartnersGeek is the new rockstar](https://dev.to/gautamkrishnar/microsoft-student-partners--geek-is-the-new-rockstar)
- [DuckDuckGoThe search engine redefined](https://dev.to/gautamkrishnar/duckduckgo-the-search-engine-redefined-4c7d)
- [Skipping the Chrome "Your connection is not private" warning](https://dev.to/gautamkrishnar/quickbits-1-skipping-the-chrome-your-connection-is-not-private-warning-4kp1)
<!-- BLOG-POST-LIST:END -->

# Other contents
Test content
Loading

0 comments on commit 372e5d6

Please sign in to comment.