Skip to content

Commit

Permalink
fix: add more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
learosema committed Sep 30, 2024
1 parent ca6ec4f commit 02b6416
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export async function resolve(...paths) {
// seems to be an URL, fetch it
const resource = last;
const response = await fetch(resource);
if (response.status >= 400) {
throw new Error('Not Found');
}
const contentType = response.headers.get('Content-Type');
if (!contentType || !contentType.startsWith('text')) {
return await response.buffer();
Expand Down
12 changes: 9 additions & 3 deletions src/transforms/template-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ export function template(str) {
*/
export async function handleTemplateFile(config, data, inputFile) {
const content = await (config.resolve || resolve)(config.dir.input, inputFile);
if (typeof content === "undefined") {
throw new Error('Not Found');
}
if (content === null) {
return null;
}

}
const parsed = path.parse(inputFile);
const ext = parsed.ext?.slice(1);
if (! config.extensions.has(ext)) {
Expand Down Expand Up @@ -120,7 +122,11 @@ export async function handleTemplateFile(config, data, inputFile) {
const layoutFilePath = path.normalize(path.join(config.dir.layouts, fileData.layout));
const l = await handleTemplateFile(config,
{...fileData, content: fileContent, layout: null}, layoutFilePath);
fileContent = l.content;
if (l) {
fileContent = l.content;;
} else {
throw new Error('Layout not found:' + layoutFilePath);
}
}

return {content: fileContent, filename: outputFile};
Expand Down
19 changes: 19 additions & 0 deletions tests/resolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('resolve', () => {
headers.set('Content-Type', 'text/css');

return ({
status: 200,
headers,
async text() {return ':where(html){}'}
})
Expand All @@ -43,4 +44,22 @@ describe('resolve', () => {
globalThis.fetch = originalFetch;
});

it('should not fetch stuff from the internet when the status code is an error', () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = mock.fn(async () => {
const headers = new Map();
headers.set('Content-Type', 'text/html');
return ({
status: 404,
headers,
async text() {return 'Not Found'}
})
});

assert.rejects(async () => {
await resolve(config.dir.input, 'https://not-found.io/404.html');
});
globalThis.fetch = originalFetch;
});

});
108 changes: 101 additions & 7 deletions tests/transforms/template-data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ const TEST_DATA = {
}
}

const TEST_MD = `---
layout: base.html
author: Lea Rosema
---
# {{ title }}
const TEST_MD = `# {{ title }}
An article by {{ author }}
`
Expand Down Expand Up @@ -112,13 +108,72 @@ describe('template function', () => {
});
});

describe('handleTemplateFile function', () => {
describe('handleTemplateFile function', {only: true}, () => {

const withFrontmatter = (str, data) => `---json\n${JSON.stringify(data)}\n---\n${str}`

it('should work with basic html files without specifying a layout', async () => {
const config = new SissiConfig();
config.addExtension(md);

const vFS = new Map();
vFS.set('index.html', '<h1>{{ title }}</h1>');

config.resolve = (...paths) => {
const resource = path.normalize(path.join(...paths));
return vFS.get(resource);
}

const result = await handleTemplateFile(config, {title: 'Lea was here'}, 'index.html');

assert.equal(result.filename, 'public/index.html');
assert.equal(result.content, '<h1>Lea was here</h1>')
});

it('should work with basic html files with specifying a layout', async () => {
const config = new SissiConfig();
config.addExtension(md);

const vFS = new Map();
vFS.set('index.html', withFrontmatter('<h1>{{ title }}</h1>', {layout: 'base.html'}));
vFS.set('_layouts/base.html', '<body>{{ content }}</body>')

config.resolve = (...paths) => {
const resource = path.normalize(path.join(...paths));
return vFS.get(resource);
}

const result = await handleTemplateFile(config, {title: 'Lea was here'}, 'index.html');

assert.equal(result.filename, 'public/index.html');
assert.equal(result.content, '<body><h1>Lea was here</h1></body>');
});

it('should work with the default markdown plugin', async () => {
const config = new SissiConfig();
config.addExtension(md);

const vFS = new Map();
vFS.set('index.md', TEST_MD);
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'base.html', author: 'Lea Rosema'}));
vFS.set('_layouts/base.html', '<body>{{ content }}</body>');

config.resolve = (...paths) => {
const resource = path.normalize(path.join(...paths));
return vFS.get(resource);
}

const result = await handleTemplateFile(config, {title: 'Lea was here'}, 'index.md');

assert.equal(result.filename, 'public/index.html');
assert.equal(result.content, '<body><h1>Lea was here</h1>\n\n<p>An article by Lea Rosema</p>\n</body>')
});

it('should work with the default markdown plugin.', async () => {
const config = new SissiConfig();
config.addExtension(md);

const vFS = new Map();
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'base.html', author: 'Lea Rosema'}));
vFS.set('_layouts/base.html', '<body>{{ content }}</body>');

config.resolve = (...paths) => {
Expand All @@ -131,4 +186,43 @@ describe('handleTemplateFile function', () => {
assert.equal(result.filename, 'public/index.html');
assert.equal(result.content, '<body><h1>Lea was here</h1>\n\n<p>An article by Lea Rosema</p>\n</body>')
});

it('should throw an error when a non-existant file is specified', async () => {
const config = new SissiConfig();
config.addExtension(md);

const vFS = new Map();
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'notfound.html', author: 'Lea Rosema'}));

config.resolve = (...paths) => {
const resource = path.normalize(path.join(...paths));
if (vFS.has(resource)) {
return vFS.get(resource);
}
}

assert.rejects(async () => {
await handleTemplateFile(config, {title: 'Lea was here'}, 'something-completely-different.md');
});
});

it('should throw an error when a non-existant file is specified as layout', {only: true}, async () => {
const config = new SissiConfig();
config.addExtension(md);

const vFS = new Map();
vFS.set('index.md', withFrontmatter(TEST_MD, {'layout': 'notfound.html', author: 'Lea Rosema'}));

config.resolve = (...paths) => {
const resource = path.normalize(path.join(...paths));
if (vFS.has(resource)) {
return vFS.get(resource);
}
}

assert.rejects(async () => {
await handleTemplateFile(config, {title: 'Lea was here'}, 'index.md');
});
});

});

0 comments on commit 02b6416

Please sign in to comment.