Skip to content

Commit 3945084

Browse files
author
NHibiki
committed
add a README url check for CircleCI
1 parent c8ceb1b commit 3945084

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ jobs:
1212
steps:
1313
- checkout
1414
- run: npm install
15+
- run: npm install node-fetch
1516
- run: npm test

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unit Testing and Continuous Integration
22

3+
[![CircleCI](https://circleci.com/gh/NHibiki-NYU/UnitTestingCI.svg?style=svg)](https://circleci.com/gh/NHibiki-NYU/UnitTestingCI)
4+
35
This repository is for sudents in the course to experiment with unit testing and continuous integration. It uses the testing framework [Jest](https://jestjs.io/) and [CircleCI](https://circleci.com/) as described in the video tutorials linked below.
46

57
This repo is forked from [CodingTrain/TestingTestTest](https://github.com/CodingTrain/TestingTestTest).

sketch.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const { sum, sub, prod, digital_root, sum42, sayHelloTo, anomalyCode, nOfFibonacci } = require('./sketch');
2+
const fs = require("fs");
3+
const path = require("path");
4+
const fetch = require("node-fetch");
25

36
// test('adds 1 + 2 to equal 3', () => {
47
// expect(sum(1, 2)).toBe(3);
@@ -97,3 +100,28 @@ test('anomalyCode thousand should be 50000', () => {
97100
test('the 20th number of fibonacci should be 6765', () => {
98101
expect(nOfFibonacci(20)).toBe(6765);
99102
})
103+
104+
const testUrlInMarkdown = (file) => {
105+
let markdown = fs.readFileSync(file).toString();
106+
let urls = [];
107+
let regExp = /\[([^\]]+)\]\(([^)]+)\)/g;
108+
let result = null;
109+
while (result = regExp.exec(markdown)) urls.push(result[2]);
110+
return [urls.length, async () => {
111+
for (let url of urls) {
112+
if (url.includes("://") && !url.split("://")[0].includes("/")) {
113+
// An External url
114+
let res = await fetch(url);
115+
if (![200, 301, 302].includes(res.status)) throw new Error(`External Link Test: ${url} with bad response code ${res.status}`);
116+
} else {
117+
if (!fs.existsSync(path.join(path.dirname(file), url))) throw new Error(`Internal Link Test: ${url} with not found error`);
118+
}
119+
}
120+
}];
121+
}
122+
123+
it('tests url avaliability in README.md', async () => {
124+
let [testCount, testFunc] = testUrlInMarkdown("README.md");
125+
jest.setTimeout(testCount * 5000);
126+
return await testFunc();
127+
});

0 commit comments

Comments
 (0)