Skip to content

Commit

Permalink
Modernize python
Browse files Browse the repository at this point in the history
Differential Revision: D67415690

fbshipit-source-id: 3eca735039163ed137513c63fc0de59c0a81a71d
  • Loading branch information
zertosh authored and facebook-github-bot committed Dec 18, 2024
1 parent 6ccc605 commit e1dc5ff
Showing 1 changed file with 34 additions and 46 deletions.
80 changes: 34 additions & 46 deletions windows_shim/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -138,16 +139,16 @@ 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()

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, "")
Expand All @@ -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, "")
Expand All @@ -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, "")
Expand All @@ -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(
Expand All @@ -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, "")
Expand Down Expand Up @@ -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, "")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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')
Expand All @@ -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")
Expand All @@ -384,20 +377,18 @@ 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)

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, "")
Expand All @@ -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")
Expand All @@ -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, "")
Expand All @@ -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="/"),
)
Expand Down

0 comments on commit e1dc5ff

Please sign in to comment.