Skip to content

Commit

Permalink
Merge branch 'main' into ditch-ts-project-references
Browse files Browse the repository at this point in the history
  • Loading branch information
theseanl committed Jan 28, 2024
2 parents f9a4ecd + 4811a0b commit d19759d
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions packages/unified-latex-cli/tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,35 @@ console.log = (...args) => {
const exePath = path.resolve(__dirname, "../dist/unified-latex-cli.mjs");
const examplesPath = path.resolve(__dirname, "examples");

async function execCLI(args: string[]) {
return await executeCommand(`node`, [
// package.json points to typescript sources and it is replaced with the build output
// before publishing by scripts/make-package.mjs. In testing, we need to point to the
// build output explicitly by passing the conditions flag.
`-C`,
`prebuilt`,
exePath,
...args,
]);
}

describe(
"unified-latex-cli",
() => {
let stdout: string, stderr: string;
it("executable exists", async () => {
expect(fsLegacy.existsSync(exePath)).toBeTruthy();
});
it("can execute without error", async () => {
let { stdout, stderr } = await exec(`node ${exePath} -h`);
const stdout = await execCLI(["-h"]);
expect(stdout).toBeTruthy();
});
it("can format document", async () => {
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/needs-fixing.tex`
);
const stdout = await execCLI([`${examplesPath}/needs-fixing.tex`]);
expect(stdout).toMatchSnapshot();
});
it("can expand macro", async () => {
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/needs-expanding.tex`,
`-e`,
"\\newcommand{foo}[1]{FOO(#1)}",
Expand All @@ -50,8 +58,7 @@ describe(
}
{
// Make sure we don't lose spaces in math mode
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/needs-expanding.tex`,
`-e`,
"\\newcommand{foo}[1]{$\\x #1$}",
Expand All @@ -62,42 +69,44 @@ describe(
}
});
it("can expand macros defined in document", async () => {
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/has-definition.tex --stats-json`
);
const stdout = await execCLI([
`${examplesPath}/has-definition.tex`,
`--stats-json`,
]);
const { newcommands } = JSON.parse(stdout) as {
newcommands: { name: string }[];
};
const newcommandNames = newcommands.map((c) => c.name);
expect(newcommandNames).toEqual(["foo", "baz"]);

{
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/has-definition.tex --expand-document-macro foo --expand-document-macro baz`
);
const stdout = await execCLI([
`${examplesPath}/has-definition.tex`,
`--expand-document-macro`,
`foo`,
`--expand-document-macro`,
`baz`,
]);
expect(stdout).toMatchSnapshot();
}
});
it("can override default macros", async () => {
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/has-existing-definition.tex`,
]);
expect(stdout).toMatchSnapshot();
}
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/has-existing-definition.tex`,
`-e`,
"\\newcommand{mathbb}{\\mathbb}",
]);
expect(stdout).toMatchSnapshot();
}
{
let stdout = await executeCommand(`node`, [
exePath,
const stdout = await execCLI([
`${examplesPath}/has-existing-definition.tex`,
`-e`,
"\\newcommand{mathbb}[2]{\\mathbb{#1}{#2}}",
Expand All @@ -107,17 +116,19 @@ describe(
});
it("can convert to html", async () => {
{
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/simple.tex --html`
);
const stdout = await execCLI([
`${examplesPath}/simple.tex`,
`--html`,
]);
expect(stdout).toMatchSnapshot();
}
});
it("can convert to markdown", async () => {
{
let { stdout, stderr } = await exec(
`node ${exePath} ${examplesPath}/simple.tex --markdown`
);
const stdout = await execCLI([
`${examplesPath}/simple.tex`,
`--markdown`,
]);
expect(stdout).toMatchSnapshot();
}
});
Expand All @@ -131,7 +142,10 @@ describe(
* Run commands with arguments using "cross-spawn", which correctly escapes arguments
* so that end results are the same across different shells.
*/
async function executeCommand(executablePath: string, args: string[]) {
function executeCommand(
executablePath: string,
args: string[]
): Promise<string> {
return new Promise((resolve, reject) => {
const childProcess = spawn(executablePath, args, { stdio: "pipe" });

Expand Down

0 comments on commit d19759d

Please sign in to comment.