Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e883de8

Browse files
committed
refactor: simpler way to use try_execfile.py
1 parent bfd259e commit e883de8

1 file changed

Lines changed: 13 additions & 27 deletions

File tree

tests/test_process.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ def test_save_signal_usr1(self) -> None:
817817
""")
818818

819819

820-
TRY_EXECFILE = os.path.join(os.path.dirname(__file__), "modules/process_test/try_execfile.py")
820+
TRY_EXECFILE = Path(__file__).parent / "modules/process_test/try_execfile.py"
821+
TRY_EXECFILE_CODE = TRY_EXECFILE.read_text(encoding="utf-8")
821822

822823

823824
class EnvironmentTest(CoverageTest):
@@ -835,23 +836,20 @@ def assert_tryexecfile_output(self, expected: str, actual: str) -> None:
835836
assert actual == expected
836837

837838
def test_coverage_run_is_like_python(self) -> None:
838-
with open(TRY_EXECFILE, encoding="utf-8") as f:
839-
self.make_file("run_me.py", f.read())
839+
self.make_file("run_me.py", TRY_EXECFILE_CODE)
840840
expected = self.run_command("python run_me.py")
841841
actual = self.run_command("coverage run run_me.py")
842842
self.assert_tryexecfile_output(expected, actual)
843843

844844
def test_coverage_run_far_away_is_like_python(self) -> None:
845-
with open(TRY_EXECFILE, encoding="utf-8") as f:
846-
self.make_file("sub/overthere/prog.py", f.read())
845+
self.make_file("sub/overthere/prog.py", TRY_EXECFILE_CODE)
847846
expected = self.run_command("python sub/overthere/prog.py")
848847
actual = self.run_command("coverage run sub/overthere/prog.py")
849848
self.assert_tryexecfile_output(expected, actual)
850849

851850
@pytest.mark.skipif(not env.WINDOWS, reason="This is about Windows paths")
852851
def test_coverage_run_far_away_is_like_python_windows(self) -> None:
853-
with open(TRY_EXECFILE, encoding="utf-8") as f:
854-
self.make_file("sub/overthere/prog.py", f.read())
852+
self.make_file("sub/overthere/prog.py", TRY_EXECFILE_CODE)
855853
expected = self.run_command("python sub\\overthere\\prog.py")
856854
actual = self.run_command("coverage run sub\\overthere\\prog.py")
857855
self.assert_tryexecfile_output(expected, actual)
@@ -863,24 +861,19 @@ def test_coverage_run_dashm_is_like_python_dashm(self) -> None:
863861
self.assert_tryexecfile_output(expected, actual)
864862

865863
def test_coverage_run_dir_is_like_python_dir(self) -> None:
866-
with open(TRY_EXECFILE, encoding="utf-8") as f:
867-
self.make_file("with_main/__main__.py", f.read())
868-
864+
self.make_file("with_main/__main__.py", TRY_EXECFILE_CODE)
869865
expected = self.run_command("python with_main")
870866
actual = self.run_command("coverage run with_main")
871867
self.assert_tryexecfile_output(expected, actual)
872868

873869
def test_coverage_run_dashm_dir_no_init_is_like_python(self) -> None:
874-
with open(TRY_EXECFILE, encoding="utf-8") as f:
875-
self.make_file("with_main/__main__.py", f.read())
876-
870+
self.make_file("with_main/__main__.py", TRY_EXECFILE_CODE)
877871
expected = self.run_command("python -m with_main")
878872
actual = self.run_command("coverage run -m with_main")
879873
self.assert_tryexecfile_output(expected, actual)
880874

881875
def test_coverage_run_dashm_dir_with_init_is_like_python(self) -> None:
882-
with open(TRY_EXECFILE, encoding="utf-8") as f:
883-
self.make_file("with_main/__main__.py", f.read())
876+
self.make_file("with_main/__main__.py", TRY_EXECFILE_CODE)
884877
self.make_file("with_main/__init__.py", "")
885878

886879
expected = self.run_command("python -m with_main")
@@ -953,9 +946,7 @@ def test_coverage_run_script_imports_doubledashsource(self) -> None:
953946
def test_coverage_run_dashm_is_like_python_dashm_off_path(self) -> None:
954947
# https://github.com/coveragepy/coveragepy/issues/242
955948
self.make_file("sub/__init__.py", "")
956-
with open(TRY_EXECFILE, encoding="utf-8") as f:
957-
self.make_file("sub/run_me.py", f.read())
958-
949+
self.make_file("sub/run_me.py", TRY_EXECFILE_CODE)
959950
expected = self.run_command("python -m sub.run_me")
960951
actual = self.run_command("coverage run -m sub.run_me")
961952
self.assert_tryexecfile_output(expected, actual)
@@ -972,8 +963,7 @@ def test_coverage_zip_is_like_python(self) -> None:
972963
# Test running coverage from a zip file itself. Some environments
973964
# (windows?) zip up the coverage main to be used as the coverage
974965
# command.
975-
with open(TRY_EXECFILE, encoding="utf-8") as f:
976-
self.make_file("run_me.py", f.read())
966+
self.make_file("run_me.py", TRY_EXECFILE_CODE)
977967
expected = self.run_command("python run_me.py")
978968
cov_main = os.path.join(TESTS_DIR, "covmain.zip")
979969
actual = self.run_command(f"python {cov_main} run run_me.py")
@@ -985,27 +975,23 @@ def test_coverage_zip_is_like_python(self) -> None:
985975
reason="Windows gets this wrong: https://github.com/python/cpython/issues/131484",
986976
)
987977
def test_pythonsafepath(self) -> None:
988-
with open(TRY_EXECFILE, encoding="utf-8") as f:
989-
self.make_file("run_me.py", f.read())
978+
self.make_file("run_me.py", TRY_EXECFILE_CODE)
990979
self.set_environ("PYTHONSAFEPATH", "1")
991980
expected = self.run_command("python run_me.py")
992981
actual = self.run_command("coverage run run_me.py")
993982
self.assert_tryexecfile_output(expected, actual)
994983

995984
@pytest.mark.skipif(env.PYVERSION < (3, 11), reason="PYTHONSAFEPATH is new in 3.11")
996985
def test_pythonsafepath_dashm_runme(self) -> None:
997-
with open(TRY_EXECFILE, encoding="utf-8") as f:
998-
self.make_file("run_me.py", f.read())
986+
self.make_file("run_me.py", TRY_EXECFILE_CODE)
999987
self.set_environ("PYTHONSAFEPATH", "1")
1000988
expected = self.run_command("python run_me.py")
1001989
actual = self.run_command("python -m coverage run run_me.py")
1002990
self.assert_tryexecfile_output(expected, actual)
1003991

1004992
@pytest.mark.skipif(env.PYVERSION < (3, 11), reason="PYTHONSAFEPATH is new in 3.11")
1005993
def test_pythonsafepath_dashm(self) -> None:
1006-
with open(TRY_EXECFILE, encoding="utf-8") as f:
1007-
self.make_file("with_main/__main__.py", f.read())
1008-
994+
self.make_file("with_main/__main__.py", TRY_EXECFILE_CODE)
1009995
self.set_environ("PYTHONSAFEPATH", "1")
1010996
expected = self.run_command("python -m with_main", status=1)
1011997
actual = self.run_command("coverage run -m with_main", status=1)

0 commit comments

Comments
 (0)