Skip to content

Commit

Permalink
expanded validator, connected to index
Browse files Browse the repository at this point in the history
  • Loading branch information
Oaphi committed Aug 9, 2021
1 parent d32e014 commit f66dbdc
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { generateTampermonkeyHeaders } from "./generators/tampermonkey";
import { generateViolentMonkeyHeaders } from "./generators/violentmonkey";
import { scase } from "./utils/common";
import { getPackage } from "./utils/package";
import { validateMatchHeaders } from "./utils/validators";

const names: UserScriptManagerName[] = [
"greasemonkey",
Expand All @@ -35,6 +36,7 @@ export const generate = async <T extends GrantOptions>(
output,
spaces = 4,
direct = false,
matches = [],
...rest
}: GeneratorOptions<T>
) => {
Expand All @@ -52,10 +54,16 @@ export const generate = async <T extends GrantOptions>(
return "";
}

const { invalid, status, valid } = validateMatchHeaders(matches);
if (!status) {
console.log(bgRed`Invalid @match headers:\n` + invalid.join("\n"));
}

const handler = managerTypeMap[type] as HeaderGenerator<T>;

const content = handler(parsedPackage, {
...rest,
matches: valid,
spaces,
packagePath,
output,
Expand Down
7 changes: 6 additions & 1 deletion src/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export const validateMatchHeaders = (matches: string[]) => {
const validationRegex =
/^((?:https?|file|ftp|\*)(?=:\/\/)|(?:urn(?=:))):(?:\/\/)?(?:((?:\*||.+?)(?=\/|$)))?(\/\*|(?:.+?\*?)+)?|<all_urls>/;
return matches.every((match) => validationRegex.test(match));
const invalid = matches.filter((match) => !validationRegex.test(match));
return {
invalid,
status: !invalid.length,
valid: matches.filter((m) => !invalid.includes(m)),
};
};
98 changes: 86 additions & 12 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import { readFile, stat, unlink } from "fs/promises";
import { join } from "path";
import { promisify } from "util";
import { generate, GeneratorOptions } from "../src";
import { GrantOptions, GreasemonkeyGrants } from "../src/generators";
import { TampermonkeyGrants } from "../src/generators/tampermonkey/types";
import { CommonGrantOptions } from "../src/generators";
import {
GreasemonkeyGrantOptions,
GreasemonkeyGrants,
} from "../src/generators/greasemonkey/types";
import {
TampermonkeyGrantOptions,
TampermonkeyGrants,
} from "../src/generators/tampermonkey/types";
import { getLongest } from "../src/utils/common";

use(cpr);
Expand All @@ -19,7 +26,10 @@ describe("main", () => {
const output = join(base, "/test/headers.js");
const entry = "./src/index.ts";

const common: GeneratorOptions = { output, packagePath: pkg };
const common: GeneratorOptions<CommonGrantOptions> = {
output,
packagePath: pkg,
};

//@see https://developer.chrome.com/docs/extensions/mv2/match_patterns/
const allMatches: string[] = [
Expand All @@ -45,20 +55,34 @@ describe("main", () => {
"window.onurlchange",
];

const allGrantOptions: GrantOptions[] = [
const grantsGM: GreasemonkeyGrants[] = [
"GM.deleteValue",
"GM.getValue",
"GM.listValues",
"GM.setValue",
];

const grantOptionsCommon: CommonGrantOptions[] = [
"get",
"set",
"list",
"delete",
"unsafe",
];

const grantOptionsTM: TampermonkeyGrantOptions[] = [
...grantOptionsCommon,
"close",
"focus",
"change",
];

describe.skip("Greasemonkey", async () => {
//TODO: add once other commands ready
});
const grantOptionsGM: GreasemonkeyGrantOptions[] = [
...grantOptionsCommon,
"clip",
"fetch",
"notify",
];

describe("CLI Options", async function () {
this.timeout(5e3);
Expand All @@ -85,14 +109,14 @@ describe("main", () => {
});

it("-g options should correctly add @grant", async () => {
const gOpts = allGrantOptions.map((g) => `-g "${g}"`).join(" ");
const gOpts = grantOptionsTM.map((g) => `-g "${g}"`).join(" ");

const { stdout } = await aexec(
`ts-node ${entry} tampermonkey ${gOpts} -p ${pkg} -o ${output} -d`
);

const matched = stdout.match(/@grant\s+(.+)/g) || [];
expect(matched).length(allGrantOptions.length);
expect(matched).length(grantOptionsTM.length);

const allAreTampermonkeyHeaders = matched.every((grant) =>
grantsTM.includes(
Expand Down Expand Up @@ -152,7 +176,10 @@ describe("main", () => {
describe("Tampermonkey", async () => {
const artefacts: string[] = [];

const directCommon: GeneratorOptions = { ...common, direct: true };
const directCommon: GeneratorOptions<TampermonkeyGrantOptions> = {
...common,
direct: true,
};

//make sure test output will be cleared
beforeEach(() => artefacts.push(output));
Expand Down Expand Up @@ -180,11 +207,11 @@ describe("main", () => {
it("@grant headers should be generated", async () => {
const content = await generate("tampermonkey", {
...directCommon,
grants: allGrantOptions,
grants: grantOptionsTM,
});

const matched = content.match(/@grant\s+(.+)/g) || [];
expect(matched).length(allGrantOptions.length);
expect(matched).length(grantOptionsTM.length);
});

it("header names should be equally indented", async () => {
Expand All @@ -206,6 +233,53 @@ describe("main", () => {
});
});

describe("Greasemonkey", async () => {
const artefacts: string[] = [];

const directCommon: GeneratorOptions<GreasemonkeyGrantOptions> = {
...common,
direct: true,
};

//make sure test output will be cleared
beforeEach(() => artefacts.push(output));

afterEach(() => {
Promise.all(artefacts.map(unlink));
artefacts.length = 0;
});

it("headers are generated correctly", async () => {
const content = await generate("greasemonkey", directCommon);
expect(!!content).to.be.true;
});

it("@match headers should be generated", async () => {
const content = await generate("greasemonkey", {
...directCommon,
matches: allMatches,
});

const matched = content.match(/@match\s+(.+)/g) || [];
expect(matched).length(allMatches.length);
});

it("@grant headers should be generated", async () => {
const content = await generate("greasemonkey", {
...directCommon,
grants: grantOptionsGM,
});

const matched = content.match(/@grant\s+(.+)/g) || [];
expect(matched).length(grantOptionsGM.length);

grantsGM.forEach((grant) => {
expect(new RegExp(`\\b${grant}\\b`, "m").test(grant)).to.be
.true;
});
});
});

describe.skip("ViolentMonkey", async () => {
//TODO: add once other commands ready
});
Expand Down
14 changes: 9 additions & 5 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ describe("validators", () => {
];

it("should correctly validate valid headers", () => {
const valid = validateMatchHeaders(sampleMatches);
expect(valid).to.be.true;
const { status, invalid } = validateMatchHeaders(sampleMatches);
expect(status).to.be.true;
expect(invalid).to.be.empty;
});

it("should correctly validate invalid headers", () => {
const valid = validateMatchHeaders([
const typo = "http//typo.ed/*";

const { status, invalid } = validateMatchHeaders([
...sampleMatches,
"http//typo.ed/*",
typo,
]);
expect(valid).to.be.false;
expect(status).to.be.false;
expect(invalid).to.contain(typo);
});
});
});

0 comments on commit f66dbdc

Please # to comment.