Skip to content

Commit 02b6416

Browse files
committed
fix: add more testing
1 parent ca6ec4f commit 02b6416

File tree

4 files changed

+132
-10
lines changed

4 files changed

+132
-10
lines changed

Diff for: src/resolver.js

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export async function resolve(...paths) {
1212
// seems to be an URL, fetch it
1313
const resource = last;
1414
const response = await fetch(resource);
15+
if (response.status >= 400) {
16+
throw new Error('Not Found');
17+
}
1518
const contentType = response.headers.get('Content-Type');
1619
if (!contentType || !contentType.startsWith('text')) {
1720
return await response.buffer();

Diff for: src/transforms/template-data.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ export function template(str) {
8888
*/
8989
export async function handleTemplateFile(config, data, inputFile) {
9090
const content = await (config.resolve || resolve)(config.dir.input, inputFile);
91+
if (typeof content === "undefined") {
92+
throw new Error('Not Found');
93+
}
9194
if (content === null) {
9295
return null;
93-
}
94-
96+
}
9597
const parsed = path.parse(inputFile);
9698
const ext = parsed.ext?.slice(1);
9799
if (! config.extensions.has(ext)) {
@@ -120,7 +122,11 @@ export async function handleTemplateFile(config, data, inputFile) {
120122
const layoutFilePath = path.normalize(path.join(config.dir.layouts, fileData.layout));
121123
const l = await handleTemplateFile(config,
122124
{...fileData, content: fileContent, layout: null}, layoutFilePath);
123-
fileContent = l.content;
125+
if (l) {
126+
fileContent = l.content;;
127+
} else {
128+
throw new Error('Layout not found:' + layoutFilePath);
129+
}
124130
}
125131

126132
return {content: fileContent, filename: outputFile};

Diff for: tests/resolver.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('resolve', () => {
3131
headers.set('Content-Type', 'text/css');
3232

3333
return ({
34+
status: 200,
3435
headers,
3536
async text() {return ':where(html){}'}
3637
})
@@ -43,4 +44,22 @@ describe('resolve', () => {
4344
globalThis.fetch = originalFetch;
4445
});
4546

47+
it('should not fetch stuff from the internet when the status code is an error', () => {
48+
const originalFetch = globalThis.fetch;
49+
globalThis.fetch = mock.fn(async () => {
50+
const headers = new Map();
51+
headers.set('Content-Type', 'text/html');
52+
return ({
53+
status: 404,
54+
headers,
55+
async text() {return 'Not Found'}
56+
})
57+
});
58+
59+
assert.rejects(async () => {
60+
await resolve(config.dir.input, 'https://not-found.io/404.html');
61+
});
62+
globalThis.fetch = originalFetch;
63+
});
64+
4665
});

Diff for: tests/transforms/template-data.test.js

+101-7
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ const TEST_DATA = {
2121
}
2222
}
2323

24-
const TEST_MD = `---
25-
layout: base.html
26-
author: Lea Rosema
27-
---
28-
# {{ title }}
24+
const TEST_MD = `# {{ title }}
2925
3026
An article by {{ author }}
3127
`
@@ -112,13 +108,72 @@ describe('template function', () => {
112108
});
113109
});
114110

115-
describe('handleTemplateFile function', () => {
111+
describe('handleTemplateFile function', {only: true}, () => {
112+
113+
const withFrontmatter = (str, data) => `---json\n${JSON.stringify(data)}\n---\n${str}`
114+
115+
it('should work with basic html files without specifying a layout', async () => {
116+
const config = new SissiConfig();
117+
config.addExtension(md);
118+
119+
const vFS = new Map();
120+
vFS.set('index.html', '<h1>{{ title }}</h1>');
121+
122+
config.resolve = (...paths) => {
123+
const resource = path.normalize(path.join(...paths));
124+
return vFS.get(resource);
125+
}
126+
127+
const result = await handleTemplateFile(config, {title: 'Lea was here'}, 'index.html');
128+
129+
assert.equal(result.filename, 'public/index.html');
130+
assert.equal(result.content, '<h1>Lea was here</h1>')
131+
});
132+
133+
it('should work with basic html files with specifying a layout', async () => {
134+
const config = new SissiConfig();
135+
config.addExtension(md);
136+
137+
const vFS = new Map();
138+
vFS.set('index.html', withFrontmatter('<h1>{{ title }}</h1>', {layout: 'base.html'}));
139+
vFS.set('_layouts/base.html', '<body>{{ content }}</body>')
140+
141+
config.resolve = (...paths) => {
142+
const resource = path.normalize(path.join(...paths));
143+
return vFS.get(resource);
144+
}
145+
146+
const result = await handleTemplateFile(config, {title: 'Lea was here'}, 'index.html');
147+
148+
assert.equal(result.filename, 'public/index.html');
149+
assert.equal(result.content, '<body><h1>Lea was here</h1></body>');
150+
});
151+
116152
it('should work with the default markdown plugin', async () => {
117153
const config = new SissiConfig();
118154
config.addExtension(md);
119155

120156
const vFS = new Map();
121-
vFS.set('index.md', TEST_MD);
157+
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'base.html', author: 'Lea Rosema'}));
158+
vFS.set('_layouts/base.html', '<body>{{ content }}</body>');
159+
160+
config.resolve = (...paths) => {
161+
const resource = path.normalize(path.join(...paths));
162+
return vFS.get(resource);
163+
}
164+
165+
const result = await handleTemplateFile(config, {title: 'Lea was here'}, 'index.md');
166+
167+
assert.equal(result.filename, 'public/index.html');
168+
assert.equal(result.content, '<body><h1>Lea was here</h1>\n\n<p>An article by Lea Rosema</p>\n</body>')
169+
});
170+
171+
it('should work with the default markdown plugin.', async () => {
172+
const config = new SissiConfig();
173+
config.addExtension(md);
174+
175+
const vFS = new Map();
176+
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'base.html', author: 'Lea Rosema'}));
122177
vFS.set('_layouts/base.html', '<body>{{ content }}</body>');
123178

124179
config.resolve = (...paths) => {
@@ -131,4 +186,43 @@ describe('handleTemplateFile function', () => {
131186
assert.equal(result.filename, 'public/index.html');
132187
assert.equal(result.content, '<body><h1>Lea was here</h1>\n\n<p>An article by Lea Rosema</p>\n</body>')
133188
});
189+
190+
it('should throw an error when a non-existant file is specified', async () => {
191+
const config = new SissiConfig();
192+
config.addExtension(md);
193+
194+
const vFS = new Map();
195+
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'notfound.html', author: 'Lea Rosema'}));
196+
197+
config.resolve = (...paths) => {
198+
const resource = path.normalize(path.join(...paths));
199+
if (vFS.has(resource)) {
200+
return vFS.get(resource);
201+
}
202+
}
203+
204+
assert.rejects(async () => {
205+
await handleTemplateFile(config, {title: 'Lea was here'}, 'something-completely-different.md');
206+
});
207+
});
208+
209+
it('should throw an error when a non-existant file is specified as layout', {only: true}, async () => {
210+
const config = new SissiConfig();
211+
config.addExtension(md);
212+
213+
const vFS = new Map();
214+
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'notfound.html', author: 'Lea Rosema'}));
215+
216+
config.resolve = (...paths) => {
217+
const resource = path.normalize(path.join(...paths));
218+
if (vFS.has(resource)) {
219+
return vFS.get(resource);
220+
}
221+
}
222+
223+
assert.rejects(async () => {
224+
await handleTemplateFile(config, {title: 'Lea was here'}, 'index.md');
225+
});
226+
});
227+
134228
});

0 commit comments

Comments
 (0)