ci: optimize installation tests and inspect code (#11150)

This commit is contained in:
Alex Rudenko 2023-10-13 10:03:14 +02:00 committed by GitHub
parent 8aa6cb37d2
commit cfe4b8b0cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 118 additions and 96 deletions

View File

@ -60,7 +60,6 @@ jobs:
rm $diff_file rm $diff_file
check-changes: check-changes:
needs: inspect-code
uses: ./.github/workflows/changed-packages.yml uses: ./.github/workflows/changed-packages.yml
with: with:
check-mergeable-state: true check-mergeable-state: true

View File

@ -16,32 +16,37 @@
import assert from 'assert'; import assert from 'assert';
import {readdir} from 'fs/promises'; import {readdir} from 'fs/promises';
import {platform} from 'os';
import {join} from 'path'; import {join} from 'path';
import {configureSandbox} from './sandbox.js'; import {configureSandbox} from './sandbox.js';
import {readAsset} from './util.js'; import {readAsset} from './util.js';
describe('`puppeteer` with Firefox', () => { // Skipping this test on Windows as windows runners are much slower.
configureSandbox({ (platform() === 'win32' ? describe.skip : describe)(
dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'], '`puppeteer` with Firefox',
env: cwd => { () => {
return { configureSandbox({
PUPPETEER_CACHE_DIR: join(cwd, '.cache', 'puppeteer'), dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'],
PUPPETEER_PRODUCT: 'firefox', env: cwd => {
}; return {
}, PUPPETEER_CACHE_DIR: join(cwd, '.cache', 'puppeteer'),
}); PUPPETEER_PRODUCT: 'firefox',
};
},
});
it('evaluates CommonJS', async function () { it('evaluates CommonJS', async function () {
const files = await readdir(join(this.sandbox, '.cache', 'puppeteer')); const files = await readdir(join(this.sandbox, '.cache', 'puppeteer'));
assert.equal(files.length, 1); assert.equal(files.length, 1);
assert.equal(files[0], 'firefox'); assert.equal(files[0], 'firefox');
const script = await readAsset('puppeteer-core', 'requires.cjs'); const script = await readAsset('puppeteer-core', 'requires.cjs');
await this.runScript(script, 'cjs'); await this.runScript(script, 'cjs');
}); });
it('evaluates ES modules', async function () { it('evaluates ES modules', async function () {
const script = await readAsset('puppeteer-core', 'imports.js'); const script = await readAsset('puppeteer-core', 'imports.js');
await this.runScript(script, 'mjs'); await this.runScript(script, 'mjs');
}); });
}); }
);

View File

@ -15,40 +15,45 @@
*/ */
import {readFile, writeFile} from 'fs/promises'; import {readFile, writeFile} from 'fs/promises';
import {platform} from 'os';
import {join} from 'path'; import {join} from 'path';
import {configureSandbox} from './sandbox.js'; import {configureSandbox} from './sandbox.js';
import {execFile, readAsset} from './util.js'; import {execFile, readAsset} from './util.js';
describe('`puppeteer` with TypeScript', () => { // Skipping this test on Windows as windows runners are much slower.
configureSandbox({ (platform() === 'win32' ? describe.skip : describe)(
dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'], '`puppeteer` with TypeScript',
devDependencies: ['typescript@4.7.4', '@types/node@16.3.3'], () => {
env: cwd => { configureSandbox({
return { dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'],
PUPPETEER_CACHE_DIR: join(cwd, '.cache', 'puppeteer'), devDependencies: ['typescript@4.7.4', '@types/node@16.3.3'],
}; env: cwd => {
}, return {
}); PUPPETEER_CACHE_DIR: join(cwd, '.cache', 'puppeteer'),
};
},
});
it('should work', async function () { it('should work', async function () {
// Write a Webpack configuration. // Write a Webpack configuration.
await writeFile( await writeFile(
join(this.sandbox, 'tsconfig.json'), join(this.sandbox, 'tsconfig.json'),
await readAsset('puppeteer', 'tsconfig.json') await readAsset('puppeteer', 'tsconfig.json')
); );
// Write the source code. // Write the source code.
await writeFile( await writeFile(
join(this.sandbox, 'index.ts'), join(this.sandbox, 'index.ts'),
await readAsset('puppeteer', 'basic.ts') await readAsset('puppeteer', 'basic.ts')
); );
// Compile. // Compile.
await execFile('npx', ['tsc'], {cwd: this.sandbox, shell: true}); await execFile('npx', ['tsc'], {cwd: this.sandbox, shell: true});
const script = await readFile(join(this.sandbox, 'index.js'), 'utf-8'); const script = await readFile(join(this.sandbox, 'index.js'), 'utf-8');
await this.runScript(script, 'cjs'); await this.runScript(script, 'cjs');
}); });
}); }
);

View File

@ -17,6 +17,7 @@
import assert from 'assert'; import assert from 'assert';
import {readdirSync} from 'fs'; import {readdirSync} from 'fs';
import {readdir} from 'fs/promises'; import {readdir} from 'fs/promises';
import {platform} from 'os';
import {join} from 'path'; import {join} from 'path';
import {configureSandbox} from './sandbox.js'; import {configureSandbox} from './sandbox.js';
@ -46,57 +47,65 @@ describe('`puppeteer`', () => {
}); });
}); });
describe('`puppeteer` with PUPPETEER_DOWNLOAD_PATH', () => { // Skipping this test on Windows as windows runners are much slower.
configureSandbox({ (platform() === 'win32' ? describe.skip : describe)(
dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'], '`puppeteer` with PUPPETEER_DOWNLOAD_PATH',
env: cwd => { () => {
return { configureSandbox({
PUPPETEER_DOWNLOAD_PATH: join(cwd, '.cache', 'puppeteer'), dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'],
}; env: cwd => {
}, return {
}); PUPPETEER_DOWNLOAD_PATH: join(cwd, '.cache', 'puppeteer'),
};
},
});
it('evaluates', async function () { it('evaluates', async function () {
const files = await readdir(join(this.sandbox, '.cache', 'puppeteer')); const files = await readdir(join(this.sandbox, '.cache', 'puppeteer'));
assert.equal(files.length, 1); assert.equal(files.length, 1);
assert.equal(files[0], 'chrome'); assert.equal(files[0], 'chrome');
const script = await readAsset('puppeteer', 'basic.js'); const script = await readAsset('puppeteer', 'basic.js');
await this.runScript(script, 'mjs'); await this.runScript(script, 'mjs');
}); });
}); }
);
describe('`puppeteer` clears cache', () => { // Skipping this test on Windows as windows runners are much slower.
configureSandbox({ (platform() === 'win32' ? describe.skip : describe)(
dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'], '`puppeteer` clears cache',
env: cwd => { () => {
return { configureSandbox({
PUPPETEER_CACHE_DIR: join(cwd, '.cache', 'puppeteer'), dependencies: ['@puppeteer/browsers', 'puppeteer-core', 'puppeteer'],
}; env: cwd => {
}, return {
}); PUPPETEER_CACHE_DIR: join(cwd, '.cache', 'puppeteer'),
};
},
});
it('evaluates', async function () { it('evaluates', async function () {
assert.equal( assert.equal(
readdirSync(join(this.sandbox, '.cache', 'puppeteer', 'chrome')).length, readdirSync(join(this.sandbox, '.cache', 'puppeteer', 'chrome')).length,
1 1
); );
await this.runScript( await this.runScript(
await readAsset('puppeteer', 'installCanary.js'), await readAsset('puppeteer', 'installCanary.js'),
'mjs' 'mjs'
); );
assert.equal( assert.equal(
readdirSync(join(this.sandbox, '.cache', 'puppeteer', 'chrome')).length, readdirSync(join(this.sandbox, '.cache', 'puppeteer', 'chrome')).length,
2 2
); );
await this.runScript(await readAsset('puppeteer', 'trimCache.js'), 'mjs'); await this.runScript(await readAsset('puppeteer', 'trimCache.js'), 'mjs');
assert.equal( assert.equal(
readdirSync(join(this.sandbox, '.cache', 'puppeteer', 'chrome')).length, readdirSync(join(this.sandbox, '.cache', 'puppeteer', 'chrome')).length,
1 1
); );
}); });
}); }
);

View File

@ -72,6 +72,7 @@ declare module 'mocha' {
*/ */
export const configureSandbox = (options: SandboxOptions): void => { export const configureSandbox = (options: SandboxOptions): void => {
before(async function (): Promise<void> { before(async function (): Promise<void> {
console.time('before');
const sandbox = await mkdtemp(join(tmpdir(), 'puppeteer-')); const sandbox = await mkdtemp(join(tmpdir(), 'puppeteer-'));
const dependencies = (options.dependencies ?? []).map(module => { const dependencies = (options.dependencies ?? []).map(module => {
switch (module) { switch (module) {
@ -125,13 +126,16 @@ export const configureSandbox = (options: SandboxOptions): void => {
await writeFile(script, content); await writeFile(script, content);
await execFile('node', [script], {cwd: sandbox, env}); await execFile('node', [script], {cwd: sandbox, env});
}; };
console.timeEnd('before');
}); });
after(async function () { after(async function () {
console.time('after');
if (!process.env['KEEP_SANDBOX']) { if (!process.env['KEEP_SANDBOX']) {
await rm(this.sandbox, {recursive: true, force: true, maxRetries: 5}); await rm(this.sandbox, {recursive: true, force: true, maxRetries: 5});
} else { } else {
console.log('sandbox saved in', this.sandbox); console.log('sandbox saved in', this.sandbox);
} }
console.timeEnd('after');
}); });
}; };