Archived
1
0

not finished

This commit is contained in:
Asher
2019-01-07 18:46:19 -06:00
committed by Kyle Carberry
parent 776bb227e6
commit 9cd81f73fa
79 changed files with 11015 additions and 0 deletions

3
packages/requirefs/test/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
!lib/node_modules
*.tar
*.zip

View File

@ -0,0 +1 @@
exports = require("./chained-2");

View File

@ -0,0 +1 @@
exports = require("./chained-3");

View File

@ -0,0 +1 @@
exports.text = "moo";

View File

@ -0,0 +1 @@
exports = require("donkey");

View File

@ -0,0 +1 @@
exports.frog = "hi";

View File

@ -0,0 +1,3 @@
const frogger = require("frogger");
exports = frogger;

View File

@ -0,0 +1 @@
exports.banana = "potato";

View File

@ -0,0 +1 @@
exports = coder.test;

View File

@ -0,0 +1 @@
exports.orangeColor = require("./subfolder/oranges").orange;

View File

@ -0,0 +1 @@
exports = require("../individual");

View File

@ -0,0 +1 @@
exports.orange = "blue";

View 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 });

View 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");
});
});
}
});

View 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;
}
}