Skip to content

Commit

Permalink
- r better types
Browse files Browse the repository at this point in the history
  • Loading branch information
isidore committed Apr 22, 2024
1 parent d9b142a commit 9b105bd
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 122 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
};
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@
},
"parserOptions": {
"sourceType": "module"
}
},

}
27 changes: 15 additions & 12 deletions lib/Approvals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import {ManualNamer} from "./ManualNamer";
import path from "path";
import {postRunCleanup} from "./postRunCleanup";
import {BinaryWriter} from "./Writers/BinaryWriter";
import {FileApprover, Namer, Writer, Reporter} from "./FileApprover";
import {FileApprover} from "./FileApprover";
import {Namer} from "./Core/Namer";
import {ReporterFactory} from "./Reporting/ReporterFactory";
import {stringifyKeysInOrder} from "./AUtils";
import {FinalMessages} from "./FinalMessages";
import {Writer} from "./Core/Writer";
import {Reporter} from "./Core/Reporter";


// if someone tries to call 'require("approvals")...' without calling ".mocha(...) or
Expand Down Expand Up @@ -132,15 +135,15 @@ function verify(dirName: string, testName: string, data: BinaryWriter | string,
}

function verifyAsJSON(dirName: string, testName: string, data: BinaryWriter | string, optionsOverride: any) {
return verifyAsJSONAndScrub(dirName, testName, data, null, optionsOverride);
return verifyAsJSONAndScrub(dirName, testName, data, Scrubbers.noScrubber, optionsOverride);
}

function verifyAsJSONAndScrub(dirName: string, testName: string, data: BinaryWriter | string, scrubber: Scrubber, optionsOverride: any): void {
return verifyAndScrub(dirName, testName, stringifyKeysInOrder(data), scrubber, optionsOverride);
}

function verifyWithControl(namer: Namer, writer: Writer, reporterFactory?: () => Reporter[], optionsOverride?: Partial<cfg.Config>) {
var newOptions = cfg.getConfig(optionsOverride);
const newOptions = cfg.getConfig(optionsOverride);

reporterFactory = reporterFactory || function () {
return [ReporterFactory.loadReporter(newOptions.reporters)];
Expand All @@ -156,7 +159,7 @@ module.exports = {
* Allows you to provide overrides to the default configuration.
*
* @example
* var approvals = require('approvals');
* const approvals = require('approvals');
* approvals.configure({
* reporters: ['p4merge']
* });
Expand All @@ -176,12 +179,12 @@ module.exports = {
*
* @example
* // basic approval test
* var approvals = require('approvals');
* const approvals = require('approvals');
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify");
*
* @example
* // basic approval test providing an option to override configuration
* var approvals = require('approvals');
* const approvals = require('approvals');
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify", { normalizeLineEndingsTo: true });
*
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
Expand All @@ -196,8 +199,8 @@ module.exports = {
*
* @example
* // basic approval test with a custom scrubber
* var approvals = require('approvals');
* var scrubber = approvals.scrubbers.multiScrubber([
* const approvals = require('approvals');
* const scrubber = approvals.scrubbers.multiScrubber([
* function (data) {
* return (data || '').replace("some text", "some other text");
* },
Expand All @@ -217,7 +220,7 @@ module.exports = {
* You can pass as "data" any javascript object to be JSON.stringified and run verify against.
*
* @example
* var approvals = require('approvals');
* const approvals = require('approvals');
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in an object" });
*
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
Expand All @@ -232,8 +235,8 @@ module.exports = {
* @example
* // basic approval test with a custom scrubber
* var approvals = require('approvals');
* var scrubber = approvals.scrubbers.multiScrubber([
* const approvals = require('approvals');
* const scrubber = approvals.scrubbers.multiScrubber([
* function (data) {
* return (data || '').replace("some text", "some other text");
* },
Expand Down Expand Up @@ -272,7 +275,7 @@ module.exports = {
* `reporters` gives access to the `MultiReporter`
*
* @example
* var MultiReporter = approvals.reporters.MultiReporter
* const MultiReporter = approvals.reporters.MultiReporter
*/
reporters: reportersExport,

Expand Down
5 changes: 5 additions & 0 deletions lib/Core/Namer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Namer {
getApprovedFile(ext: string): string;
getReceivedFile(ext: string): string;
}

9 changes: 9 additions & 0 deletions lib/Core/Reporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Config} from "../config";

export interface Reporter {
canReportOn(fileName: string): boolean;

report(approvedFileName: string, receivedFileName: string, options: Partial<Config>): void;

name: string;
}
5 changes: 5 additions & 0 deletions lib/Core/Writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Writer {
getFileExtension(): string;

write(filePath: string): void;
}
20 changes: 4 additions & 16 deletions lib/FileApprover.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import fs from 'fs';
import chalk from 'chalk';
import {Config} from "./config";
import {Namer} from "./Core/Namer";
import {Writer} from "./Core/Writer";
import {Reporter} from "./Core/Reporter";

export interface Namer {
getApprovedFile(ext: string): string;
getReceivedFile(ext: string): string;
}

export interface Writer {
getFileExtension(): string;
write(filePath: string): void;
}

export interface Reporter {
canReportOn(fileName: string): boolean;
report(approvedFileName: string, receivedFileName: string, options: Options): void;
name: string;
}

interface Options {
stripBOM?: boolean;
Expand Down Expand Up @@ -97,7 +85,7 @@ static verify(namer: Namer, writer: Writer, reporterFactory: () => Reporter[], o
// Delete the received file
fs.unlinkSync(receivedFileName);

process.emit("approvalFileApproved", approvedFileName);
process.emit("approvalFileApproved" , approvedFileName);

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 19, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 21, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 19, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 21, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 19, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20, x64)

No overload matches this call.

Check failure on line 88 in lib/FileApprover.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 21, x64)

No overload matches this call.
}}

export function verify(namer: Namer, writer: Writer, reporterFactory: () => Reporter[], options?: Partial<Config>): void {
Expand Down
Loading

0 comments on commit 9b105bd

Please sign in to comment.