eleventy-test 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +56 -0
- package/dist/ScenarioOutput.d.ts +12 -0
- package/dist/eleventyUtils.d.ts +9 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +199 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Denperidge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# eleventy-test
|
|
2
|
+
Eleventy is hands-down my favourite static site generator. I also make [plugins for it](https://github.com/search?q=owner%3ADenperidge%20topic%3Aeleventy-plugin&type=repositories)! So [after a while of brute forcing my Eleventy testing](https://github.com/Denperidge/eleventy-auto-cache-buster/tree/14787bebe3bb73f4c6bd971196f3bec87812044f/tests), I thought an easier solution should exist. Hopefully this is that!
|
|
3
|
+
- Build using multiple Eleventy versions & configurations for your tests (called scenarios) seperately. Returns the file contents per scenario output for easy access during testing.
|
|
4
|
+
- Compatible with any test framework or method.
|
|
5
|
+
- Automatic installation of latest/specific Eleventy version (if not found locally).
|
|
6
|
+
- Aside from the Eleventy versions used to test, this package itself has **zero** dependencies when installing.
|
|
7
|
+
|
|
8
|
+
Want to see how it is in action? For the dogfooding fans, you can see this library in action in this [library's tests](tests/)!
|
|
9
|
+
|
|
10
|
+
## How-to
|
|
11
|
+
### Use the plugin
|
|
12
|
+
1. Install the plugin
|
|
13
|
+
```bash
|
|
14
|
+
npm install --save-dev eleventy-test
|
|
15
|
+
yarn add -D eleventy-test
|
|
16
|
+
```
|
|
17
|
+
2. Create `${projectRoot}/tests/scenarios/`
|
|
18
|
+
3. Create a subdirectory for each scenario you wish to set up. The directory name should start with the *exact* (1.0.2) or *major* (1) version. The format should be one of the following:
|
|
19
|
+
- `${projectRoot}/tests/scenarios/${eleventyVersion}--${label}/`
|
|
20
|
+
- `${projectRoot}/tests/scenarios/${eleventyVersion}/`
|
|
21
|
+
3. Add an Eleventy configuration file to the scenario
|
|
22
|
+
4. Use the buildScenarios function and its output as needed
|
|
23
|
+
```js
|
|
24
|
+
import { buildScenarios } from "eleventy-test";
|
|
25
|
+
import test from "ava";
|
|
26
|
+
|
|
27
|
+
// omit the second parameter to return as array
|
|
28
|
+
const resultsAsDict = await buildScenarios(cwd(), false);
|
|
29
|
+
|
|
30
|
+
test("Check if index.html is consistent across builds", t => {
|
|
31
|
+
t.deepEqual(
|
|
32
|
+
results["3--example"].getFileContent("/index.html"),
|
|
33
|
+
results["3.0.0--identical-example"].getFileContent("/index.html"));
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
And that's it! eleventy-test will handle installing the right versons and reading (and caching) the file contents back to you.
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
> Note: you might want to add `eleventy-test-out/` to your .gitignore file!
|
|
41
|
+
|
|
42
|
+
### Run/develop/test locally
|
|
43
|
+
This require Node.js & yarn to be installed.
|
|
44
|
+
```bash
|
|
45
|
+
git clone https://github.com/Denperidge/eleventy-test
|
|
46
|
+
cd eleventy-test
|
|
47
|
+
yarn install
|
|
48
|
+
|
|
49
|
+
yarn watch # Watch for changes
|
|
50
|
+
yarn build # Build
|
|
51
|
+
yarn start # Run built js as module (see bottom of index.ts require.main === module)
|
|
52
|
+
yarn test # Run the tests from tests/test.mjs
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function recursiveFindFiles(dir: string, files?: string[]): string[];
|
|
2
|
+
export default class ScenarioOutput {
|
|
3
|
+
eleventyOutputDir: string;
|
|
4
|
+
title: string;
|
|
5
|
+
private _files;
|
|
6
|
+
private cache;
|
|
7
|
+
constructor(pEleventyOutputDir: string, pTitle: string);
|
|
8
|
+
get files(): {
|
|
9
|
+
[key: string]: () => string;
|
|
10
|
+
};
|
|
11
|
+
getFileContent(filename: any): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import ScenarioOutput from "./ScenarioOutput";
|
|
2
|
+
export declare function ensureEleventyExists(projectRoot: string, eleventyVersion: string): string;
|
|
3
|
+
export declare function buildEleventy({ eleventyVersion, scenarioDir, scenarioName, projectRoot, globalInputDir }: {
|
|
4
|
+
eleventyVersion: any;
|
|
5
|
+
scenarioDir: any;
|
|
6
|
+
scenarioName: any;
|
|
7
|
+
projectRoot?: string;
|
|
8
|
+
globalInputDir: any;
|
|
9
|
+
}): Promise<ScenarioOutput>;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import ScenarioOutput from "./ScenarioOutput";
|
|
2
|
+
export * from "./eleventyUtils";
|
|
3
|
+
export declare function buildScenarios(projectRoot: string, returnArray?: true): Promise<ScenarioOutput[]>;
|
|
4
|
+
export declare function buildScenarios(projectRoot: string, returnArray?: false): Promise<{
|
|
5
|
+
[key: string]: ScenarioOutput;
|
|
6
|
+
}>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var src_exports = {};
|
|
21
|
+
__export(src_exports, {
|
|
22
|
+
buildEleventy: () => buildEleventy,
|
|
23
|
+
buildScenarios: () => buildScenarios,
|
|
24
|
+
ensureEleventyExists: () => ensureEleventyExists
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
var import_process2 = require("process");
|
|
28
|
+
var import_path3 = require("path");
|
|
29
|
+
var import_fs3 = require("fs");
|
|
30
|
+
|
|
31
|
+
// src/eleventyUtils.ts
|
|
32
|
+
var import_child_process = require("child_process");
|
|
33
|
+
var import_fs2 = require("fs");
|
|
34
|
+
var import_path2 = require("path");
|
|
35
|
+
var import_process = require("process");
|
|
36
|
+
|
|
37
|
+
// src/ScenarioOutput.ts
|
|
38
|
+
var import_path = require("path");
|
|
39
|
+
var import_fs = require("fs");
|
|
40
|
+
function recursiveFindFiles(dir, files = []) {
|
|
41
|
+
const foundDirs = [];
|
|
42
|
+
(0, import_fs.readdirSync)(dir).forEach((name) => {
|
|
43
|
+
const path = (0, import_path.join)(dir, name);
|
|
44
|
+
const stat = (0, import_fs.lstatSync)(path);
|
|
45
|
+
if (stat.isDirectory()) {
|
|
46
|
+
foundDirs.push(path);
|
|
47
|
+
} else if (stat.isFile()) {
|
|
48
|
+
files.push(path);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
foundDirs.forEach((dir2) => {
|
|
52
|
+
files = recursiveFindFiles(dir2, files);
|
|
53
|
+
});
|
|
54
|
+
return files;
|
|
55
|
+
}
|
|
56
|
+
var ScenarioOutput = class {
|
|
57
|
+
eleventyOutputDir;
|
|
58
|
+
title;
|
|
59
|
+
_files;
|
|
60
|
+
cache;
|
|
61
|
+
constructor(pEleventyOutputDir, pTitle) {
|
|
62
|
+
this._files = {};
|
|
63
|
+
this.cache = {};
|
|
64
|
+
this.title = pTitle;
|
|
65
|
+
this.eleventyOutputDir = pEleventyOutputDir;
|
|
66
|
+
recursiveFindFiles(this.eleventyOutputDir).forEach((filepath) => {
|
|
67
|
+
this._files[filepath.replace(this.eleventyOutputDir, "")] = function() {
|
|
68
|
+
return (0, import_fs.readFileSync)(filepath, { encoding: "utf-8" });
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
get files() {
|
|
73
|
+
return this._files;
|
|
74
|
+
}
|
|
75
|
+
getFileContent(filename) {
|
|
76
|
+
if (!Object.keys(this.cache).includes(filename)) {
|
|
77
|
+
this.cache[filename] = this._files[filename]();
|
|
78
|
+
}
|
|
79
|
+
return this.cache[filename];
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/eleventyUtils.ts
|
|
84
|
+
function ensureEleventyExists(projectRoot, eleventyVersion) {
|
|
85
|
+
const eleventyDir = (0, import_path2.join)(projectRoot, "node_modules/@11ty/eleventy" + eleventyVersion);
|
|
86
|
+
if ((0, import_fs2.existsSync)(eleventyDir)) {
|
|
87
|
+
return eleventyDir;
|
|
88
|
+
} else {
|
|
89
|
+
console.log("Not existing!", eleventyVersion);
|
|
90
|
+
if ((0, import_fs2.existsSync)((0, import_path2.join)(projectRoot, "package-lock.json"))) {
|
|
91
|
+
throw Error("not implemented");
|
|
92
|
+
} else if ((0, import_fs2.existsSync)((0, import_path2.join)(projectRoot, "yarn.lock"))) {
|
|
93
|
+
try {
|
|
94
|
+
(0, import_child_process.execSync)(`yarn add -D @11ty/eleventy${eleventyVersion}@npm:@11ty/eleventy@${eleventyVersion}`, { cwd: projectRoot });
|
|
95
|
+
} catch (e) {
|
|
96
|
+
console.error(`Couldn't install eleventy ${eleventyVersion} using yarn`);
|
|
97
|
+
throw e;
|
|
98
|
+
}
|
|
99
|
+
return eleventyDir;
|
|
100
|
+
} else {
|
|
101
|
+
throw new Error("Could not determine package manager");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async function buildEleventy({
|
|
106
|
+
eleventyVersion,
|
|
107
|
+
scenarioDir,
|
|
108
|
+
scenarioName,
|
|
109
|
+
projectRoot = (0, import_process.cwd)(),
|
|
110
|
+
globalInputDir
|
|
111
|
+
}) {
|
|
112
|
+
return new Promise((resolve, reject) => {
|
|
113
|
+
const eleventyDir = ensureEleventyExists(projectRoot, eleventyVersion);
|
|
114
|
+
const bin = JSON.parse(
|
|
115
|
+
(0, import_fs2.readFileSync)(
|
|
116
|
+
(0, import_path2.join)(eleventyDir, "package.json"),
|
|
117
|
+
{ encoding: "utf-8" }
|
|
118
|
+
)
|
|
119
|
+
).bin.eleventy;
|
|
120
|
+
const pathToBin = (0, import_path2.join)(eleventyDir, bin);
|
|
121
|
+
const scenarioInputDir = (0, import_path2.join)(scenarioDir, "input");
|
|
122
|
+
const inputDir = (0, import_fs2.existsSync)(scenarioInputDir) ? scenarioInputDir : globalInputDir;
|
|
123
|
+
if (inputDir == void 0) {
|
|
124
|
+
throw Error("inputDir is undefined!");
|
|
125
|
+
}
|
|
126
|
+
const outputDir = (0, import_path2.join)(scenarioDir, "eleventy-test-out");
|
|
127
|
+
(0, import_fs2.rmSync)(outputDir, { force: true, recursive: true });
|
|
128
|
+
try {
|
|
129
|
+
const out = (0, import_child_process.fork)(
|
|
130
|
+
pathToBin,
|
|
131
|
+
["--input", inputDir, "--output", outputDir],
|
|
132
|
+
{ cwd: scenarioDir }
|
|
133
|
+
);
|
|
134
|
+
out.on("message", (msg) => {
|
|
135
|
+
console.log(msg);
|
|
136
|
+
});
|
|
137
|
+
out.on("close", (code) => {
|
|
138
|
+
const output = new ScenarioOutput(outputDir, scenarioName);
|
|
139
|
+
resolve(output);
|
|
140
|
+
});
|
|
141
|
+
} catch (e) {
|
|
142
|
+
throw e;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/index.ts
|
|
148
|
+
var DIR_BASE = "tests";
|
|
149
|
+
var DIR_SCENARIOS = (0, import_path3.join)(DIR_BASE, "scenarios");
|
|
150
|
+
var DIR_INPUT = (0, import_path3.join)(DIR_BASE, "input");
|
|
151
|
+
async function buildScenarios(projectRoot = (0, import_process2.cwd)(), returnArray = true) {
|
|
152
|
+
return new Promise(async (resolve, reject) => {
|
|
153
|
+
const scenariosDir = (0, import_path3.join)(projectRoot, DIR_SCENARIOS);
|
|
154
|
+
const globalInputDir = (0, import_fs3.existsSync)((0, import_path3.join)(projectRoot, DIR_INPUT)) ? (0, import_path3.join)(projectRoot, DIR_INPUT) : void 0;
|
|
155
|
+
const scenarioDirs = (0, import_fs3.readdirSync)(scenariosDir);
|
|
156
|
+
const scenarioOutputs = [];
|
|
157
|
+
for (let i = 0; i < scenarioDirs.length; i++) {
|
|
158
|
+
const scenarioDirname = scenarioDirs[i];
|
|
159
|
+
const scenarioDir = (0, import_path3.join)(scenariosDir, scenarioDirname);
|
|
160
|
+
let scenarioEleventyVersion = scenarioDirname.includes("--") ? scenarioDirname.split("--")[0] : scenarioDirname;
|
|
161
|
+
if (scenarioEleventyVersion.length < 5) {
|
|
162
|
+
const scenarioMajorVersion = scenarioDirname[0];
|
|
163
|
+
const versions = await (await fetch("https://api.github.com/repos/11ty/eleventy/tags")).json();
|
|
164
|
+
for (let i2 = 0; i2 < versions.length; i2++) {
|
|
165
|
+
const version = versions[i2];
|
|
166
|
+
if (!version.name.includes("-") && version.name[1] == scenarioMajorVersion) {
|
|
167
|
+
scenarioEleventyVersion = version.name.substring(1);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
scenarioOutputs.push(await buildEleventy({
|
|
173
|
+
eleventyVersion: scenarioEleventyVersion,
|
|
174
|
+
scenarioName: scenarioDirname,
|
|
175
|
+
globalInputDir,
|
|
176
|
+
projectRoot,
|
|
177
|
+
scenarioDir
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
if (returnArray) {
|
|
181
|
+
resolve(scenarioOutputs);
|
|
182
|
+
} else {
|
|
183
|
+
const returnDict = {};
|
|
184
|
+
scenarioOutputs.forEach((scenarioOutput) => {
|
|
185
|
+
returnDict[scenarioOutput.title] = scenarioOutput;
|
|
186
|
+
});
|
|
187
|
+
resolve(returnDict);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (require.main === module) {
|
|
192
|
+
buildScenarios((0, import_process2.cwd)());
|
|
193
|
+
}
|
|
194
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
195
|
+
0 && (module.exports = {
|
|
196
|
+
buildEleventy,
|
|
197
|
+
buildScenarios,
|
|
198
|
+
ensureEleventyExists
|
|
199
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eleventy-test",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Multi-configuration testing for Eleventy plugins",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "https://github.com/Denperidge/eleventy-test.git",
|
|
7
|
+
"author": "Denperidge",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"files": ["dist/*"],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node dist/index.js",
|
|
12
|
+
"watch": "npm-run-all --parallel 'build:js --watch' 'build:types -w'",
|
|
13
|
+
"build": "yarn build:js && yarn build:types",
|
|
14
|
+
"build:js": "esbuild src/index.ts --bundle --outfile=dist/index.js --platform=node",
|
|
15
|
+
"build:types": "tsc --declaration src/index.ts --emitDeclarationOnly --outDir ./dist",
|
|
16
|
+
"test": "ava tests/test.mjs --timeout=30s"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@11ty/eleventy1.0.2": "npm:@11ty/eleventy@1.0.2",
|
|
20
|
+
"@11ty/eleventy2.0.0-canary.8": "npm:@11ty/eleventy@2.0.0-canary.8",
|
|
21
|
+
"@11ty/eleventy2.0.1": "npm:@11ty/eleventy@2.0.1",
|
|
22
|
+
"@11ty/eleventy3.0.0": "npm:@11ty/eleventy@3.0.0",
|
|
23
|
+
"@types/node": "^22.10.1",
|
|
24
|
+
"ava": "^6.2.0",
|
|
25
|
+
"esbuild": "^0.24.0",
|
|
26
|
+
"jsdom": "^25.0.1",
|
|
27
|
+
"npm-run-all": "^4.1.5",
|
|
28
|
+
"typescript": "^5.7.2"
|
|
29
|
+
}
|
|
30
|
+
}
|