Repository navigation
Skip a Symlink Test Where Windows Withholds the Privilege, and Run bash From PATH #2508
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d0699ee
Skip a Symlink Test Where Windows Withholds the Privilege, and Run ba…
ptr727 e5ba2e2
Walk PATH Past the WSL Launcher, and Read It on Every Call
ptr727 2ca0124
Quote the Receipt Case's Script Path, and Pass Over a bash Shim
ptr727 9ee210b
Test the Windows bash Selection on Any Host, and Name an Unusable bas…
ptr727 6341616
Probe Each PATH Directory for bash.exe, and Record the Skipped Link S…
ptr727 2760e9a
Take the Link Case Last in the Two Mixed build_dist Tests
ptr727 07c31f6
Let shutil.which Answer for bash Off Windows
ptr727 3e5e7fd
Join a Stub Directory Onto PATH With os.pathsep in Two configure Tests
ptr727 08f21e5
Resolve the jq Stub's Directory in bash Before Prepending It
ptr727 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Host capabilities a test needs and a Windows host may lack. | ||
|
|
||
| A test that needs one of these skips with the reason when the host lacks it, rather than | ||
| erroring on a precondition that has nothing to do with the code under test. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import functools | ||
| import ntpath | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| @functools.cache | ||
| def can_symlink() -> bool: | ||
| """Whether this host lets an unprivileged process create a symlink. | ||
|
|
||
| Windows refuses one with WinError 1314 unless Developer Mode is on or the process is elevated. | ||
| """ | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| try: | ||
| (Path(tmp) / "link").symlink_to(Path(tmp) / "target") | ||
| except (OSError, NotImplementedError): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| NO_SYMLINK = "this host cannot create a symlink without Developer Mode or elevation" | ||
| requires_symlink = unittest.skipUnless(can_symlink(), NO_SYMLINK) | ||
|
|
||
|
|
||
| _WINDOWS = os.name == "nt" | ||
|
|
||
|
|
||
| def _windows_path(path: str) -> str: | ||
| return ntpath.normcase(ntpath.normpath(path)) | ||
|
|
||
|
|
||
| def runs_a_script(found: str) -> bool: | ||
| """Whether found, an absolute path, is a bash that runs a script on this host. | ||
|
|
||
| On Windows that rules out a Windows-supplied bash.exe, under the Windows directory or among | ||
| the WindowsApps aliases, which starts WSL rather than running bash here. Paths are compared by | ||
| ntpath rather than resolved, so the check reads the same on any host a test runs it on. | ||
| """ | ||
| if not _WINDOWS: | ||
| return True | ||
| path = _windows_path(found) | ||
| windows = _windows_path(os.environ.get("SystemRoot", r"C:\Windows")) | ||
| if path.startswith(windows + "\\"): | ||
| return False | ||
| profile = os.environ.get("LOCALAPPDATA") | ||
| return not profile or ntpath.dirname(path) != _windows_path( | ||
| ntpath.join(profile, "Microsoft", "WindowsApps") | ||
| ) | ||
|
|
||
|
|
||
| def _executable(path: str) -> bool: | ||
| return os.path.isfile(path) and os.access(path, os.X_OK) | ||
|
|
||
|
|
||
| def bash_path() -> str | None: | ||
| """The first bash on PATH that a workflow step's script can run under, or None. | ||
|
|
||
| A bare "bash" passed to subprocess on Windows is resolved by CreateProcess, which searches | ||
| System32 before PATH and so finds the WSL launcher. That launcher hands the script to a second | ||
| shell that re-parses it, which mangles any quoting or expansion the script relies on, so the | ||
| PATH entry is named instead, passing over the launcher wherever PATH lists it. There each | ||
| absolute directory is probed for bash.exe itself rather than through shutil.which, which on | ||
| Windows would also look in the current directory first and admit a bash.cmd shim that cmd.exe | ||
| re-parses. Elsewhere shutil.which answers, as it did for a bare "bash". PATH is read on every | ||
| call rather than cached, since a test may change it for the duration of its run. | ||
| """ | ||
| if not _WINDOWS: | ||
| return shutil.which("bash") | ||
| for entry in os.environ.get("PATH", "").split(ntpath.pathsep): | ||
| directory = entry.strip('"') | ||
| if not ntpath.isabs(directory): | ||
| continue | ||
| candidate = ntpath.join(directory, "bash.exe") | ||
| if _executable(candidate) and runs_a_script(candidate): | ||
| return candidate | ||
| return None | ||
|
|
||
|
|
||
| def bash_or_skip() -> str: | ||
| """bash_path(), skipping the calling test where this host has none.""" | ||
| found = bash_path() | ||
| if found is None: | ||
| raise unittest.SkipTest( | ||
| "no bash on PATH that runs a script on this host, so run the suite from Git Bash" | ||
| ) | ||
| return found |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.