add more tests

This commit is contained in:
Rich Harris
2018-07-15 22:50:53 -04:00
parent bf8e56748a
commit 6c5630f281
46 changed files with 275 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
--require source-map-support/register
--recursive
test/unit/**/*.js
test/unit/*/*.js
test/common/test.js

View File

@@ -63,6 +63,14 @@ export default function create_routes(cwd = locations.routes()) {
.sort(comparator);
items.forEach(item => {
if (item.basename[0] === '_') {
if (item.basename !== (item.is_dir ? '_default' : '_default.html')) return;
}
if (item.basename[0] === '.') {
if (item.file !== '.well-known') return;
}
const segments = parent_segments.slice();
if (item.is_index && segments.length > 0) {
@@ -114,7 +122,22 @@ export default function create_routes(cwd = locations.routes()) {
}
else if (item.basename === 'index.html') {
// TODO if this is a leaf, create a route for it
const is_branch = items.some(other_item => {
if (other_item === item) return false;
if (item.is_dir) {
return fs.existsSync(path.join(dir, item.basename, 'index.html'));
}
return item.is_page;
});
if (!is_branch) {
pages.push({
pattern: get_pattern(parent_segments),
parts: stack
});
}
}
else if (item.is_page) {
@@ -155,6 +178,32 @@ export default function create_routes(cwd = locations.routes()) {
walk(cwd, [], [], []);
// check for clashes
const seen_pages: Map<string, Page> = new Map();
pages.forEach(page => {
const pattern = page.pattern.toString();
if (seen_pages.has(pattern)) {
const file = page.parts.pop().component.file;
const other_page = seen_pages.get(pattern);
const other_file = other_page.parts.pop().component.file;
throw new Error(`The ${other_file} and ${file} pages clash`);
}
seen_pages.set(pattern, page);
});
const seen_routes: Map<string, ServerRoute> = new Map();
server_routes.forEach(route => {
const pattern = route.pattern.toString();
if (seen_routes.has(pattern)) {
const other_route = seen_routes.get(pattern);
throw new Error(`The ${other_route.file} and ${route.file} routes clash`);
}
seen_routes.set(pattern, route);
});
return {
components,
pages,

View File

@@ -0,0 +1,186 @@
import * as path from 'path';
import glob from 'glob';
import { locations } from '../config';
import { Route } from '../interfaces';
export default function create_routes({
files
} = {
files: glob.sync('**/*.*', {
cwd: locations.routes(),
dot: true,
nodir: true
})
}) {
const all_routes = files
.filter((file: string) => !/(^|\/|\\)(_(?!error\.html)|\.(?!well-known))/.test(file))
.map((file: string) => {
if (/]\[/.test(file)) {
throw new Error(`Invalid route ${file} — parameters must be separated`);
}
if (file === '4xx.html' || file === '5xx.html') {
throw new Error('As of Sapper 0.14, 4xx.html and 5xx.html should be replaced with _error.html');
}
const base = file.replace(/\.[^/.]+$/, '');
const parts = base.split('/'); // glob output is always posix-style
if (/^index(\..+)?/.test(parts[parts.length - 1])) {
const part = parts.pop();
if (parts.length > 0) parts[parts.length - 1] += part.slice(5);
}
const id = (
parts.join('_').replace(/[[\]]/g, '$').replace(/^\d/, '_$&').replace(/[^a-zA-Z0-9_$]/g, '_')
) || '_';
const type = file.endsWith('.html') ? 'page' : 'route';
const params: string[] = [];
const match_patterns: Record<string, string> = {};
const param_pattern = /\[([^\(\]]+)(?:\((.+?)\))?\]/g;
let match;
while (match = param_pattern.exec(base)) {
params.push(match[1]);
if (typeof match[2] !== 'undefined') {
if (/[\(\)\?\:]/.exec(match[2])) {
throw new Error('Sapper does not allow (, ), ? or : in RegExp routes yet');
}
// Make a map of the regexp patterns
match_patterns[match[1]] = `(${match[2]}?)`;
}
}
// TODO can we do all this with sub-parts? or does
// nesting make that impossible?
let pattern_string = '';
let i = parts.length;
let nested = true;
while (i--) {
const part = encodeURI(parts[i].normalize()).replace(/\?/g, '%3F').replace(/#/g, '%23').replace(/%5B/g, '[').replace(/%5D/g, ']');
const dynamic = ~part.indexOf('[');
if (dynamic) {
// Get keys from part and replace with stored match patterns
const keys = part.replace(/\(.*?\)/, '').split(/[\[\]]/).filter((x, i) => { if (i % 2) return x });
let matcher = part;
keys.forEach(k => {
const key_pattern = new RegExp('\\[' + k + '(?:\\((.+?)\\))?\\]');
matcher = matcher.replace(key_pattern, match_patterns[k] || `([^/]+?)`);
})
pattern_string = (nested && type === 'page') ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
} else {
nested = false;
pattern_string = `\\/${part}${pattern_string}`;
}
}
const pattern = new RegExp(`^${pattern_string}\\/?$`);
const test = (url: string) => pattern.test(url);
const exec = (url: string) => {
const match = pattern.exec(url);
if (!match) return;
const result: Record<string, string> = {};
params.forEach((param, i) => {
result[param] = match[i + 1];
});
return result;
};
return {
id,
base,
type,
file,
pattern,
test,
exec,
parts,
params
};
});
const pages = all_routes
.filter(r => r.type === 'page')
.sort(comparator);
const server_routes = all_routes
.filter(r => r.type === 'route')
.sort(comparator);
return { pages, server_routes };
}
function comparator(a, b) {
if (a.parts[0] === '_error') return -1;
if (b.parts[0] === '_error') return 1;
const max = Math.max(a.parts.length, b.parts.length);
for (let i = 0; i < max; i += 1) {
const a_part = a.parts[i];
const b_part = b.parts[i];
if (!a_part) return -1;
if (!b_part) return 1;
const a_sub_parts = get_sub_parts(a_part);
const b_sub_parts = get_sub_parts(b_part);
const max = Math.max(a_sub_parts.length, b_sub_parts.length);
for (let i = 0; i < max; i += 1) {
const a_sub_part = a_sub_parts[i];
const b_sub_part = b_sub_parts[i];
if (!a_sub_part) return 1; // b is more specific, so goes first
if (!b_sub_part) return -1;
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
return a_sub_part.dynamic ? 1 : -1;
}
if (!a_sub_part.dynamic && a_sub_part.content !== b_sub_part.content) {
return (
(b_sub_part.content.length - a_sub_part.content.length) ||
(a_sub_part.content < b_sub_part.content ? -1 : 1)
);
}
// If both parts dynamic, check for regexp patterns
if (a_sub_part.dynamic && b_sub_part.dynamic) {
const regexp_pattern = /\((.*?)\)/;
const a_match = regexp_pattern.exec(a_sub_part.content);
const b_match = regexp_pattern.exec(b_sub_part.content);
if (!a_match && b_match) {
return 1; // No regexp, so less specific than b
}
if (!b_match && a_match) {
return -1;
}
if (a_match && b_match && a_match[1] !== b_match[1]) {
return b_match[1].length - a_match[1].length;
}
}
}
}
throw new Error(`The ${a.base} and ${b.base} routes clash`);
}
function get_sub_parts(part: string) {
return part.split(/\[(.+)\]/)
.map((content, i) => {
if (!content) return null;
return {
content,
dynamic: i % 2 === 1
};
})
.filter(Boolean);
}

View File

@@ -333,6 +333,9 @@ function get_page_handler(routes: RouteObject[], store_getter: (req: Req) => Sto
};
Object.assign(props, preloaded);
res.end('TODO');
return;
const { html, head, css } = App.render({ Page: route.handler, props }, {
store
});

View File

@@ -1,10 +1,10 @@
const path = require('path');
const assert = require('assert');
const { create_routes_alt } = require('../../dist/core.ts.js');
const { create_routes } = require('../../../dist/core.ts.js');
describe('create_routes', () => {
describe.only('create_routes', () => {
it('creates routes', () => {
const { components, pages, server_routes } = create_routes_alt(path.join(__dirname, 'samples/basic'));
const { components, pages, server_routes } = create_routes(path.join(__dirname, 'samples/basic'));
const page_index = { name: 'page_index', file: '_default.html' };
const page_about = { name: 'page_about', file: 'about.html' };
@@ -70,7 +70,7 @@ describe('create_routes', () => {
});
it('encodes invalid characters', () => {
const { components, pages } = create_routes_alt(path.join(__dirname, 'samples/encoding'));
const { components, pages } = create_routes(path.join(__dirname, 'samples/encoding'));
const quote = { name: 'page_$34', file: '".html' };
const hash = { name: 'page_$35', file: '#.html' };
@@ -90,7 +90,7 @@ describe('create_routes', () => {
});
it('allows regex qualifiers', () => {
const { pages } = create_routes_alt(path.join(__dirname, 'samples/qualifiers'));
const { pages } = create_routes(path.join(__dirname, 'samples/qualifiers'));
assert.deepEqual(pages.map(p => p.pattern), [
/^\/([0-9-a-z]{3,})\/?$/,
@@ -100,7 +100,7 @@ describe('create_routes', () => {
});
it('sorts routes correctly', () => {
const { pages } = create_routes_alt(path.join(__dirname, 'samples/sorting'));
const { pages } = create_routes(path.join(__dirname, 'samples/sorting'));
assert.deepEqual(pages.map(p => p.parts.map(part => part.component.file)), [
['_default.html'],
@@ -114,4 +114,33 @@ describe('create_routes', () => {
['[wildcard].html']
]);
});
it('ignores files and directories with leading underscores', () => {
const { server_routes } = create_routes(path.join(__dirname, 'samples/hidden-underscore'));
assert.deepEqual(server_routes.map(r => r.file), [
'index.js',
'e/f/g/h.js'
]);
});
it('ignores files and directories with leading dots except .well-known', () => {
const { server_routes } = create_routes(path.join(__dirname, 'samples/hidden-dot'));
assert.deepEqual(server_routes.map(r => r.file), [
'.well-known/dnt-policy.txt.js'
]);
});
it('fails on clashes', () => {
assert.throws(() => {
const { pages } = create_routes(path.join(__dirname, 'samples/clash-pages'));
console.log(pages);
}, /The \[bar\]\/index\.html and \[foo\]\.html pages clash/);
assert.throws(() => {
const { server_routes } = create_routes(path.join(__dirname, 'samples/clash-routes'));
console.log(server_routes);
}, /The \[bar\]\/index\.js and \[foo\]\.js routes clash/);
});
});