diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index afde01a8..12e9ff89 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -14,6 +14,7 @@ import dataclasses import locale import logging +import platform import shlex import subprocess import urllib.parse @@ -222,11 +223,17 @@ def run(self, command: str, *args: str, **kwargs: Any) -> CommandResult: raise NotImplementedError def run_local(self, command: str, *args: str) -> CommandResult: - command = self.quote(command, *args) - cmd = self.encode(command) + shell_command = self.quote(command, *args) + cmd = self.encode(shell_command) + shell = True + if platform.system() == "Windows": + # WindowsService and WindowsFile expect the shell to be PowerShell + # OpenSSH Server in Windows and WinRM use PowerShell as shell by default + shell_command = ["powershell", "-Command", command] + shell = False p = subprocess.Popen( - cmd, - shell=True, + shell_command, + shell=shell, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, diff --git a/testinfra/modules/file.py b/testinfra/modules/file.py index 8f6dcd46..8cf051ae 100644 --- a/testinfra/modules/file.py +++ b/testinfra/modules/file.py @@ -344,7 +344,7 @@ def exists(self): """ return ( - self.check_output(r"powershell -command \"Test-Path '%s'\"", self.path) + self.check_output("Test-Path %s", self.path) == "True" ) @@ -352,7 +352,7 @@ def exists(self): def is_file(self): return ( self.check_output( - r"powershell -command \"(Get-Item '%s') -is [System.IO.FileInfo]\"", + "(Get-Item %s) -is [System.IO.FileInfo]", self.path, ) == "True" @@ -362,7 +362,7 @@ def is_file(self): def is_directory(self): return ( self.check_output( - r"powershell -command \"(Get-Item '%s') -is [System.IO.DirectoryInfo]\"", + "(Get-Item %s) -is [System.IO.DirectoryInfo]", self.path, ) == "True" @@ -380,7 +380,7 @@ def is_socket(self): def is_symlink(self): return ( self.check_output( - r"powershell -command \"(Get-Item -Path '%s').Attributes -band [System.IO.FileAttributes]::ReparsePoint\"", + "(Get-Item -Path %s).Attributes -band [System.IO.FileAttributes]::ReparsePoint", self.path, ) == "True" @@ -394,7 +394,7 @@ def linked_to(self): 'C:/Program Files/lock' """ return self.check_output( - r"powershell -command \"(Get-Item -Path '%s' -ReadOnly).FullName\"", + "(Get-Item -Path %s -ReadOnly).FullName", self.path, ) @@ -425,7 +425,7 @@ def contains(self, pattern): """ return ( self.run_test( - r"powershell -command \"Select-String -Path '%s' -Pattern '%s'\"", + "Select-String -Path %s -Pattern %s", self.path, pattern, ).stdout @@ -441,7 +441,7 @@ def sha256sum(self): raise NotImplementedError def _get_content(self, decode): - out = self.run_expect([0], r"powershell -command \"cat -- '%s'\"", self.path) + out = self.run_expect([0], "cat -- %s", self.path) if decode: return out.stdout return out.stdout_bytes @@ -472,7 +472,7 @@ def mtime(self): datetime.datetime(2015, 3, 15, 20, 25, 40) """ date_time_str = self.check_output( - r"powershell -command \"Get-ChildItem -Path '%s' | Select-Object -ExpandProperty LastWriteTime\"", + "Get-ChildItem -Path %s | Select-Object -ExpandProperty LastWriteTime", self.path, ) return datetime.datetime.strptime( @@ -484,7 +484,7 @@ def size(self): """Return size of file in bytes""" return int( self.check_output( - r"powershell -command \"Get-Item -Path '%s' | Select-Object -ExpandProperty Length\"", + "Get-Item -Path %s | Select-Object -ExpandProperty Length", self.path, ) ) @@ -496,7 +496,7 @@ def listdir(self): ['foo_file', 'bar_dir'] """ out = self.check_output( - r"powershell -command \"Get-ChildItem -Path '%s' | Select-Object -ExpandProperty Name\"", + "Get-ChildItem -Path %s | Select-Object -ExpandProperty Name", self.path, ) return [item.strip() for item in out.strip().split("\n")] diff --git a/testinfra/modules/service.py b/testinfra/modules/service.py index 24e666cb..30584713 100644 --- a/testinfra/modules/service.py +++ b/testinfra/modules/service.py @@ -340,7 +340,8 @@ class WindowsService(Service): @property def exists(self): out = self.check_output( - f"Get-Service -Name {self.name} -ErrorAction SilentlyContinue" + "Get-Service -Name %s -ErrorAction SilentlyContinue", + self.name ) return self.name in out @@ -348,7 +349,7 @@ def exists(self): def is_running(self): return ( self.check_output( - "Get-Service '%s' | Select -ExpandProperty Status", + "Get-Service %s | Select -ExpandProperty Status", self.name, ) == "Running" @@ -358,7 +359,7 @@ def is_running(self): def is_enabled(self): return ( self.check_output( - "Get-Service '%s' | Select -ExpandProperty StartType", + "Get-Service %s | Select -ExpandProperty StartType", self.name, ) == "Automatic"