Skip to content

Commit

Permalink
test: test test-negative workflow folder
Browse files Browse the repository at this point in the history
Previously, only the `test-positive` workflow folder
was tested.

Additionally, when an `expectError` test fails,
we log: `✅ compiling ${file} produced an error, which is well`,
as otherwise it can be a bit confusing to view the logs.
  • Loading branch information
aloisklink authored and MindaugasLaganeckas committed Jun 27, 2022
1 parent 9ae5cb2 commit 985ce30
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ fontawesome/
.npmrc
yarn-error.log
test-positive/*.out.md
test-output/

58 changes: 33 additions & 25 deletions src-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ const { execSync, spawnSync } = require("child_process");
// Joins together directory/file names in a OS independent way
const {join} = require("path");

const workflows = "test-positive";
const out = "test-positive";
const workflows = ["test-positive", "test-negative"];
const out = "test-output";

/**
* Process workflow from stdin into specified format file
*/
async function compileDiagramFromStdin(file, format) {
async function compileDiagramFromStdin(workflow, file, format) {
return new Promise(function (resolve, reject) {
try {
const result = file.replace(/\.(?:mmd|md)$/, "-stdin." + format);
// eslint-disable-next-line no-console
console.warn(`Compiling ${file} into ${result}`);
execSync(`cat ${workflows}/${file} | \
node index.bundle.js -o ${out}/${result} -c ${workflows}/config.json`, {
execSync(`cat ${workflow}/${file} | \
node index.bundle.js -o ${out}/${result} -c ${workflow}/config.json`, {
"encoding": "utf8", // execSync has buffer as default, unlike exec
});

Expand All @@ -34,15 +34,16 @@ async function compileDiagramFromStdin(file, format) {

/**
* Process workflow into specified format file
*
*
* @param {string} workflow - Workflow folder.
* @param {string} file - Name of mermaid input file relative to workflow folder.
* @param {"svg" | "pdf" | "png"} format - Format of output file.
* @param {Object} [options] - Optional options.
* @param {string} [options.puppeteerConfigFile] - If set, puppeteerConfigFile.
* Must be relative to workflow folder.
* @throws {Error} if mmdc fails to launch, or if it has exitCode != 0
*/
async function compileDiagram(file, format, {puppeteerConfigFile} = {}) {
async function compileDiagram(workflow, file, format, {puppeteerConfigFile} = {}) {
return new Promise(function(resolve, reject) {
const result = file.replace(/\.(?:mmd|md)$/, "." + format);
// eslint-disable-next-line no-console
Expand All @@ -51,17 +52,17 @@ async function compileDiagram(file, format, {puppeteerConfigFile} = {}) {
const args = [
"index.bundle.js",
"-i",
workflows + "/" + file,
join(workflow, file),
"-o",
out + "/" + result,
"-c",
workflows + "/config.json",
join(workflow, "config.json"),
"-b",
"lightgray"
];

if (puppeteerConfigFile) {
args.push("--puppeteerConfigFile", join(workflows, puppeteerConfigFile));
args.push("--puppeteerConfigFile", join(workflow, puppeteerConfigFile));
}

const child = spawnSync("node", args, { timeout: 5000 });
Expand Down Expand Up @@ -89,29 +90,32 @@ async function compileDiagram(file, format, {puppeteerConfigFile} = {}) {
async function compileAll() {
await fs.mkdir(out, { recursive: true });

const files = await fs.readdir(workflows);
await Promise.all(
await Promise.all(workflows.map(async(workflow) => {
const files = await fs.readdir(workflow);
await Promise.all(
files.map(async file => {
if (!(file.endsWith(".mmd") | /\.md$/.test(file)) && file !== 'markdown-output.out.md') {
return Promise.resolve();
}
let resultP;
if (file === 'markdown-output.md') {
resultP = compileDiagram(file, "out.md");
resultP = compileDiagram(workflow, file, "out.md");
} else {
resultP = compileDiagram(file, "svg")
.then(() => compileDiagram(file, "png"));
resultP = compileDiagram(workflow, file, "svg")
.then(() => compileDiagram(workflow, file, "png"));
}
const expectError = /expect-error/.test(file);
if (!expectError) return resultP;
try {
await resultP;
} catch (err) {
return `compiling ${file} produced an error, which is well`;
console.log(`✅ compiling ${file} produced an error, which is well`);
return;
}
throw new Error(`Expected ${file} to fail, but it succeeded`);
})
)
)
}))
}

/**
Expand All @@ -120,32 +124,36 @@ async function compileAll() {
async function compileAllStdin() {
await fs.mkdir(out, { recursive: true });

const files = await fs.readdir(workflows);
await Promise.all(
await Promise.all(workflows.map(async(workflow) => {
const files = await fs.readdir(workflow);
await Promise.all(
files.map(async file => {
// currently, piping markdown through stdin is not supported
// as mermaid-cli has no idea it's markdown, not mermaid code
if (!(file.endsWith(".mmd"))) {
return `Skipping ${file}, as it does not end with .mmd`;
}
const expectError = /expect-error/.test(file);
const resultP = compileDiagramFromStdin(file, "svg")
.then(() => compileDiagramFromStdin(file, "png"));
const resultP = compileDiagramFromStdin(workflow, file, "svg")
.then(() => compileDiagramFromStdin(workflow, file, "png"));
if (!expectError) return resultP;
try {
await resultP;
} catch (err) {
return `compling ${file} from stdin produced an error, which is well`;
console.log(`✅ compiling ${file} from stdin produced an error, which is well`);
return;
}
throw new Error(`Expected ${file} from stdin to fail, but it succeeded`);
})
)
)
}))
}

async function shouldErrorOnFailure() {
await compileDiagram("sequence.mmd", "svg"); // should work with default puppeteerConfigFile
await fs.mkdir(out, { recursive: true });
await compileDiagram("test-positive", "sequence.mmd", "svg"); // should work with default puppeteerConfigFile
try {
await compileDiagram("sequence.mmd", "svg", {puppeteerConfigFile: "../test-negative/puppeteerTimeoutConfig.json"});
await compileDiagram("test-positive", "sequence.mmd", "svg", {puppeteerConfigFile: "../test-negative/puppeteerTimeoutConfig.json"});
} catch (error) {
console.log(`compiling with invalid puppeteerConfigFile file produced an error, which is well`);
return;
Expand Down

0 comments on commit 985ce30

Please sign in to comment.