Skip to content

Fix quote logic and shell logic in Windows #808

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions testinfra/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dataclasses
import locale
import logging
import platform
import shlex
import subprocess
import urllib.parse
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions testinfra/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,15 @@ def exists(self):
"""

return (
self.check_output(r"powershell -command \"Test-Path '%s'\"", self.path)
self.check_output("Test-Path %s", self.path)
== "True"
)

@property
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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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,
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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,
)
)
Expand All @@ -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")]
7 changes: 4 additions & 3 deletions testinfra/modules/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,16 @@ 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

@property
def is_running(self):
return (
self.check_output(
"Get-Service '%s' | Select -ExpandProperty Status",
"Get-Service %s | Select -ExpandProperty Status",
self.name,
)
== "Running"
Expand All @@ -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"
Expand Down