Skip to content

Commit

Permalink
Improve integration tests (#225)
Browse files Browse the repository at this point in the history
* add tests that include all files

* adding mod tests

* improve accuracy of test coverage

* improve snapshot tests

* Delete type_utils_test.ts

* Improve the builtin generation tests

* revert file name changes

* clean up

* Add tests
  • Loading branch information
WilliamBergamin authored Oct 6, 2023
1 parent e4f2538 commit 94858bf
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 24 deletions.
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
},
"tasks": {
"test": "deno fmt --check && deno lint && deno bundle src/mod.ts && deno test --allow-read --parallel src/ tests/",
"test": "deno fmt --check && deno lint && deno bundle src/mod.ts && deno test --allow-read --allow-run --parallel src/ tests/",
"coverage": "rm -rf .coverage && deno test --reporter=dot --parallel --allow-read --coverage=.coverage src/ && deno coverage --exclude=fixtures --exclude=_test --lcov --output=lcov.info .coverage && deno run --allow-read https://deno.land/x/[email protected]/cli.ts"
},
"lock": false
Expand Down
3 changes: 3 additions & 0 deletions src/schema/slack/functions/_scripts/src/templates/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { SlackFunctionTemplate } from "./template_function.ts";
export { SlackTestFunctionTemplate } from "./test_template.ts";
export { SlackFunctionModTemplate } from "./template_mod.ts";
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getSlackCallbackId,
renderTypeImports,
sanitize,
} from "./template_utils.ts";
} from "./utils.ts";
import { AllowedTypeValue, AllowedTypeValueObject } from "./types.ts";

const typeMap: Record<string, AllowedTypeValueObject> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
autogeneratedComment,
getFunctionName,
renderFunctionImport,
} from "./template_utils.ts";
} from "./utils.ts";
import { FunctionRecord } from "../types.ts";

export const SlackFunctionModTemplate = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getSlackCallbackId,
renderFunctionImport,
renderTypeImports,
} from "./template_utils.ts";
} from "./utils.ts";
import { FunctionParameter, FunctionRecord } from "../types.ts";
import { manifestFunctionFieldsToTypeScript } from "./template_function.ts";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
renderFunctionImport,
renderTypeImports,
sanitize,
} from "../template_utils.ts";
} from "./utils.ts";
import {
assertEquals,
assertStringIncludes,
} from "../../../../../../../dev_deps.ts";
import { FunctionRecord } from "../../types.ts";
import SchemaTypes from "../../../../../../schema_types.ts";
import SlackTypes from "../../../../../schema_types.ts";
import { InternalSlackTypes } from "../../../../../types/custom/mod.ts";
} from "../../../../../../dev_deps.ts";
import { FunctionRecord } from "../types.ts";
import SchemaTypes from "../../../../../schema_types.ts";
import SlackTypes from "../../../../schema_types.ts";
import { InternalSlackTypes } from "../../../../types/custom/mod.ts";

const DESCRIPTION = "Test the Slack function template";
const TITLE = "test function";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
isObjectFunctionProperty,
redText,
yellowText,
} from "../utils.ts";
import { assert, assertEquals, IsExact } from "../../../../../../dev_deps.ts";
} from "./utils.ts";
import { assert, assertEquals, IsExact } from "../../../../../dev_deps.ts";
import {
ArrayFunctionProperty,
FunctionProperty,
ObjectFunctionProperty,
} from "../types.ts";
} from "./types.ts";

Deno.test("colored text remain consistent", () => {
assertEquals("\x1b[92mtest\x1b[0m", greenText("test"));
Expand Down
39 changes: 28 additions & 11 deletions src/schema/slack/functions/_scripts/src/write_function_files.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import SlackFunctionTemplate from "./templates/template_function.ts";
import SlackTestFunctionTemplate from "./templates/test_template.ts";
import SlackFunctionModTemplate from "./templates/template_mod.ts";
import {
SlackFunctionModTemplate,
SlackFunctionTemplate,
SlackTestFunctionTemplate,
} from "./templates/mod.ts";
import { getSlackFunctions, greenText, redText } from "./utils.ts";
import { FunctionRecord } from "./types.ts";

const VALID_FILENAME_REGEX = /^[0-9a-zA-Z_\-]+$/;

async function main() {
const slackFunctions: FunctionRecord[] = await getSlackFunctions();
const slackFunctions: FunctionRecord[] = await _internals.getSlackFunctions();

// Sorting alphabetically cause only a monster would generate these in a random order
slackFunctions.sort((a, b) => a.callback_id.localeCompare(b.callback_id));
Expand All @@ -30,26 +32,41 @@ async function main() {
const filename = `../${functionRecord.callback_id}.ts`;
const testFilename = `../${functionRecord.callback_id}_test.ts`;

const templateString = SlackFunctionTemplate(functionRecord);
const templateTestString = SlackTestFunctionTemplate(
const templateString = _internals.SlackFunctionTemplate(functionRecord);
const templateTestString = _internals.SlackTestFunctionTemplate(
functionRecord,
);

await Deno.writeTextFile(filename, templateString);
await Deno.writeTextFile(testFilename, templateTestString);
await _internals.writeTextFile(filename, templateString);
await _internals.writeTextFile(testFilename, templateTestString);
}),
);

console.log(
`Generated ${slackFunctions.length} Slack functions with their unit tests`,
);

const modString = SlackFunctionModTemplate(slackFunctions);
const modString = _internals.SlackFunctionModTemplate(slackFunctions);

await Deno.writeTextFile("../mod.ts", modString);
await _internals.writeTextFile("../mod.ts", modString);
console.log("Updated functions module export");
}

export const _internals = {
main,
getSlackFunctions,
SlackFunctionModTemplate,
SlackFunctionTemplate,
SlackTestFunctionTemplate,
writeTextFile: (
path: string,
data: string,
options?: Deno.WriteFileOptions,
): ReturnType<typeof Deno.writeTextFile> => {
return Deno.writeTextFile(path, data, options);
},
};

if (import.meta.main) {
main();
_internals.main();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
assertEquals,
assertExists,
mock,
} from "../../../../../../src/dev_deps.ts";
import { _internals } from "../../../../../../src/schema/slack/functions/_scripts/src/write_function_files.ts";
import { getSlackFunctions } from "../../../../../../src/schema/slack/functions/_scripts/src/utils.ts";

Deno.test(`write_function_files.ts ${_internals.main.name} function generates valid typescript`, async () => {
const getSlackFunctionsStub = mock.stub(
_internals,
"getSlackFunctions",
(
functionsPayloadPath =
"src/schema/slack/functions/_scripts/src/test/data/function.json",
) => {
return getSlackFunctions(
functionsPayloadPath,
);
},
);

const outputs: Record<string, string> = {};

const writeTextFileStub = mock.stub(
_internals,
"writeTextFile",
// deno-lint-ignore require-await
async (path, data) => {
outputs[path] = data;
},
);

try {
await _internals.main();
mock.assertSpyCalls(writeTextFileStub, 3);
mock.assertSpyCalls(getSlackFunctionsStub, 1);

const generatedContent = outputs["../send_message.ts"];
assertExists(generatedContent);

const command = new Deno.Command(Deno.execPath(), {
cwd: `${Deno.cwd()}/src/schema/slack/functions/`,
args: [
"eval",
generatedContent,
],
});
const { code, stdout, stderr } = await command.output();
const textDecoder = new TextDecoder();

assertEquals(
textDecoder.decode(stderr),
"",
"The generated TypeScript content is not valid",
);
assertEquals(
textDecoder.decode(stdout),
"",
"The generated TypeScript should not print to the console",
);
assertEquals(code, 0);
} finally {
writeTextFileStub.restore();
getSlackFunctionsStub.restore();
}
});

0 comments on commit 94858bf

Please sign in to comment.