Repository navigation
test(core): skip environment-dependent tests with a reason instead of failing (#28830) #28832
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
Closed
Chirag6722
wants to merge
2
commits into
google-gemini:main
from
Chirag6722:fix/28830-guard-windows-env-dependent-tests
Closed
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
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,87 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as fs from 'node:fs'; | ||
| import * as os from 'node:os'; | ||
| import * as path from 'node:path'; | ||
|
|
||
| /** | ||
| * Capability probes for tests whose outcome depends on the host rather than on | ||
| * the code under test. | ||
| * | ||
| * These exist so such a test can be skipped with a reason instead of failing. | ||
| * A suite that is red for environmental reasons teaches contributors to ignore | ||
| * it, which costs more than the coverage the test provides. | ||
| * | ||
| * Prefer a capability probe to a `process.platform` check. Skipping every | ||
| * Windows host would also skip the contributors most likely to be changing | ||
| * Windows-specific behavior, whose machines can usually run these tests. | ||
| */ | ||
|
|
||
| let cachedCanCreateSymlinks: boolean | undefined; | ||
|
|
||
| /** | ||
| * Whether this host can create symbolic links. | ||
| * | ||
| * On Windows `fs.symlinkSync` needs Developer Mode or an elevated shell and | ||
| * otherwise throws `EPERM`, so a default developer machine cannot build a | ||
| * symlink fixture. This creates one real link rather than inferring from the | ||
| * platform, and caches the answer because it cannot change within a run. | ||
| */ | ||
| export function canCreateSymlinks(): boolean { | ||
| if (cachedCanCreateSymlinks !== undefined) { | ||
| return cachedCanCreateSymlinks; | ||
| } | ||
|
|
||
| let dir: string | undefined; | ||
| try { | ||
| dir = fs.mkdtempSync(path.join(os.tmpdir(), 'symlink-probe-')); | ||
| const target = path.join(dir, 'target'); | ||
| fs.writeFileSync(target, ''); | ||
| fs.symlinkSync(target, path.join(dir, 'link')); | ||
| cachedCanCreateSymlinks = true; | ||
| } catch { | ||
| cachedCanCreateSymlinks = false; | ||
| } finally { | ||
| if (dir !== undefined) { | ||
| try { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } catch { | ||
| // The probe must never fail a suite it is only there to describe. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return cachedCanCreateSymlinks; | ||
| } | ||
|
|
||
| /** | ||
| * Whether PowerShell 7+ (`pwsh`) is resolvable on PATH. | ||
| * | ||
| * The Windows shell-quoting pipeline behaves differently when it falls back to | ||
| * Windows PowerShell 5.1, which is what a default Windows install provides. | ||
| */ | ||
| export function hasPowerShell7(): boolean { | ||
| if (os.platform() !== 'win32') { | ||
| return false; | ||
| } | ||
|
|
||
| return (process.env['PATH'] ?? '').split(path.delimiter).some((entry) => { | ||
| // Windows PATH entries containing spaces are sometimes stored quoted. | ||
| // `path.join` would keep the quote inside the path, so the lookup would | ||
| // miss a pwsh that is actually installed — and a false negative here | ||
| // skips the tests on precisely the hosts that can run them. | ||
| const unquoted = entry.replace(/^"|"$/g, ''); | ||
| if (unquoted === '') { | ||
| return false; | ||
| } | ||
| try { | ||
| return fs.existsSync(path.join(unquoted, 'pwsh.exe')); | ||
| } catch { | ||
| return false; | ||
| } | ||
| }); | ||
| } | ||
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
Oops, something went wrong.
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.
On Windows, entries in the
PATHenvironment variable can sometimes be wrapped in double quotes (e.g., if they contain spaces). Whenpath.joinis called with a quoted entry, it produces an invalid path (e.g.,"C:\\Program Files\\PowerShell\\7"\\pwsh.exe), causingfs.existsSyncto returnfalseeven if PowerShell 7 is installed. Sanitizing the path entry by removing surrounding quotes ensures robust capability detection.