Skip to content

Commit 567653a

Browse files
committed
Fix PowerShell activation on *nix systems
1 parent d17098d commit 567653a

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

pyautoenv.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,26 +373,27 @@ def poetry_project_name(directory: str) -> Union[str, None]:
373373

374374
def activator(env_directory: str, args: Args) -> str:
375375
"""Get the activator script for the environment in the given directory."""
376-
dir_name = "Scripts" if operating_system() == Os.WINDOWS else "bin"
376+
is_windows = operating_system() == Os.WINDOWS
377+
dir_name = "Scripts" if is_windows else "bin"
377378
if args.fish:
378379
script = "activate.fish"
379380
elif args.pwsh:
380-
poetry_dir = poetry_cache_dir()
381-
if (
382-
poetry_dir is not None
383-
and env_directory.startswith(poetry_dir)
384-
and operating_system() != Os.WINDOWS
385-
):
386-
# In poetry environments on *NIX systems, this activator has
387-
# a lowercase A.
388-
script = "activate.ps1"
381+
if is_windows:
382+
script = "Activate.ps1"
389383
else:
390-
# In venv environments, and Windows poetry environments,
391-
# this activator has an uppercase A.
384+
# PowerShell activation scripts on Nix systems have some
385+
# slightly inconsistent naming. When using Poetry or uv, the
386+
# activation script is lower case, using the venv module,
387+
# the script is title case.
388+
# We can't really know what was used to generate the venv
389+
# so just check which activation script exists.
390+
script_path = os.path.join(env_directory, dir_name, "activate.ps1")
391+
if os.path.isfile(script_path):
392+
return script_path
392393
script = "Activate.ps1"
393394
else:
394395
script = "activate"
395-
return os.path.join(env_directory, dir_name, f"{script}")
396+
return os.path.join(env_directory, dir_name, script)
396397

397398

398399
@lru_cache(maxsize=128)

tests/test_venv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def fs(self, fs: FakeFilesystem) -> FakeFilesystem:
6363
fs.create_dir(self.PY_PROJ / "src")
6464
fs.create_file(self.VENV_DIR / "bin" / "activate")
6565
fs.create_file(self.VENV_DIR / "bin" / "activate.fish")
66-
fs.create_file(self.VENV_DIR / "bin" / "Activate.ps1")
66+
fs.create_file(self.VENV_DIR / "bin" / "activate.ps1")
6767
fs.create_file(self.VENV_DIR / "Scripts" / "activate")
6868
fs.create_file(self.VENV_DIR / "Scripts" / "Activate.ps1")
6969
fs.create_dir("not_a_venv")
@@ -200,7 +200,7 @@ class TestVenvBashLinux(VenvTester):
200200

201201

202202
class TestVenvPwshLinux(VenvTester):
203-
activator = "bin/Activate.ps1"
203+
activator = "bin/activate.ps1"
204204
flag = "--pwsh"
205205
os = pyautoenv.Os.LINUX
206206

0 commit comments

Comments
 (0)