Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

3.7 KB
Raw Blame
/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import fs from 'fs';
import path from 'path';
import { test, expect } from './npmTest';

// Lower bounds on the number of inlined npm packages per LICENSE. If a bundle drops below
// these, something is broken with bundle generation or dependencies were silently removed.
// Package count per bundle .LICENSE sidecar; `0` marks a non-bundle license
// (e.g. a vendored binary's third-party license) that has no "Total Packages"
// summary and is only checked for presence.
const EXPECTED: Record<string, Record<string, number>> = {
  'playwright-core': {
    'lib/serverRegistry.js.LICENSE': 2,
    'lib/utilsBundle.js.LICENSE': 75,
    // Vendored WebP codec (libwebp compiled to WASM), not a bundle sidecar.
    'lib/webp_codec.LICENSE': 0,
  },
  'playwright': {
    'lib/matchers/expect.js.LICENSE': 20,
    'lib/transform/esmLoader.js.LICENSE': 10,
    'lib/transform/babelBundle.js.LICENSE': 65,
  },
};

async function collectLicenses(dir: string): Promise<string[]> {
  const result: string[] = [];
  const walk = async (d: string) => {
    const entries = await fs.promises.readdir(d, { withFileTypes: true }).catch(() => []);
    for (const e of entries) {
      const p = path.join(d, e.name);
      if (e.isDirectory())
        await walk(p);
      else if (e.name.endsWith('.LICENSE'))
        result.push(p);
    }
  };
  await walk(dir);
  return result.sort();
}

for (const [pkg, licenses] of Object.entries(EXPECTED)) {
  test(`${pkg} bundles ship .LICENSE files with expected package counts`, async ({ exec, tmpWorkspace }) => {
    const registry = JSON.parse(await fs.promises.readFile(path.join(__dirname, '.registry.json'), 'utf8'));
    const tarball: string = registry[pkg];
    expect(tarball, `no tarball recorded for ${pkg} in .registry.json`).toBeTruthy();

    const extractDir = path.join(tmpWorkspace, `extract-${pkg}`);
    await fs.promises.mkdir(extractDir, { recursive: true });
    await exec('tar', '-xzf', tarball, '-C', extractDir);

    const libDir = path.join(extractDir, 'package', 'lib');
    const found = await collectLicenses(libDir);
    const expected = Object.keys(licenses).map(p => path.join(libDir, ...p.slice('lib/'.length).split('/'))).sort();
    expect(found, `LICENSE files under ${pkg}/lib do not match the expected set — update EXPECTED`).toEqual(expected);

    for (const [relPath, minPackages] of Object.entries(licenses)) {
      const absPath = path.join(extractDir, 'package', relPath);
      const contents = await fs.promises.readFile(absPath, 'utf8');
      if (!minPackages) {
        // Non-bundle license (no "Total Packages" summary) — just verify it ships.
        expect(contents.trim().length, `${pkg}/${relPath} is empty`).toBeGreaterThan(0);
        continue;
      }
      const match = contents.match(/^Total Packages: (\d+)$/m);
      expect(match, `${pkg}/${relPath} is missing the "Total Packages" summary line`).toBeTruthy();
      const count = Number(match![1]);
      test.info().annotations.push({ type: 'licenses', description: `${pkg}/${relPath}: ${count} packages` });
      expect(count, `${pkg}/${relPath} lists only ${count} packages, expected at least ${minPackages}`).toBeGreaterThanOrEqual(minPackages);
    }
  });
}