-
-
Notifications
You must be signed in to change notification settings - Fork 2
test: add library quality gates and package validation #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
terminalchai
wants to merge
1
commit into
LibreSign:main
Choose a base branch
from
terminalchai:test/80-package-quality-gates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+191
−37
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ jobs: | |
| env: | ||
| CYPRESS_INSTALL_BINARY: 0 | ||
| run: | | ||
| npm i | ||
| npm ci | ||
| npm run build | ||
|
|
||
| - name: Publish to npm | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ jobs: | |
|
|
||
| - name: Install & Build | ||
| run: | | ||
| npm i | ||
| npm ci | ||
| npm run build:demo | ||
|
|
||
| - name: Deploy | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // SPDX-FileCopyrightText: 2026 LibreCode coop and contributors | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| import assert from 'node:assert/strict' | ||
| import { execFile } from 'node:child_process' | ||
| import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises' | ||
| import { tmpdir } from 'node:os' | ||
| import path from 'node:path' | ||
| import process from 'node:process' | ||
| import { promisify } from 'node:util' | ||
| import { fileURLToPath } from 'node:url' | ||
|
|
||
| const execFileAsync = promisify(execFile) | ||
| const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') | ||
| const npmExecPath = process.env.npm_execpath | ||
|
|
||
| if (!npmExecPath) { | ||
| throw new Error('Run this check through npm so the active npm executable can be reused.') | ||
| } | ||
|
|
||
| async function runNpm(args, cwd) { | ||
| return execFileAsync(process.execPath, [npmExecPath, ...args], { | ||
| cwd, | ||
| maxBuffer: 10 * 1024 * 1024, | ||
| }) | ||
| } | ||
|
|
||
| function exportTargets(exports) { | ||
| if (typeof exports === 'string') { | ||
| return [exports] | ||
| } | ||
| if (!exports || typeof exports !== 'object') { | ||
| return [] | ||
| } | ||
| return Object.values(exports).flatMap(exportTargets) | ||
| } | ||
|
|
||
| const tempRoot = await mkdtemp(path.join(tmpdir(), 'pdf-elements-package-')) | ||
|
|
||
| try { | ||
| const packDir = path.join(tempRoot, 'pack') | ||
| const consumerDir = path.join(tempRoot, 'consumer') | ||
| await Promise.all([mkdir(packDir), mkdir(consumerDir)]) | ||
|
|
||
| const { stdout } = await runNpm( | ||
| ['pack', '--json', '--ignore-scripts', '--pack-destination', packDir], | ||
| packageRoot | ||
| ) | ||
| const [packResult] = JSON.parse(stdout) | ||
| assert(packResult?.filename, 'npm pack did not report a tarball filename') | ||
|
|
||
| const packedFiles = new Set(packResult.files.map(({ path: filePath }) => filePath)) | ||
| const packageJson = JSON.parse(await readFile(path.join(packageRoot, 'package.json'), 'utf8')) | ||
| const requiredFiles = ['COPYING', 'README.md', 'dist/index.css', 'dist/index.mjs', packageJson.types] | ||
|
|
||
| for (const target of exportTargets(packageJson.exports)) { | ||
| assert(target.startsWith('./'), `Package export must be relative: ${target}`) | ||
| requiredFiles.push(target.slice(2)) | ||
| } | ||
|
|
||
| for (const filePath of new Set(requiredFiles)) { | ||
| assert(packedFiles.has(filePath), `Packed package is missing ${filePath}`) | ||
| } | ||
|
|
||
| await writeFile( | ||
| path.join(consumerDir, 'package.json'), | ||
| JSON.stringify({ name: 'pdf-elements-smoke-consumer', private: true, type: 'module' }), | ||
| 'utf8' | ||
| ) | ||
|
|
||
| const tarballPath = path.join(packDir, packResult.filename) | ||
| await runNpm( | ||
| ['install', '--ignore-scripts', '--no-audit', '--no-fund', '--no-package-lock', tarballPath], | ||
| consumerDir | ||
| ) | ||
|
|
||
| await writeFile( | ||
| path.join(consumerDir, 'verify.mjs'), | ||
| `import assert from 'node:assert/strict' | ||
| import PDFElements, { ensureWorkerReady } from '@libresign/pdf-elements' | ||
|
|
||
| assert(PDFElements, 'The public entry point has no default export') | ||
| assert.equal(typeof ensureWorkerReady, 'function') | ||
|
|
||
| for (const specifier of ${JSON.stringify(Object.keys(packageJson.exports))}) { | ||
| const publicSpecifier = specifier === '.' | ||
| ? '@libresign/pdf-elements' | ||
| : \`@libresign/pdf-elements/\${specifier.slice(2)}\` | ||
| assert(import.meta.resolve(publicSpecifier), \`Unable to resolve \${publicSpecifier}\`) | ||
| } | ||
| `, | ||
| 'utf8' | ||
| ) | ||
|
|
||
| await execFileAsync(process.execPath, ['verify.mjs'], { cwd: consumerDir }) | ||
|
|
||
| await writeFile( | ||
| path.join(consumerDir, 'index.ts'), | ||
| `import PDFElements, { | ||
| ensureWorkerReady, | ||
| type PDFDocumentEntry, | ||
| } from '@libresign/pdf-elements' | ||
|
|
||
| const component = PDFElements | ||
| const prepareWorker: () => Promise<void> = ensureWorkerReady | ||
| let document: PDFDocumentEntry | undefined | ||
|
|
||
| void component | ||
| void prepareWorker | ||
| void document | ||
| `, | ||
| 'utf8' | ||
| ) | ||
| await writeFile( | ||
| path.join(consumerDir, 'tsconfig.json'), | ||
| JSON.stringify({ | ||
| compilerOptions: { | ||
| lib: ['ES2022', 'DOM'], | ||
| module: 'NodeNext', | ||
| moduleResolution: 'NodeNext', | ||
| noEmit: true, | ||
| strict: true, | ||
| target: 'ES2022', | ||
| }, | ||
| include: ['index.ts'], | ||
| }), | ||
| 'utf8' | ||
| ) | ||
|
|
||
| const tscPath = path.join(packageRoot, 'node_modules', 'typescript', 'bin', 'tsc') | ||
| await execFileAsync(process.execPath, [tscPath, '--project', 'tsconfig.json'], { | ||
| cwd: consumerDir, | ||
| }) | ||
|
|
||
| globalThis.console.log(`Validated packed package ${packResult.filename} from a clean consumer.`) | ||
| } finally { | ||
| await rm(tempRoot, { recursive: true, force: true }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need
--ignore-scriptshere?The goal of this test is to validate the artifact produced by
npm packas a consumer would receive it. Since the package has aprepackscript, using--ignore-scriptsmeans this test depends onnpm run buildandnpm run validate:disthaving already been executed by the workflow instead of validating the real packing lifecycle.I think it would be safer to run
npm packnormally here, so this smoke test also catches problems inprepackor cases wheretest:packageis executed directly.