[ci] format

This commit is contained in:
matthewp 2022-11-11 16:30:25 +00:00 committed by fredkbot
parent 9eee0f0166
commit 8aea2dea61
5 changed files with 129 additions and 98 deletions

View file

@ -74,10 +74,7 @@ export const AstroConfigSchema = z.object({
.url() .url()
.optional() .optional()
.transform((val) => (val ? appendForwardSlash(val) : val)), .transform((val) => (val ? appendForwardSlash(val) : val)),
base: z base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base),
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.base),
trailingSlash: z trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')]) .union([z.literal('always'), z.literal('never'), z.literal('ignore')])
.optional() .optional()

View file

@ -61,7 +61,11 @@ function getParts(part: string, file: string) {
return result; return result;
} }
function getPattern(segments: RoutePart[][], base: string, addTrailingSlash: AstroConfig['trailingSlash']) { function getPattern(
segments: RoutePart[][],
base: string,
addTrailingSlash: AstroConfig['trailingSlash']
) {
const pathname = segments const pathname = segments
.map((segment) => { .map((segment) => {
if (segment.length === 1 && segment[0].spread) { if (segment.length === 1 && segment[0].spread) {

View file

@ -9,18 +9,23 @@ describe('base configuration', () => {
describe('with trailingSlash: "never"', () => { describe('with trailingSlash: "never"', () => {
describe('index route', () => { describe('index route', () => {
it('Requests that include a trailing slash 404', async () => { it('Requests that include a trailing slash 404', async () => {
const fs = createFs({ const fs = createFs(
{
'/src/pages/index.astro': `<h1>testing</h1>`, '/src/pages/index.astro': `<h1>testing</h1>`,
}, root); },
root
);
await runInContainer({ await runInContainer(
{
fs, fs,
root, root,
userConfig: { userConfig: {
base: '/docs', base: '/docs',
trailingSlash: 'never', trailingSlash: 'never',
}, },
}, async (container) => { },
async (container) => {
const { req, res, done } = createRequestAndResponse({ const { req, res, done } = createRequestAndResponse({
method: 'GET', method: 'GET',
url: '/docs/', url: '/docs/',
@ -28,22 +33,28 @@ describe('base configuration', () => {
container.handle(req, res); container.handle(req, res);
await done; await done;
expect(res.statusCode).to.equal(404); expect(res.statusCode).to.equal(404);
}); }
);
}); });
it('Requests that exclude a trailing slash 200', async () => { it('Requests that exclude a trailing slash 200', async () => {
const fs = createFs({ const fs = createFs(
{
'/src/pages/index.astro': `<h1>testing</h1>`, '/src/pages/index.astro': `<h1>testing</h1>`,
}, root); },
root
);
await runInContainer({ await runInContainer(
{
fs, fs,
root, root,
userConfig: { userConfig: {
base: '/docs', base: '/docs',
trailingSlash: 'never', trailingSlash: 'never',
}, },
}, async (container) => { },
async (container) => {
const { req, res, done } = createRequestAndResponse({ const { req, res, done } = createRequestAndResponse({
method: 'GET', method: 'GET',
url: '/docs', url: '/docs',
@ -51,24 +62,30 @@ describe('base configuration', () => {
container.handle(req, res); container.handle(req, res);
await done; await done;
expect(res.statusCode).to.equal(200); expect(res.statusCode).to.equal(200);
}); }
);
}); });
}); });
describe('sub route', () => { describe('sub route', () => {
it('Requests that include a trailing slash 404', async () => { it('Requests that include a trailing slash 404', async () => {
const fs = createFs({ const fs = createFs(
{
'/src/pages/sub/index.astro': `<h1>testing</h1>`, '/src/pages/sub/index.astro': `<h1>testing</h1>`,
}, root); },
root
);
await runInContainer({ await runInContainer(
{
fs, fs,
root, root,
userConfig: { userConfig: {
base: '/docs', base: '/docs',
trailingSlash: 'never', trailingSlash: 'never',
}, },
}, async (container) => { },
async (container) => {
const { req, res, done } = createRequestAndResponse({ const { req, res, done } = createRequestAndResponse({
method: 'GET', method: 'GET',
url: '/docs/sub/', url: '/docs/sub/',
@ -76,22 +93,28 @@ describe('base configuration', () => {
container.handle(req, res); container.handle(req, res);
await done; await done;
expect(res.statusCode).to.equal(404); expect(res.statusCode).to.equal(404);
}); }
);
}); });
it('Requests that exclude a trailing slash 200', async () => { it('Requests that exclude a trailing slash 200', async () => {
const fs = createFs({ const fs = createFs(
{
'/src/pages/sub/index.astro': `<h1>testing</h1>`, '/src/pages/sub/index.astro': `<h1>testing</h1>`,
}, root); },
root
);
await runInContainer({ await runInContainer(
{
fs, fs,
root, root,
userConfig: { userConfig: {
base: '/docs', base: '/docs',
trailingSlash: 'never', trailingSlash: 'never',
}, },
}, async (container) => { },
async (container) => {
const { req, res, done } = createRequestAndResponse({ const { req, res, done } = createRequestAndResponse({
method: 'GET', method: 'GET',
url: '/docs/sub', url: '/docs/sub',
@ -99,7 +122,8 @@ describe('base configuration', () => {
container.handle(req, res); container.handle(req, res);
await done; await done;
expect(res.statusCode).to.equal(200); expect(res.statusCode).to.equal(200);
}); }
);
}); });
}); });
}); });

View file

@ -9,17 +9,23 @@ const root = new URL('../../fixtures/alias/', import.meta.url);
describe('routing - createRouteManifest', () => { describe('routing - createRouteManifest', () => {
it('using trailingSlash: "never" does not match the index route when it contains a trailing slash', async () => { it('using trailingSlash: "never" does not match the index route when it contains a trailing slash', async () => {
const fs = createFs({ const fs = createFs(
{
'/src/pages/index.astro': `<h1>test</h1>`, '/src/pages/index.astro': `<h1>test</h1>`,
}, root); },
const settings = await createDefaultDevSettings({ root
);
const settings = await createDefaultDevSettings(
{
base: '/search', base: '/search',
trailingSlash: 'never' trailingSlash: 'never',
}, root); },
root
);
const manifest = createRouteManifest({ const manifest = createRouteManifest({
cwd: fileURLToPath(root), cwd: fileURLToPath(root),
settings, settings,
fsMod: fs fsMod: fs,
}); });
const [{ pattern }] = manifest.routes; const [{ pattern }] = manifest.routes;
expect(pattern.test('')).to.equal(true); expect(pattern.test('')).to.equal(true);