Repository navigation
test(gax): verify handwritten lifecycle hooks and gaxios @types/bun - #9729
danieljbruce wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a new unit test file bunHandwrittenLifecycleAndTypes.ts to verify handwritten libraries' lifecycle hooks and ensure gaxios fetch compliance types are correctly defined. The review feedback suggests normalizing whitespace in the source files before asserting on code snippets to prevent the tests from being fragile to formatting changes.
| assert.ok( | ||
| gaxiosSrc.includes( | ||
| 'fetch: (...args: Parameters<typeof fetch>) => ReturnType<typeof fetch>;', | ||
| ), | ||
| 'Expected FetchCompliance in gaxios.ts to use the callable fetch signature instead of typeof fetch', | ||
| ); | ||
| assert.ok( | ||
| commonSrc.includes( | ||
| 'fetchImplementation?: (\n ...args: Parameters<typeof fetch>\n ) => ReturnType<typeof fetch>;', | ||
| ) || | ||
| commonSrc.includes( | ||
| '(...args: Parameters<typeof fetch>) => ReturnType<typeof fetch>', | ||
| ), | ||
| 'Expected GaxiosOptions.fetchImplementation in common.ts to use the callable fetch signature', | ||
| ); |
There was a problem hiding this comment.
Asserting exact multi-line strings with specific indentation and line breaks (like \n ...) is highly fragile and prone to breaking if code formatters (e.g., Prettier) are run or updated.
To make the test robust against formatting changes, normalize the whitespace of the source files before performing the substring checks.
const normalizedGaxiosSrc = gaxiosSrc.replace(/\s+/g, ' ');
assert.ok(
normalizedGaxiosSrc.includes(
'fetch: (...args: Parameters<typeof fetch>) => ReturnType<typeof fetch>',
),
'Expected FetchCompliance in gaxios.ts to use the callable fetch signature instead of typeof fetch',
);
const normalizedCommonSrc = commonSrc.replace(/\s+/g, ' ');
assert.ok(
normalizedCommonSrc.includes(
'fetchImplementation?: (...args: Parameters<typeof fetch>) => ReturnType<typeof fetch>',
) ||
normalizedCommonSrc.includes(
'(...args: Parameters<typeof fetch>) => ReturnType<typeof fetch>',
),
'Expected GaxiosOptions.fetchImplementation in common.ts to use the callable fetch signature',
);| 'Expected GaxiosOptions.fetchImplementation in common.ts to use the callable fetch signature', | ||
| ); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
[Generated] Add regression unit test suite verifying all 14 handwritten/* client libraries define clean prepare scripts without bun install trustedDependencies-blocked postinstall hooks and verifying gaxios narrows FetchCompliance to (...args: Parameters<typeof fetch>) => ReturnType<typeof fetch> for @types/bun compatibility.
2e92698 to
72877ed
Compare
557b47e to
5e19a64
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a new test suite, bunHandwrittenLifecycleAndTypes.ts, to verify that handwritten libraries define clean prepare scripts without blocked postinstall hooks under Bun, and that gaxios correctly narrows fetch signatures for @types/bun. The feedback recommends dynamically resolving the repository root to support running tests from both compiled JS and source TS directories, and normalizing line endings to LF when reading source files to prevent test failures on Windows due to CRLF line endings.
| import * as path from 'path'; | ||
| import {describe, it} from 'mocha'; | ||
|
|
||
| const REPO_ROOT = path.resolve(__dirname, '../../../../../..'); |
There was a problem hiding this comment.
To ensure the tests can be run directly from the TypeScript source files (e.g., via ts-node or bun directly on the .ts file) as well as from the compiled JavaScript in the build directory, resolve the repository root dynamically by checking for the existence of a known directory like handwritten.
const REPO_ROOT = (() => {
const paths = [
path.resolve(__dirname, '../../../../../..'), // from build/test/unit (compiled JS)
path.resolve(__dirname, '../../../../..'), // from test/unit (source TS)
];
for (const p of paths) {
if (fs.existsSync(path.join(p, 'handwritten'))) {
return p;
}
}
throw new Error('Could not resolve repository root');
})();| const gaxiosSrc = fs.readFileSync(gaxiosSrcPath, 'utf8'); | ||
| const commonSrc = fs.readFileSync(commonSrcPath, 'utf8'); |
There was a problem hiding this comment.
On Windows platforms, git may checkout files with CRLF (\r\n) line endings. Since the assertion on commonSrc contains explicit \n characters, this test will fail on Windows. Normalizing the line endings to \n after reading the files prevents this issue.
| const gaxiosSrc = fs.readFileSync(gaxiosSrcPath, 'utf8'); | |
| const commonSrc = fs.readFileSync(commonSrcPath, 'utf8'); | |
| const gaxiosSrc = fs.readFileSync(gaxiosSrcPath, 'utf8').replace(/\r\n/g, '\n'); | |
| const commonSrc = fs.readFileSync(commonSrcPath, 'utf8').replace(/\r\n/g, '\n'); |
Description
Adds tests verifying handwritten lifecycle hooks and gaxios @types/bun FetchCompliance compatibility.
Impact
Prevents regressions in bun install lifecycle scripts and @types/bun typechecking.
Changes
core/packages/gax/test/unit/bunHandwrittenLifecycleAndTypes.tscovering all 14handwritten/*client libraries and 14core/*packages:package.jsondefines deterministicpreparecompilation hooks without unapprovedpostinstalllifecycle scripts that would be blocked by Bun v1.1+ defaulttrustedDependenciespolicies (explicitly accounting for@google-cloud/profiler's V8-nativepprofaddon).gaxios(FetchComplianceandGaxiosOptions['fetchImplementation']) type-checks cleanly whenglobalThis.fetchincludes@types/bun's staticfetch.preconnectnamespace property withskipLibCheck: false.Testing
python3 .agents/skills/cistack/scripts/cistack.py --repo-root . --base-ref fix/b578005287-spanner-otel-bun-release --branches test/b578004580-bun-handwritten-lifecycle-gaxios --mode preflight-push2andbun --bun bin/run-test.cjs --no-c8 core/packages/gax/build/test/unit/streamArrayParser.js.core/packages/gax/build/test/unit/bunHandwrittenLifecycleAndTypes.jsunder both Node.js and Bun (JS_RUNTIME=bun).node ./bin/linter.mjs --strictpasses with zero errors.Alternatives
b/576206722) without in-repo regression tests; rejected because in-repo unit tests catch lifecycle script orFetchCompliancetype regressions in CI before release.bun installacross all 14 handwritten libraries on every unit test invocation; rejected for unit tests because network registry installs are slow and flaky in offline/hermetic CI environments.prepare/postinstallscripts or@types/bundeclaration compatibility.Fixes b/578004580
For b/576206722