diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index ee9d3aa8fb..def46ef7df 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -268,9 +268,7 @@ def test_sys_manipulations_when_running_script(self, tmp_path): # GIVEN out_file = tmp_path / "result.bin" target_file = tmp_path / "test.py" - target_file.write_text("import some_adjacent_module") - other_file = tmp_path / "some_adjacent_module.py" - other_file.write_text("import sys; print(sys.argv); print(sys.path[0])") + target_file.write_text("import json, sys; print(json.dumps(sys.path))") # WHEN proc = subprocess.run( @@ -292,57 +290,48 @@ def test_sys_manipulations_when_running_script(self, tmp_path): ) # THEN - assert proc.returncode == 0 - argv, path0 = proc.stdout.splitlines() - assert argv == repr([str(target_file), "some", "provided args"]) - assert path0 == str(tmp_path) assert out_file.exists() + assert proc.returncode == 0 + # Running `python -m` put cwd in sys.path; ensure we replaced it. + path = json.loads(proc.stdout) + assert os.getcwd() not in path + assert str(tmp_path) in path def test_sys_manipulations_when_running_module(self, tmp_path): # GIVEN out_file = tmp_path / "result.bin" - target_file = tmp_path / "test.py" - target_file.write_text("import some_adjacent_module") - other_file = tmp_path / "some_adjacent_module.py" - other_file.write_text("import sys; print(sys.argv); print(sys.path[0])") - env = os.environ.copy() - env["PYTHONPATH"] = str(tmp_path) + ":" + os.environ.get("PYTHONPATH", "") # WHEN proc = subprocess.run( [ sys.executable, - "-m", - "memray", + "-c", + "import sys; from memray.commands import main; sys.exit(main())", "run", "--quiet", "--output", str(out_file), "-m", - "test", - "some", - "provided args", + "site", ], check=True, capture_output=True, text=True, - env=env, ) # THEN - assert proc.returncode == 0 - argv, path0 = proc.stdout.splitlines() - assert argv == repr([str(target_file), "some", "provided args"]) - assert path0 == os.getcwd() assert out_file.exists() + assert proc.returncode == 0 + # Running `python -c` put "" in sys.path; ensure we replaced it. + path = eval( + " ".join(line for line in proc.stdout.splitlines() if line.startswith(" ")) + ) + assert "" not in path + assert os.getcwd() in path def test_sys_manipulations_when_running_cmd(self, tmp_path): # GIVEN out_file = tmp_path / "result.bin" - other_file = tmp_path / "some_adjacent_module.py" - other_file.write_text("import sys; print(sys.argv); print(sys.path[0])") - env = os.environ.copy() - env["PYTHONPATH"] = str(tmp_path) + ":" + os.environ.get("PYTHONPATH", "") # WHEN proc = subprocess.run( @@ -355,22 +344,20 @@ def test_sys_manipulations_when_running_cmd(self, tmp_path): "--output", str(out_file), "-c", - "import some_adjacent_module", - "some", - "provided args", + "import json, sys; print(json.dumps(sys.path))", ], check=True, capture_output=True, text=True, - env=env, ) # THEN - assert proc.returncode == 0 - argv, path0 = proc.stdout.splitlines() - assert argv == repr(["-c", "some", "provided args"]) - assert path0 == "" assert out_file.exists() + assert proc.returncode == 0 + # Running `python -m` put cwd in sys.path; ensure we replaced it. + path = json.loads(proc.stdout) + assert os.getcwd() not in path + assert "" in path @pytest.mark.parametrize("option", [None, "--live", "--live-remote"]) def test_run_file_that_is_not_python(self, capsys, option):