Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors #4

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run Tests

on: [push]

jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Install dependencies
run: npm install

- name: Run Jest tests
run: npm run test
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ Read the blacklight-collector README for a full list of options and their defaul
## Outputs

All scans will be saved in the `outputs` folder, in sub-folders named for the hostname of the url being scanned.

## Testing

`npm run test`
24 changes: 24 additions & 0 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { reportFailures } from '../src/utils';

const logSpy = jest.spyOn(console, 'log');

afterEach(() => {
jest.clearAllMocks();
});

test('correctly outputs if all urls succeeded', () => {
const failedUrls = [];
reportFailures(failedUrls, 100);
expect(logSpy).toHaveBeenCalledWith('All URLs were successfully scanned! 🎉');
});

test('correctly outputs when there are failed Urls', () => {
const failedUrls = ['url1', 'url2'];
const totalUrlNum = 5;
reportFailures(failedUrls, totalUrlNum);
expect(logSpy).toHaveBeenCalledTimes(4);
expect(logSpy).toHaveBeenCalledWith(`${failedUrls.length} out of ${totalUrlNum} URLs failed`);
expect(logSpy).toHaveBeenCalledWith('The following URLs failed:');
expect(logSpy).toHaveBeenCalledWith(failedUrls[0]);
expect(logSpy).toHaveBeenCalledWith(failedUrls[1]);
});
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://themarkup.org/donate"
},
"scripts": {
"main": "ts-node main.ts",
"main": "ts-node src/main.ts",
"test": "jest",
"clean": "rimraf outputs -i",
"prettier": "prettier \"*.ts\" --write"
Expand Down Expand Up @@ -44,9 +44,7 @@
"ts-jest": {
"diagnostics": false
}
},
"globalSetup": "./config/test-server-setup.js",
"globalTeardown": "./config/test-server-teardown.js"
}
},
"publishConfig": {
"access": "public"
Expand Down
7 changes: 4 additions & 3 deletions main.ts → src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { collect } from "@themarkup/blacklight-collector";
import { reportFailures } from "./utils";

// Gather URLs from input file
if (!fs.existsSync("urls.txt")) {
const urlsPath = join(__dirname, '../urls.txt');
if (!fs.existsSync(urlsPath)) {
console.log(
"Please create a file named 'urls.txt', containing a newline-separated list of urls to scan."
);
exit();
}
const urls = fs.readFileSync("urls.txt", "utf8");
const urls = fs.readFileSync(urlsPath, "utf8");
const urlsList = urls.split("\n");
const outDir = "outputs";
const outDir = "../outputs";

let progressBar = progress.create({ total: urlsList.length });

Expand Down
File renamed without changes.
Loading