From e1dc5ff1d08aa11cf3ed43922db44fdf93984679 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Wed, 18 Dec 2024 15:58:09 -0800 Subject: [PATCH] Modernize python Differential Revision: D67415690 fbshipit-source-id: 3eca735039163ed137513c63fc0de59c0a81a71d --- windows_shim/tests/test.py | 80 ++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/windows_shim/tests/test.py b/windows_shim/tests/test.py index 3e101c2..68ba72a 100755 --- a/windows_shim/tests/test.py +++ b/windows_shim/tests/test.py @@ -15,11 +15,12 @@ import tempfile import time import unittest +from collections.abc import Iterator from contextlib import contextmanager from pathlib import Path -from typing import Final, Iterator, List +from typing import Final -EMPTY_STR_LIST: Final[List[str]] = [] +EMPTY_STR_LIST: Final[list[str]] = [] try: from .fb.ci import set_ci_envs @@ -29,14 +30,14 @@ pass -def get_path_env(name: str) -> str: +def get_path_env(name: str) -> Path: try: value = os.environ[name] except KeyError as err: raise unittest.SkipTest(f"required env var not set: `{name}`") from err try: - return str(Path(value).resolve(strict=True)) + return Path(value).resolve(strict=True) except FileNotFoundError as err: raise unittest.SkipTest(f"required path does not exist: `{value}`") from err @@ -113,7 +114,7 @@ def setUp(self) -> None: dotslash_bin = get_path_env("DOTSLASH_BIN") dotslash_windows_shim = get_path_env("DOTSLASH_WINDOWS_SHIM") - self._tempdir = tempfile.TemporaryDirectory() # noqa: P201 + self._tempdir = tempfile.TemporaryDirectory() self._fixtures: Path = Path(self._tempdir.name) bin_dir = self._fixtures / "bin" @@ -138,7 +139,8 @@ def tearDown(self) -> None: os.environ["PATH"] = self._original_path for _ in range(3): try: - return self._tempdir.cleanup() + self._tempdir.cleanup() + return except PermissionError: time.sleep(1) self._tempdir.cleanup() @@ -146,8 +148,7 @@ def tearDown(self) -> None: def test_args_none(self) -> None: ret = subprocess.run( [str(self._fixtures / "print_args.exe")], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "") @@ -163,8 +164,7 @@ def test_args_none_symlink(self) -> None: ret = subprocess.run( [str(print_args_path)], input="this should not be seen on Windows", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "") @@ -182,8 +182,7 @@ def test_args_none_with_period_in_name(self) -> None: ) ret = subprocess.run( [str(self._fixtures / "print_args.dotslash.exe")], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "") @@ -201,8 +200,7 @@ def test_args_none_with_no_extension(self) -> None: print_args_path = self._fixtures / "print_args" ret = subprocess.run( [print_args_path], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual( @@ -217,8 +215,7 @@ def test_args_none_with_no_extension(self) -> None: def test_args_none_with_unc_path(self) -> None: ret = subprocess.run( ["\\\\?\\" + str(self._fixtures / "print_args.exe")], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "") @@ -250,8 +247,7 @@ def test_args_none_max_path_name(self) -> None: "cp print_args.exe print_args-{suffix}.exe && " "./print_args-{suffix}.exe".format(suffix=SUFFIX), ], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "") @@ -261,8 +257,7 @@ def test_args_none_max_path_name(self) -> None: def test_args_empty(self) -> None: ret = subprocess.run( [str(self._fixtures / "print_args.exe"), ""], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "1:\n") @@ -272,8 +267,7 @@ def test_args_empty(self) -> None: def test_args_simple(self) -> None: ret = subprocess.run( [str(self._fixtures / "print_args.exe"), "a", "b", "c"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "1:a\n2:b\n3:c\n") @@ -291,8 +285,7 @@ def test_args_simple_with_unicode_in_name(self) -> None: ) ret = subprocess.run( [str(self._fixtures / "print🍎args.exe"), "a", "b", "c"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "1:a\n2:b\n3:c\n") @@ -311,8 +304,7 @@ def test_args_with_space_in_name(self) -> None: with self.subTest(args=args): ret = subprocess.run( [str(self._fixtures / "print args.exe"), *args], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, stderr) @@ -323,8 +315,7 @@ def test_args_simple_relative_path(self) -> None: with move_cwd(self._fixtures): ret = subprocess.run( ["./print_args.exe", "a", "b", "c"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "1:a\n2:b\n3:c\n") @@ -335,8 +326,7 @@ def test_args_simple_in_path(self) -> None: with prepend_path(self._fixtures): ret = subprocess.run( ["print_args.exe", "a", "b", "c"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "1:a\n2:b\n3:c\n") @@ -345,9 +335,13 @@ def test_args_simple_in_path(self) -> None: def test_args_quotes(self) -> None: ret = subprocess.run( - [str(self._fixtures / "print_args.exe"), '""', '"""', 'a="\\"\'b\'\\""'], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + [ + str(self._fixtures / "print_args.exe"), + '""', + '"""', + 'a="\\"\'b\'\\""', + ], + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, '1:""\n2:"""\n3:a="\\"\'b\'\\""\n') @@ -364,8 +358,7 @@ def test_args_unicode(self) -> None: "🍎", # 4 byte UTF-8 character https://www.compart.com/en/unicode/U+1F34E "πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨", # 11 bytes https://emojipedia.org/kiss-woman-man/ ], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "1:a\n2:Π€\n3:κŽ†\n4:🍎\n5:πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨\n") @@ -384,11 +377,10 @@ def test_args_long_args(self) -> None: long_arg = "x" * (MAX_COMMAND_LINE - 512) ret = subprocess.run( [str(self._fixtures / "print_args.exe"), long_arg], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) - self.assertEqual(ret.stderr, "1:{}\n".format(long_arg)) + self.assertEqual(ret.stderr, f"1:{long_arg}\n") self.assertRegex(ret.stdout, PRINT_ARGS_ARG0) self.assertEqual(ret.returncode, 0) @@ -396,8 +388,7 @@ def test_stdin_to_stdout(self) -> None: ret = subprocess.run( [str(self._fixtures / "stdin_to_stdout.exe")], input="abc", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "") @@ -408,8 +399,7 @@ def test_stdin_to_stderr(self) -> None: ret = subprocess.run( [str(self._fixtures / "stdin_to_stderr.exe")], input="abc", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "abc") @@ -419,8 +409,7 @@ def test_stdin_to_stderr(self) -> None: def test_exit_code(self) -> None: ret = subprocess.run( [str(self._fixtures / "exit_code.exe"), "64"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", ) self.assertEqual(ret.stderr, "") @@ -430,8 +419,7 @@ def test_exit_code(self) -> None: def test_missing_dotslash(self) -> None: ret = subprocess.run( [str(self._fixtures / "exit_code.exe"), "0"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf8", env=dict(os.environ, PATH="/"), )