not finished
This commit is contained in:
3
packages/requirefs/test/.gitignore
vendored
Normal file
3
packages/requirefs/test/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
!lib/node_modules
|
||||
*.tar
|
||||
*.zip
|
1
packages/requirefs/test/lib/chained-1.js
Normal file
1
packages/requirefs/test/lib/chained-1.js
Normal file
@ -0,0 +1 @@
|
||||
exports = require("./chained-2");
|
1
packages/requirefs/test/lib/chained-2.js
Normal file
1
packages/requirefs/test/lib/chained-2.js
Normal file
@ -0,0 +1 @@
|
||||
exports = require("./chained-3");
|
1
packages/requirefs/test/lib/chained-3.js
Normal file
1
packages/requirefs/test/lib/chained-3.js
Normal file
@ -0,0 +1 @@
|
||||
exports.text = "moo";
|
1
packages/requirefs/test/lib/customModule.js
Normal file
1
packages/requirefs/test/lib/customModule.js
Normal file
@ -0,0 +1 @@
|
||||
exports = require("donkey");
|
1
packages/requirefs/test/lib/individual.js
Normal file
1
packages/requirefs/test/lib/individual.js
Normal file
@ -0,0 +1 @@
|
||||
exports.frog = "hi";
|
3
packages/requirefs/test/lib/nodeResolve.js
Normal file
3
packages/requirefs/test/lib/nodeResolve.js
Normal file
@ -0,0 +1,3 @@
|
||||
const frogger = require("frogger");
|
||||
|
||||
exports = frogger;
|
1
packages/requirefs/test/lib/node_modules/frogger/index.js
generated
vendored
Normal file
1
packages/requirefs/test/lib/node_modules/frogger/index.js
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.banana = "potato";
|
1
packages/requirefs/test/lib/scope.js
Normal file
1
packages/requirefs/test/lib/scope.js
Normal file
@ -0,0 +1 @@
|
||||
exports = coder.test;
|
1
packages/requirefs/test/lib/subfolder.js
Normal file
1
packages/requirefs/test/lib/subfolder.js
Normal file
@ -0,0 +1 @@
|
||||
exports.orangeColor = require("./subfolder/oranges").orange;
|
1
packages/requirefs/test/lib/subfolder/goingUp.js
Normal file
1
packages/requirefs/test/lib/subfolder/goingUp.js
Normal file
@ -0,0 +1 @@
|
||||
exports = require("../individual");
|
1
packages/requirefs/test/lib/subfolder/oranges.js
Normal file
1
packages/requirefs/test/lib/subfolder/oranges.js
Normal file
@ -0,0 +1 @@
|
||||
exports.orange = "blue";
|
48
packages/requirefs/test/requirefs.bench.ts
Normal file
48
packages/requirefs/test/requirefs.bench.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import * as benchmark from "benchmark";
|
||||
import { performance } from "perf_hooks";
|
||||
import { TestCaseArray, isMac } from "./requirefs.util";
|
||||
|
||||
const files = [
|
||||
"./individual.js", "./chained-1", "./subfolder",
|
||||
"./subfolder/goingUp", "./nodeResolve",
|
||||
];
|
||||
const toBench = new TestCaseArray();
|
||||
|
||||
// Limits the amount of time taken for each test,
|
||||
// but increases uncertainty.
|
||||
benchmark.options.maxTime = 0.5;
|
||||
|
||||
let suite = new benchmark.Suite();
|
||||
let _start = 0;
|
||||
const addMany = (names: string[]): benchmark.Suite => {
|
||||
for (let name of names) {
|
||||
for (let file of files) {
|
||||
suite = suite.add(`${name} -> ${file}`, async () => {
|
||||
let rfs = await toBench.byName(name).rfs;
|
||||
rfs.require(file);
|
||||
});
|
||||
}
|
||||
}
|
||||
_start = performance.now();
|
||||
return suite;
|
||||
}
|
||||
// Returns mean time per operation, in microseconds (10^-6s).
|
||||
const mean = (c: any): number => {
|
||||
return Number((c.stats.mean * 10e+5).toFixed(5));
|
||||
};
|
||||
|
||||
// Swap out the tar command for gtar, when on MacOS.
|
||||
let testNames = ["zip", "bsdtar", isMac ? "gtar" : "tar"];
|
||||
addMany(testNames).on("cycle", (event: benchmark.Event) => {
|
||||
console.log(String(event.target) + ` (~${mean(event.target)} μs/op)`);
|
||||
}).on("complete", () => {
|
||||
const slowest = suite.filter("slowest").shift();
|
||||
const fastest = suite.filter("fastest").shift();
|
||||
console.log(`===\nFastest is ${fastest.name} with ~${mean(fastest)} μs/op`);
|
||||
if (slowest.name !== fastest.name) {
|
||||
console.log(`Slowest is ${slowest.name} with ~${mean(slowest)} μs/op`);
|
||||
}
|
||||
const d = ((performance.now() - _start)/1000).toFixed(2);
|
||||
console.log(`Benchmark took ${d} s`);
|
||||
})
|
||||
.run({ "async": true });
|
56
packages/requirefs/test/requirefs.test.ts
Normal file
56
packages/requirefs/test/requirefs.test.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import { RequireFS } from "../src/requirefs";
|
||||
import { TestCaseArray, isMac } from "./requirefs.util";
|
||||
|
||||
const toTest = new TestCaseArray();
|
||||
|
||||
describe("requirefs", () => {
|
||||
for (let i = 0; i < toTest.length(); i++) {
|
||||
const testCase = toTest.byID(i);
|
||||
if (!isMac && testCase.name === "gtar") {
|
||||
break;
|
||||
}
|
||||
if (isMac && testCase.name === "tar") {
|
||||
break;
|
||||
}
|
||||
|
||||
describe(testCase.name, () => {
|
||||
let rfs: RequireFS;
|
||||
beforeAll(async () => {
|
||||
rfs = await testCase.rfs;
|
||||
});
|
||||
|
||||
it("should parse individual module", () => {
|
||||
expect(rfs.require("./individual.js").frog).toEqual("hi");
|
||||
});
|
||||
|
||||
it("should parse chained modules", () => {
|
||||
expect(rfs.require("./chained-1").text).toEqual("moo");
|
||||
});
|
||||
|
||||
it("should parse through subfolders", () => {
|
||||
expect(rfs.require("./subfolder").orangeColor).toEqual("blue");
|
||||
});
|
||||
|
||||
it("should be able to move up directories", () => {
|
||||
expect(rfs.require("./subfolder/goingUp").frog).toEqual("hi");
|
||||
});
|
||||
|
||||
it("should resolve node_modules", () => {
|
||||
expect(rfs.require("./nodeResolve").banana).toEqual("potato");
|
||||
});
|
||||
|
||||
it("should access global scope", () => {
|
||||
// tslint:disable-next-line no-any for testing
|
||||
(window as any).coder = {
|
||||
test: "hi",
|
||||
};
|
||||
expect(rfs.require("./scope")).toEqual("hi");
|
||||
});
|
||||
|
||||
it("should find custom module", () => {
|
||||
rfs.provide("donkey", "ok");
|
||||
expect(rfs.require("./customModule")).toEqual("ok");
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
112
packages/requirefs/test/requirefs.util.ts
Normal file
112
packages/requirefs/test/requirefs.util.ts
Normal file
@ -0,0 +1,112 @@
|
||||
import * as cp from "child_process";
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
import * as os from "os";
|
||||
import { fromTar, RequireFS, fromZip } from "../src/requirefs";
|
||||
|
||||
export const isMac = os.platform() === "darwin";
|
||||
|
||||
/**
|
||||
* Encapsulates a RequireFS Promise and the
|
||||
* name of the test case it will be used in.
|
||||
*/
|
||||
interface TestCase {
|
||||
rfs: Promise<RequireFS>;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* TestCaseArray allows tests and benchmarks to share
|
||||
* test cases while limiting redundancy.
|
||||
*/
|
||||
export class TestCaseArray {
|
||||
private cases: Array<TestCase> = [];
|
||||
|
||||
constructor(cases?: Array<TestCase>) {
|
||||
if (!cases) {
|
||||
this.cases = TestCaseArray.defaults();
|
||||
return
|
||||
}
|
||||
this.cases = cases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns default test cases. MacOS users need to have `gtar` binary
|
||||
* in order to run GNU-tar tests and benchmarks.
|
||||
*/
|
||||
public static defaults(): Array<TestCase> {
|
||||
let cases: Array<TestCase> = [
|
||||
TestCaseArray.newCase("cd lib && zip -r ../lib.zip ./*", "lib.zip", async (c) => fromZip(c), "zip"),
|
||||
TestCaseArray.newCase("cd lib && bsdtar cvf ../lib.tar ./*", "lib.tar", async (c) => fromTar(c), "bsdtar"),
|
||||
];
|
||||
if (isMac) {
|
||||
const gtarInstalled: boolean = cp.execSync("which tar").length > 0;
|
||||
if (gtarInstalled) {
|
||||
cases.push(TestCaseArray.newCase("cd lib && gtar cvf ../lib.tar ./*", "lib.tar", async (c) => fromTar(c), "gtar"));
|
||||
} else {
|
||||
throw new Error("failed to setup gtar test case, gtar binary is necessary to test GNU-tar on MacOS");
|
||||
}
|
||||
} else {
|
||||
cases.push(TestCaseArray.newCase("cd lib && tar cvf ../lib.tar ./*", "lib.tar", async (c) => fromTar(c), "tar"));
|
||||
}
|
||||
return cases;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a test case prepared with the provided RequireFS Promise.
|
||||
* @param command Command to run immediately. For setup.
|
||||
* @param targetFile File to be read and handled by prepare function.
|
||||
* @param prepare Run on target file contents before test.
|
||||
* @param name Test case name.
|
||||
*/
|
||||
public static newCase(command: string, targetFile: string, prepare: (content: Uint8Array) => Promise<RequireFS>, name: string): TestCase {
|
||||
cp.execSync(command, { cwd: __dirname });
|
||||
const content = fs.readFileSync(path.join(__dirname, targetFile));
|
||||
return {
|
||||
name,
|
||||
rfs: prepare(content),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns updated TestCaseArray instance, with a new test case.
|
||||
* @see TestCaseArray.newCase
|
||||
*/
|
||||
public add(command: string, targetFile: string, prepare: (content: Uint8Array) => Promise<RequireFS>, name: string): TestCaseArray {
|
||||
this.cases.push(TestCaseArray.newCase(command, targetFile, prepare, name));
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a test case by index.
|
||||
* @param id Test case index.
|
||||
*/
|
||||
public byID(id: number): TestCase {
|
||||
if (!this.cases[id]) {
|
||||
if (id < 0 || id >= this.cases.length) {
|
||||
throw new Error(`test case index "${id}" out of bounds`);
|
||||
}
|
||||
throw new Error(`test case at index "${id}" not found`);
|
||||
}
|
||||
return this.cases[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a test case by name.
|
||||
* @param name Test case name.
|
||||
*/
|
||||
public byName(name: string): TestCase {
|
||||
let c = this.cases.find((c) => c.name === name);
|
||||
if (!c) {
|
||||
throw new Error(`test case "${name}" not found`);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of test cases.
|
||||
*/
|
||||
public length(): number {
|
||||
return this.cases.length;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user