Skip to content

Commit

Permalink
Merge branch 'main' into fix/update-spread-attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Edo-San authored Jun 8, 2023
2 parents 78cdef2 + e6bff65 commit 7c6bc4f
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 197 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-keys-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Zod errors getting flagged as configuration errors
2 changes: 1 addition & 1 deletion .changeset/sour-turtles-tap.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'@astrojs/svelte': patch
'@astrojs/svelte': major
---

Update to svelte2tsx 0.6.15 and vite-plugin-svelte 2.4.1
31 changes: 15 additions & 16 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import * as colors from 'kleur/colors';
import type { Arguments as Flags } from 'yargs-parser';
import yargs from 'yargs-parser';
import { z } from 'zod';
import { ZodError } from 'zod';
import {
createSettings,
openConfig,
Expand Down Expand Up @@ -94,15 +94,19 @@ function resolveCommand(flags: Arguments): CLICommand {

async function handleConfigError(
e: any,
{ cwd, flags, logging }: { cwd?: string; flags?: Flags; logging: LogOptions }
{ cmd, cwd, flags, logging }: { cmd: string; cwd?: string; flags?: Flags; logging: LogOptions }
) {
const path = await resolveConfigPath({ cwd, flags, fs });
if (e instanceof Error) {
if (path) {
error(logging, 'astro', `Unable to load ${colors.bold(path)}\n`);
}
error(logging, 'astro', `Unable to load ${path ? colors.bold(path) : 'your Astro config'}\n`);
if (e instanceof ZodError) {
console.error(formatConfigErrorMessage(e) + '\n');
} else if (e instanceof Error) {
console.error(formatErrorMessage(collectErrorMetadata(e)) + '\n');
}
const telemetryPromise = telemetry.record(eventConfigError({ cmd, err: e, isFatal: true }));
await telemetryPromise.catch((err2: Error) =>
debug('telemetry', `record() error: ${err2.message}`)
);
}

/**
Expand Down Expand Up @@ -181,7 +185,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
cmd,
logging,
}).catch(async (e) => {
await handleConfigError(e, { cwd: root, flags, logging });
await handleConfigError(e, { cmd, cwd: root, flags, logging });
return {} as any;
});
if (!initialAstroConfig) return;
Expand All @@ -207,7 +211,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
logging,
telemetry,
handleConfigError(e) {
handleConfigError(e, { cwd: root, flags, logging });
handleConfigError(e, { cmd, cwd: root, flags, logging });
info(logging, 'astro', 'Continuing with previous valid configuration\n');
},
});
Expand Down Expand Up @@ -284,14 +288,9 @@ async function throwAndExit(cmd: string, err: unknown) {
process.exit(1);
}

if (err instanceof z.ZodError) {
telemetryPromise = telemetry.record(eventConfigError({ cmd, err, isFatal: true }));
errorMessage = formatConfigErrorMessage(err);
} else {
const errorWithMetadata = collectErrorMetadata(createSafeError(err));
telemetryPromise = telemetry.record(eventError({ cmd, err: errorWithMetadata, isFatal: true }));
errorMessage = formatErrorMessage(errorWithMetadata);
}
const errorWithMetadata = collectErrorMetadata(createSafeError(err));
telemetryPromise = telemetry.record(eventError({ cmd, err: errorWithMetadata, isFatal: true }));
errorMessage = formatErrorMessage(errorWithMetadata);

// Timeout the error reporter (very short) because the user is waiting.
// NOTE(fks): It is better that we miss some events vs. holding too long.
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('getStaticPaths - dev calls', () => {
});

after(async () => {
devServer.stop();
await devServer.stop();
});

it('only calls getStaticPaths once', async () => {
Expand Down
157 changes: 81 additions & 76 deletions packages/astro/test/astro-markdown-plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import addClasses from './fixtures/astro-markdown-plugins/add-classes.mjs';

async function buildFixture(config) {
const fixture = await loadFixture({
root: './fixtures/astro-markdown-plugins/',
...config,
});
await fixture.build();
return fixture;
}
const defaultMarkdownConfig = {
gfm: true,
smartypants: true,
remarkPlugins: [
remarkExamplePlugin,
'remark-code-titles',
['rehype-autolink-headings', { behavior: 'prepend' }],
],
rehypePlugins: [
'rehype-slug',
['rehype-toc', { headings: ['h2', 'h3'] }],
[addClasses, { 'h1,h2,h3': 'title' }],
],
};

function remarkExamplePlugin() {
return (tree) => {
Expand All @@ -22,97 +28,96 @@ function remarkExamplePlugin() {
}

describe('Astro Markdown plugins', () => {
it('Can render markdown with plugins', async () => {
const fixture = await buildFixture({
markdown: {
remarkPlugins: [
'remark-code-titles',
['rehype-autolink-headings', { behavior: 'prepend' }],
],
rehypePlugins: [
'rehype-slug',
['rehype-toc', { headings: ['h2', 'h3'] }],
[addClasses, { 'h1,h2,h3': 'title' }],
],
},
describe('Default test plugins', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-markdown-plugins/',
markdown: defaultMarkdownConfig,
});
await fixture.build();
});
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

// test 1: Added a TOC
expect($('.toc')).to.have.lengthOf(1);
it('Can render markdown with plugins', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

// test 2: Added .title to h1
expect($('#hello-world').hasClass('title')).to.equal(true);
});
// test 1: Added a TOC
expect($('.toc')).to.have.lengthOf(1);

// Asserts Astro 1.0 behavior is removed. Test can be removed in Astro 3.0.
it('Still applies default plugins when user plugins are provided', async () => {
const fixture = await buildFixture({
markdown: {
remarkPlugins: [remarkExamplePlugin],
rehypePlugins: [[addClasses, { 'h1,h2,h3': 'title' }]],
},
// test 2: Added .title to h1
expect($('#hello-world').hasClass('title')).to.equal(true);
});
const gfmHtml = await fixture.readFile('/with-gfm/index.html');
const $1 = cheerio.load(gfmHtml);
expect($1('a[href="https://example.com"]')).to.have.lengthOf(1);

const smartypantsHtml = await fixture.readFile('/with-smartypants/index.html');
const $2 = cheerio.load(smartypantsHtml);
expect($2('p').html()).to.equal('“Smartypants” is — awesome');
// Asserts Astro 1.0 behavior is removed. Test can be removed in Astro 3.0.
it('Still applies default plugins when user plugins are provided', async () => {
const gfmHtml = await fixture.readFile('/with-gfm/index.html');
const $1 = cheerio.load(gfmHtml);
expect($1('a[href="https://example.com"]')).to.have.lengthOf(1);

testRemark(gfmHtml);
testRehype(gfmHtml, '#github-flavored-markdown-test');
});
const smartypantsHtml = await fixture.readFile('/with-smartypants/index.html');
const $2 = cheerio.load(smartypantsHtml);
expect($2('p').html()).to.equal('“Smartypants” is — awesome');

for (const gfm of [true, false]) {
it(`Handles GFM when gfm = ${gfm}`, async () => {
const fixture = await buildFixture({
markdown: {
remarkPlugins: [remarkExamplePlugin],
rehypePlugins: [[addClasses, { 'h1,h2,h3': 'title' }]],
gfm,
},
});
testRemark(gfmHtml);
testRehype(gfmHtml, '#github-flavored-markdown-test');
});

it(`Handles GFM when gfm = true`, async () => {
const html = await fixture.readFile('/with-gfm/index.html');
const $ = cheerio.load(html);

// test 1: GFM autolink applied correctly
if (gfm === true) {
expect($('a[href="https://example.com"]')).to.have.lengthOf(1);
} else {
expect($('a[href="https://example.com"]')).to.have.lengthOf(0);
}
expect($('a[href="https://example.com"]')).to.have.lengthOf(1);

testRemark(html);
testRehype(html, '#github-flavored-markdown-test');
});
}

for (const smartypants of [true, false]) {
it(`Handles SmartyPants when smartypants = ${smartypants}`, async () => {
const fixture = await buildFixture({
markdown: {
remarkPlugins: [remarkExamplePlugin],
rehypePlugins: [[addClasses, { 'h1,h2,h3': 'title' }]],
smartypants,
},
});

it(`Handles SmartyPants when smartypants = true`, async () => {
const html = await fixture.readFile('/with-smartypants/index.html');
const $ = cheerio.load(html);

// test 1: GFM autolink applied correctly
if (smartypants === true) {
expect($('p').html()).to.equal('“Smartypants” is — awesome');
} else {
expect($('p').html()).to.equal('"Smartypants" is -- awesome');
}
// test 1: smartypants applied correctly
expect($('p').html()).to.equal('“Smartypants” is — awesome');

testRemark(html);
testRehype(html, '#smartypants-test');
});
}
});

it(`Handles GFM when gfm = false`, async () => {
const fixture = await loadFixture({
root: './fixtures/astro-markdown-plugins/',
markdown: { ...defaultMarkdownConfig, gfm: false },
});
await fixture.build();

const html = await fixture.readFile('/with-gfm/index.html');
const $ = cheerio.load(html);

expect($('a[href="https://example.com"]')).to.have.lengthOf(0);

testRemark(html);
testRehype(html, '#github-flavored-markdown-test');
});

it(`Handles SmartyPants when smartypants = false`, async () => {
const fixture = await loadFixture({
root: './fixtures/astro-markdown-plugins/',
markdown: { ...defaultMarkdownConfig, smartypants: false },
});
await fixture.build();

const html = await fixture.readFile('/with-smartypants/index.html');
const $ = cheerio.load(html);

expect($('p').html()).to.equal('"Smartypants" is -- awesome');

testRemark(html);
testRehype(html, '#smartypants-test');
});
});

function testRehype(html, headingId) {
Expand Down
20 changes: 0 additions & 20 deletions packages/astro/test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,3 @@ describe('astro cli', () => {
});
});
});

describe('astro cli i18n', () => {
const LOCALES = ['en_US', 'sv_SE', 'es_419.UTF-8', 'es_ES@euro', 'C'];
LOCALES.forEach((locale) => {
it(`astro does NOT throw on "${locale}" locales`, async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
let error = null;
try {
const proc = cli('dev', '--root', fileURLToPath(projectRootURL), {
env: { LANG: locale },
});
await parseCliDevStart(proc);
} catch (e) {
console.log(e);
error = e.message;
}
expect(error).to.be.null;
});
});
});
9 changes: 2 additions & 7 deletions packages/astro/test/client-address.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
import { loadFixture, silentLogging } from './test-utils.js';
import testAdapter from './test-adapter.js';
import { nodeLogDestination } from '../dist/core/logger/node.js';
import * as cheerio from 'cheerio';

describe('Astro.clientAddress', () => {
Expand Down Expand Up @@ -110,11 +109,7 @@ describe('Astro.clientAddress', () => {

before(async () => {
// We expect an error, so silence the output
const logging = {
dest: nodeLogDestination,
level: 'silent',
};
devServer = await fixture.startDevServer({ logging });
devServer = await fixture.startDevServer({ logging: silentLogging });
});

after(async () => {
Expand Down
Loading

0 comments on commit 7c6bc4f

Please sign in to comment.