build: Initial linting setup (#1560)

* sets up linting config and runs `yarn lint --fix` once, so that all changes will show up correctly in future PRs.

* Note that there are still a lot of linter errors.
This commit is contained in:
Nathan Nichols
2021-08-26 21:22:40 -05:00
committed by Mayukha Vadari
parent 12cfed5c17
commit 8b95ee5fab
286 changed files with 15508 additions and 12691 deletions

View File

@@ -1,9 +1,11 @@
import net from 'net'
import _ from 'lodash'
import fs from 'fs'
import path from 'path'
import {Client} from 'xrpl-local'
import assert from 'assert-diff'
import fs from "fs";
import net from "net";
import path from "path";
import assert from "assert-diff";
import _ from "lodash";
import { Client } from "xrpl-local";
/**
* The test function. It takes a Client object and then some other data to
@@ -14,13 +16,13 @@ export type TestFn = (
client: Client,
address: string,
mockRippled?: any
) => void | PromiseLike<void>
) => void | PromiseLike<void>;
/**
* A suite of tests to run. Maps the test name to the test function.
*/
export interface TestSuite {
[testName: string]: TestFn
[testName: string]: TestFn;
}
/**
@@ -30,17 +32,21 @@ export interface TestSuite {
* so that we can report it.
*/
interface LoadedTestSuite {
name: string
tests: [string, TestFn][]
name: string;
tests: Array<[string, TestFn]>;
config: {
/** Set to true to skip re-running tests with an X-address. */
skipXAddress?: boolean
}
skipXAddress?: boolean;
};
}
/**
* Check the response against the expected result. Optionally validate
* that response against a given schema as well.
*
* @param response - Response received from the method.
* @param expected - Expected response from the method.
* @param schemaName - Name of the schema used to validate the shape of the response.
*/
export function assertResultMatch(
response: any,
@@ -48,29 +54,33 @@ export function assertResultMatch(
schemaName?: string
) {
if (expected.txJSON) {
assert(response.txJSON)
assert(response.txJSON);
assert.deepEqual(
JSON.parse(response.txJSON),
JSON.parse(expected.txJSON),
'checkResult: txJSON must match'
)
"checkResult: txJSON must match"
);
}
if (expected.tx_json) {
assert(response.tx_json)
assert(response.tx_json);
assert.deepEqual(
response.tx_json,
expected.tx_json,
'checkResult: tx_json must match'
)
"checkResult: tx_json must match"
);
}
assert.deepEqual(
_.omit(response, ['txJSON', 'tx_json']),
_.omit(expected, ['txJSON', 'tx_json'])
)
_.omit(response, ["txJSON", "tx_json"]),
_.omit(expected, ["txJSON", "tx_json"])
);
}
/**
* Check that the promise rejects with an expected error.
*
* @param promise - The promise returned by the method.
* @param instanceOf - Expected error type that the method will throw.
* @param message - Expected error message/substring of the error message.
*/
export async function assertRejects(
promise: PromiseLike<any>,
@@ -78,14 +88,14 @@ export async function assertRejects(
message?: string | RegExp
) {
try {
await promise
assert(false, 'Expected an error to be thrown')
await promise;
assert(false, "Expected an error to be thrown");
} catch (error) {
assert(error instanceof instanceOf, error.message)
if (typeof message === 'string') {
assert.strictEqual(error.message, message)
assert(error instanceof instanceOf, error.message);
if (typeof message === "string") {
assert.strictEqual(error.message, message);
} else if (message instanceof RegExp) {
assert(message.test(error.message))
assert(message.test(error.message));
}
}
}
@@ -93,61 +103,63 @@ export async function assertRejects(
// using a free port instead of a constant port enables parallelization
export function getFreePort() {
return new Promise((resolve, reject) => {
const server = net.createServer()
let port
server.on('listening', function () {
port = (server.address() as any).port
server.close()
})
server.on('close', function () {
resolve(port)
})
server.on('error', function (error) {
reject(error)
})
server.listen(0)
})
const server = net.createServer();
let port;
server.on("listening", function () {
port = (server.address() as any).port;
server.close();
});
server.on("close", function () {
resolve(port);
});
server.on("error", function (error) {
reject(error);
});
server.listen(0);
});
}
export function getAllPublicMethods(client: Client) {
return Array.from(
new Set([
...Object.getOwnPropertyNames(client),
...Object.getOwnPropertyNames(Client.prototype)
...Object.getOwnPropertyNames(Client.prototype),
])
).filter((key) => !key.startsWith('_'))
).filter((key) => !key.startsWith("_"));
}
export function loadTestSuites(): LoadedTestSuite[] {
const allTests = fs.readdirSync(path.join(__dirname, 'client'), {
encoding: 'utf8'
})
const allTests: any[] = fs.readdirSync(path.join(__dirname, "client"), {
encoding: "utf8",
});
return allTests
.map((methodName) => {
if (methodName.startsWith('.DS_Store')) {
return null
if (methodName.startsWith(".DS_Store")) {
return null;
}
if (methodName.endsWith('.ts')) {
methodName = methodName.slice(0, -3)
if (methodName.endsWith(".ts")) {
methodName = methodName.slice(0, -3);
}
const testSuite = require(`./client/${methodName}`)
const testSuite = require(`./client/${methodName}`);
return {
name: methodName,
config: testSuite.config || {},
tests: Object.entries(testSuite.default || {})
} as LoadedTestSuite
tests: Object.entries(testSuite.default || {}),
} as LoadedTestSuite;
})
.filter(Boolean)
.filter(Boolean) as LoadedTestSuite[];
}
/**
* Ignore WebSocket DisconnectErrors. Useful for making requests where we don't
* care about the response and plan to teardown the test before the response
* has come back.
*
* @param error - Thrown error.
*/
export function ignoreWebSocketDisconnect(error: Error): void {
if (error.message === 'websocket was closed') {
return
if (error.message === "websocket was closed") {
return;
}
throw error
}
throw error;
}