diff options
author | Simon Sawicki <contact@grub4k.xyz> | 2024-04-27 10:37:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-27 10:37:26 +0200 |
commit | 64766459e37451b665c1464073c28361fbcf1c25 (patch) | |
tree | e1b9bcf206f171f20a2739a22814568d7c16e7cd /test/test_utils.py | |
parent | 89f535e2656964b4061c25a7739d4d6ba0a30568 (diff) |
[core/windows] Improve shell quoting and tests (#9802)
Authored by: Grub4K
Diffstat (limited to 'test/test_utils.py')
-rw-r--r-- | test/test_utils.py | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/test/test_utils.py b/test/test_utils.py index ddf0a7c24..824864577 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -2059,7 +2059,22 @@ Line 1 assert extract_basic_auth('http://user:pass@foo.bar') == ('http://foo.bar', 'Basic dXNlcjpwYXNz') @unittest.skipUnless(compat_os_name == 'nt', 'Only relevant on Windows') - def test_Popen_windows_escaping(self): + def test_windows_escaping(self): + tests = [ + 'test"&', + '%CMDCMDLINE:~-1%&', + 'a\nb', + '"', + '\\', + '!', + '^!', + 'a \\ b', + 'a \\" b', + 'a \\ b\\', + # We replace \r with \n + ('a\r\ra', 'a\n\na'), + ] + def run_shell(args): stdout, stderr, error = Popen.run( args, text=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -2067,15 +2082,18 @@ Line 1 assert not error return stdout - # Test escaping - assert run_shell(['echo', 'test"&']) == '"test""&"\n' - assert run_shell(['echo', '%CMDCMDLINE:~-1%&']) == '"%CMDCMDLINE:~-1%&"\n' - assert run_shell(['echo', 'a\nb']) == '"a"\n"b"\n' - assert run_shell(['echo', '"']) == '""""\n' - assert run_shell(['echo', '\\']) == '\\\n' - # Test if delayed expansion is disabled - assert run_shell(['echo', '^!']) == '"^!"\n' - assert run_shell('echo "^!"') == '"^!"\n' + for argument in tests: + if isinstance(argument, str): + expected = argument + else: + argument, expected = argument + + args = [sys.executable, '-c', 'import sys; print(end=sys.argv[1])', argument, 'end'] + assert run_shell(args) == expected + + escaped = shell_quote(argument, shell=True) + args = f'{sys.executable} -c "import sys; print(end=sys.argv[1])" {escaped} end' + assert run_shell(args) == expected if __name__ == '__main__': |