Skip to content

Commit

Permalink
add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
pddg committed Mar 10, 2024
1 parent aa34360 commit 6162e26
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ jobs:
run: npm run build
- name: Test package
run: npm run test
env:
E2E: true
- name: Lint package
run: npm run lint
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ npm install textlint-plugin-latex2e
And add to `.textlintrc`

```
plugins: ["latex2e"]
plugins: ["@textlint/textlint-plugin-latex2e"]
```

## Limitations
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"keywords": [
"textlint",
"textlintplugin",
"latex"
],
"author": "TANIGUCHI Masaya",
Expand Down
100 changes: 100 additions & 0 deletions test/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import child_process from "child_process";
import fs from "fs";
import os from "os";
import path from "path";
import { suite, expect, test } from "vitest";

// These tests requires "E2E=true" environment variable to run and `npm` command should be callable globally.
suite.concurrent("e2e", () => {
["commonjs", "module"].forEach((moduleType) => {
runE2ETestFor(moduleType, async ({ workDir }) => {
const execCommand = (
command: string,
args: string[],
): child_process.SpawnSyncReturns<Buffer> => {
return child_process.spawnSync(command, args, {
cwd: workDir,
stdio: "ignore",
});
};
const npmInstall = execCommand("npm", ["install"]);
expect(npmInstall.status).toBe(0);
const textlintNoErr = execCommand("npx", ["textlint", "no-error.tex"]);
expect(textlintNoErr.status).toBe(0);
const textlintErr = execCommand("npx", ["textlint", "error.tex"]);
expect(textlintErr.status).toBe(1);
const textlintNoErrByComment = execCommand("npx", [
"textlint",
"no-error-by-comment.tex",
]);
expect(textlintNoErrByComment.status).toBe(0);
});
});
});

interface E2EContext {
workDir: string;
}

// runE2ETestFor is a test runner for end-to-end tests.
// This will prepare test environment and clean up after the test.
// This will also skip the test if "E2E" environment variable is not set to "true".
const runE2ETestFor = test.extend<E2EContext>({
workDir: async ({ task, skip }, use) => {
const e2e = process.env.E2E;
if (!e2e && e2e !== "true") {
skip();
}
// Task name should be a module type.
const workDir = prepare(task.name);
try {
await use(workDir);
} finally {
if (fs.existsSync(workDir)) {
fs.rmSync(workDir, { recursive: true });
}
}
},
});

// prepare will create temporary directory and write files for testing.
// This returns the path of the temporary directory.
// moduleType should be "commonjs" or "module". It will be used for package.json.
// Deleting the temporary directory is the responsibility of the caller.
const prepare = (moduleType: string): string => {
const templateDir = path.join(__dirname, "templates");

const packageRoot = path.dirname(__dirname);
const rootPackageJson = fs.readFileSync(
path.join(packageRoot, "package.json"),
"utf-8",
);
const textlintVersion =
JSON.parse(rootPackageJson)["devDependencies"]["@textlint/kernel"];

const readFromTmplDir = (filename: string) =>
fs.readFileSync(path.join(templateDir, filename), "utf-8");

const packageJsonTmpl = readFromTmplDir("package.json.tmpl");
const packageJson = packageJsonTmpl
.replace(/%PACKAGE_TYPE%/g, moduleType)
.replace(/%TEXTLINT_VERSION%/g, textlintVersion)
.replace(/%TEXTLINT_PLUGIN_LATEX2E_LOCATION%/g, `file://${packageRoot}`);
const textlintrcTmpl = readFromTmplDir(".textlintrc.tmpl");
const noErrorTexFile = readFromTmplDir("no-error.tex.tmpl");
const noErrorByCommentTexFile = readFromTmplDir(
"no-error-by-comment.tex.tmpl",
);
const errorTexFile = readFromTmplDir("error.tex.tmpl");

const tmpDir = fs.mkdtempSync(os.tmpdir());

Check failure on line 90 in test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

test/e2e.test.ts > e2e > commonjs

Error: EACCES: permission denied, mkdtemp '/tmpXXXXXX' ❯ Object.mkdtempSync node:fs:2932:3 ❯ prepare test/e2e.test.ts:90:21 ❯ workDir test/e2e.test.ts:49:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -13, syscall: 'mkdtemp', code: 'EACCES', path: '/tmpXXXXXX' }

Check failure on line 90 in test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

test/e2e.test.ts > e2e > module

Error: EACCES: permission denied, mkdtemp '/tmpXXXXXX' ❯ Object.mkdtempSync node:fs:2932:3 ❯ prepare test/e2e.test.ts:90:21 ❯ workDir test/e2e.test.ts:49:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -13, syscall: 'mkdtemp', code: 'EACCES', path: '/tmpXXXXXX' }

Check failure on line 90 in test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

test/e2e.test.ts > e2e > commonjs

Error: EACCES: permission denied, mkdtemp '/tmpXXXXXX' ❯ Object.mkdtempSync node:fs:2986:18 ❯ prepare test/e2e.test.ts:90:21 ❯ workDir test/e2e.test.ts:49:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -13, code: 'EACCES', syscall: 'mkdtemp', path: '/tmpXXXXXX' }

Check failure on line 90 in test/e2e.test.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

test/e2e.test.ts > e2e > module

Error: EACCES: permission denied, mkdtemp '/tmpXXXXXX' ❯ Object.mkdtempSync node:fs:2986:18 ❯ prepare test/e2e.test.ts:90:21 ❯ workDir test/e2e.test.ts:49:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { errno: -13, code: 'EACCES', syscall: 'mkdtemp', path: '/tmpXXXXXX' }
const writeToTmpDir = (filename: string, content: string): void => {
fs.writeFileSync(path.join(tmpDir, filename), content);
};
writeToTmpDir(".textlintrc", textlintrcTmpl);
writeToTmpDir("no-error.tex", noErrorTexFile);
writeToTmpDir("error.tex", errorTexFile);
writeToTmpDir("no-error-by-comment.tex", noErrorByCommentTexFile);
writeToTmpDir("package.json", packageJson);
return tmpDir;
};
9 changes: 9 additions & 0 deletions test/templates/.textlintrc.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": ["@textlint/textlint-plugin-latex2e"],
"filters": {
"comments": true
},
"rules": {
"no-todo": true
}
}
9 changes: 9 additions & 0 deletions test/templates/error.tex.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass{article}

\begin{document}

\section{intro}

TODO: write something

\end{document}
11 changes: 11 additions & 0 deletions test/templates/no-error-by-comment.tex.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
\documentclass{article}

\begin{document}

\section{intro}

% textlint-disable no-todo
TODO: write something
% textlint-enable no-todo

\end{document}
9 changes: 9 additions & 0 deletions test/templates/no-error.tex.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass{article}

\begin{document}

\section{intro}

This is a sample tex file.

\end{document}
11 changes: 11 additions & 0 deletions test/templates/package.json.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "textlint-plugin-latex2e-integration-test",
"version": "1.0.0",
"type": "%PACKAGE_TYPE%",
"dependencies": {
"textlint": "%TEXTLINT_VERSION%",
"@textlint/textlint-plugin-latex2e": "%TEXTLINT_PLUGIN_LATEX2E_LOCATION%",
"textlint-rule-no-todo": "2.0.1",
"textlint-filter-rule-comments": "1.2.2"
}
}

0 comments on commit 6162e26

Please sign in to comment.