0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Update smoke tests to include external docs and www (#2557)

* Update smoke tests

* nit: remove old comment

* chore: have smoketests use workspace

* nit: re-order application and execution
This commit is contained in:
Jonathan Neal 2022-02-15 10:51:12 -05:00 committed by GitHub
parent acc92431d6
commit fb9a77b77f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 216 additions and 19 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@ dist/
.DS_Store .DS_Store
.vercel .vercel
_site/ _site/
scripts/smoke/*-main/
*.log *.log
package-lock.json package-lock.json
.turbo/ .turbo/

View file

@ -27,6 +27,7 @@
"examples/component/demo", "examples/component/demo",
"examples/component/packages/*", "examples/component/packages/*",
"scripts", "scripts",
"scripts/smoke/*",
"packages/astro/test/fixtures/builtins/packages/*", "packages/astro/test/fixtures/builtins/packages/*",
"packages/astro/test/fixtures/builtins-polyfillnode", "packages/astro/test/fixtures/builtins-polyfillnode",
"packages/astro/test/fixtures/custom-elements/my-component-lib", "packages/astro/test/fixtures/custom-elements/my-component-lib",

12
scripts/jsconfig.json Normal file
View file

@ -0,0 +1,12 @@
{
"compilerOptions": {
"declaration": true,
"strict": true,
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"target": "esnext"
}
}

View file

@ -8,6 +8,7 @@
"astro-scripts": "./index.js" "astro-scripts": "./index.js"
}, },
"dependencies": { "dependencies": {
"adm-zip": "^0.5.9",
"arg": "^5.0.0", "arg": "^5.0.0",
"esbuild": "0.13.7", "esbuild": "0.13.7",
"globby": "^12.0.2", "globby": "^12.0.2",

65
scripts/smoke/cleanup.js Normal file
View file

@ -0,0 +1,65 @@
/** @file Remove all smoke tests and may remove extra smoke-test dependencies from `yarn.lock`. */
// @ts-check
import { execa } from 'execa';
import { polyfill } from '@astropub/webapi';
import { fileURLToPath } from 'node:url';
import { promises as fs } from 'node:fs';
polyfill(globalThis, { exclude: 'window document' });
/* Configuration
/* ========================================================================== */
/** URL directory containing this current script. */
const scriptDir = new URL('./', import.meta.url);
/** URL directory containing the entire project. */
const rootDir = new URL('../../', import.meta.url);
/* Application
/* ========================================================================== */
/** Runs all smoke tests. */
async function run() {
const dirs = await getChildDirectories(scriptDir)
if (dirs.length) {
console.log()
for (const dir of await getChildDirectories(scriptDir)) {
console.log('🤖', 'Removing', dir.pathname.split('/').at(-1));
fs.rm(dir, { force: true, recursive: true })
}
}
console.log()
console.log('🤖', 'Resetting', 'yarn');
await execa('yarn', [], { cwd: fileURLToPath(rootDir), stdout: 'inherit', stderr: 'inherit' });
}
/* Functionality
/* ========================================================================== */
/** Returns all child directories of the given directory. */
const getChildDirectories = async (/** @type {URL} */ dir) => {
/** @type {URL[]} */
const dirs = [];
for await (const dirent of await fs.opendir(dir)) {
if (dirent.isDirectory()) {
dirs.push(new URL(dirent.name, dir));
}
}
return dirs;
};
/* Execution
/* -------------------------------------------------------------------------- */
run();

View file

@ -1,34 +1,146 @@
/** @todo migrate these to use the independent docs repository at https://github.com/withastro/docs */ /** @file Runs all smoke tests and may add extra smoke-test dependencies to `yarn.lock`. */
import fs from 'fs'; // @ts-check
import Zip from 'adm-zip';
import { execa } from 'execa'; import { execa } from 'execa';
import { fileURLToPath } from 'url'; import { polyfill } from '@astropub/webapi';
import path from 'path'; import { fileURLToPath } from 'node:url';
import { promises as fs } from 'node:fs';
// NOTE: Only needed for Windows, due to a Turbo bug. polyfill(globalThis, { exclude: 'window document' });
// Once Turbo works on Windows, we can remove this script
// and update our CI to run through Turbo.
export default async function run() { /* Configuration
const examplesUrl = new URL('../../examples/', import.meta.url); /* -------------------------------------------------------------------------- */
const examplesToTest = fs
.readdirSync(examplesUrl)
.map((filename) => new URL(filename, examplesUrl))
.filter((fileUrl) => fs.statSync(fileUrl).isDirectory());
const allProjectsToTest = [...examplesToTest, new URL('../../docs', import.meta.url)];
/** URL directory containing this current script. */
const scriptDir = new URL('./', import.meta.url);
/** URL directory containing the entire project. */
const rootDir = new URL('../../', import.meta.url);
/** URL directory containing the example subdirectories. */
const exampleDir = new URL('examples/', rootDir);
/** URL directory containing the Astro package. */
const astroDir = new URL('packages/astro/', rootDir);
/** GitHub configuration for the external "docs" Astro project. */
const docGithubConfig = { org: 'withastro', name: 'docs', branch: 'main' };
/** GitHub configuration for the external "astro.build" Astro project. */
const wwwGithubConfig = { org: 'withastro', name: 'astro.build', branch: 'main' };
/* Application
/* -------------------------------------------------------------------------- */
/** Runs all smoke tests. */
async function run() {
console.log(''); console.log('');
for (const projectToTest of allProjectsToTest) {
const filePath = fileURLToPath(projectToTest); const directories = await getChildDirectories(exampleDir);
console.log(' 🤖 Testing', filePath, '\n');
directories.push(await downloadGithubZip(docGithubConfig), await downloadGithubZip(wwwGithubConfig));
console.log('🤖', 'Preparing', 'yarn');
await execa('yarn', [], { cwd: fileURLToPath(rootDir), stdout: 'inherit', stderr: 'inherit' });
for (const directory of directories) {
console.log('🤖', 'Testing', directory.pathname.split('/').at(-1));
try { try {
await execa('yarn', ['build'], { cwd: fileURLToPath(projectToTest), stdout: 'inherit', stderr: 'inherit' }); await execa('yarn', ['build'], { cwd: fileURLToPath(directory), stdout: 'inherit', stderr: 'inherit' });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
process.exit(1); process.exit(1);
} }
console.log('\n 🤖 Test complete.');
console.log();
} }
} }
/* Functionality
/* -------------------------------------------------------------------------- */
/** Returns the URL to the ZIP of the given GitHub project. */
const getGithubZipURL = (/** @type {GithubOpts} */ opts) => `https://github.com/${opts.org}/${opts.name}/archive/refs/heads/${opts.branch}.zip`;
/** Returns the awaited ZIP Buffer from the given GitHub project. */
const fetchGithubZip = (/** @type {GithubOpts} */ opts) =>
fetch(getGithubZipURL(opts))
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => Buffer.from(arrayBuffer));
/** Downloads a ZIP from the given GitHub project. */
const downloadGithubZip = async (/** @type {GithubOpts} */ opts) => {
/** Expected directory when the zip is downloaded. */
const githubDir = new URL(`${opts.name}-${opts.branch}`, scriptDir);
/** Whether the expected directory is already available */
const hasGithubDir = await fs.stat(githubDir).then(
(stats) => stats.isDirectory(),
() => false
);
if (!hasGithubDir) {
console.log('🤖', 'Downloading', `${opts.org}/${opts.name}#${opts.branch}`);
const buffer = await fetchGithubZip(opts);
console.log('🤖', 'Extracting', `${opts.org}/${opts.name}#${opts.branch}`);
new Zip(buffer).extractAllTo(fileURLToPath(scriptDir), true);
console.log('🤖', 'Preparing', `${opts.org}/${opts.name}#${opts.branch}`);
const astroPackage = await readDirectoryPackage(astroDir);
const githubPackage = await readDirectoryPackage(githubDir);
if ('astro' in Object(githubPackage.dependencies)) {
githubPackage.dependencies['astro'] = astroPackage.version;
}
if ('astro' in Object(githubPackage.devDependencies)) {
githubPackage.devDependencies['astro'] = astroPackage.version;
}
if ('astro' in Object(githubPackage.peerDependencies)) {
githubPackage.peerDependencies['astro'] = astroPackage.version;
}
await writeDirectoryPackage(githubDir, githubPackage);
}
return githubDir;
};
/** Returns the parsed package.json of the given directory. */
const readDirectoryPackage = async (/** @type {URL} */ dir) => JSON.parse(await fs.readFile(new URL('package.json', dir + '/'), 'utf-8'));
/** Returns upon completion of writing a package.json to the given directory. */
const writeDirectoryPackage = async (/** @type {URL} */ dir, /** @type {any} */ data) =>
await fs.writeFile(new URL('package.json', dir + '/'), JSON.stringify(data, null, ' ') + '\n');
/** Returns all child directories of the given directory. */
const getChildDirectories = async (/** @type {URL} */ dir) => {
/** @type {URL[]} */
const dirs = [];
for await (const dirent of await fs.opendir(dir)) {
if (dirent.isDirectory()) {
dirs.push(new URL(dirent.name, dir));
}
}
return dirs;
};
/* Execution
/* -------------------------------------------------------------------------- */
run(); run();
/** @typedef {{ org: string, name: string, branch: string }} GithubOpts */

View file

@ -2286,6 +2286,11 @@ acorn@^8.6.0, acorn@^8.7.0:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.0.tgz#90951fde0f8f09df93549481e5fc141445b791cf" resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.0.tgz#90951fde0f8f09df93549481e5fc141445b791cf"
integrity sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ== integrity sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==
adm-zip@^0.5.9:
version "0.5.9"
resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.5.9.tgz#b33691028333821c0cf95c31374c5462f2905a83"
integrity sha512-s+3fXLkeeLjZ2kLjCBwQufpI5fuN+kIGBxu6530nVQZGVol0d7Y/M88/xw9HGGUcJjKf8LutN3VPRUBq6N7Ajg==
agent-base@6, agent-base@^6.0.0, agent-base@^6.0.2: agent-base@6, agent-base@^6.0.0, agent-base@^6.0.2:
version "6.0.2" version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"