Skip to content

Commit 8b368a1

Browse files
godlygeekpablogsal
authored andcommitted
tests: Improve tests for our sys.path changes
As these were previously written, they didn't actually prove that the original entry added by Python when memray was run had been removed, which is especially problematic since: python -m memray run -m somemodule would remove an entry added by Python and re-add that same entry. We had no proof this was actually running the expected code and making the expected changes. Use `python -c ...` when running `memray run -m` to exercise this better, since we can check that the (different) path that would be added by the interpreter has been replaced. Continue using `python -m memray` when running `memray run -c cmd` and `memray run script.py`, but have the script that gets run print the entirety of `sys.path` so we can make assertions about what isn't there as well as what is. Signed-off-by: Matt Wozniski <[email protected]>
1 parent ce382d6 commit 8b368a1

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

tests/integration/test_main.py

+22-35
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ def test_sys_manipulations_when_running_script(self, tmp_path):
268268
# GIVEN
269269
out_file = tmp_path / "result.bin"
270270
target_file = tmp_path / "test.py"
271-
target_file.write_text("import some_adjacent_module")
272-
other_file = tmp_path / "some_adjacent_module.py"
273-
other_file.write_text("import sys; print(sys.argv); print(sys.path[0])")
271+
target_file.write_text("import json, sys; print(json.dumps(sys.path))")
274272

275273
# WHEN
276274
proc = subprocess.run(
@@ -292,57 +290,48 @@ def test_sys_manipulations_when_running_script(self, tmp_path):
292290
)
293291

294292
# THEN
295-
assert proc.returncode == 0
296-
argv, path0 = proc.stdout.splitlines()
297-
assert argv == repr([str(target_file), "some", "provided args"])
298-
assert path0 == str(tmp_path)
299293
assert out_file.exists()
294+
assert proc.returncode == 0
295+
# Running `python -m` put cwd in sys.path; ensure we replaced it.
296+
path = json.loads(proc.stdout)
297+
assert os.getcwd() not in path
298+
assert str(tmp_path) in path
300299

301300
def test_sys_manipulations_when_running_module(self, tmp_path):
302301
# GIVEN
303302
out_file = tmp_path / "result.bin"
304-
target_file = tmp_path / "test.py"
305-
target_file.write_text("import some_adjacent_module")
306-
other_file = tmp_path / "some_adjacent_module.py"
307-
other_file.write_text("import sys; print(sys.argv); print(sys.path[0])")
308-
env = os.environ.copy()
309-
env["PYTHONPATH"] = str(tmp_path) + ":" + os.environ.get("PYTHONPATH", "")
310303

311304
# WHEN
312305
proc = subprocess.run(
313306
[
314307
sys.executable,
315-
"-m",
316-
"memray",
308+
"-c",
309+
"import sys; from memray.commands import main; sys.exit(main())",
317310
"run",
318311
"--quiet",
319312
"--output",
320313
str(out_file),
321314
"-m",
322-
"test",
323-
"some",
324-
"provided args",
315+
"site",
325316
],
326317
check=True,
327318
capture_output=True,
328319
text=True,
329-
env=env,
330320
)
331321

332322
# THEN
333-
assert proc.returncode == 0
334-
argv, path0 = proc.stdout.splitlines()
335-
assert argv == repr([str(target_file), "some", "provided args"])
336-
assert path0 == os.getcwd()
337323
assert out_file.exists()
324+
assert proc.returncode == 0
325+
# Running `python -c` put "" in sys.path; ensure we replaced it.
326+
path = eval(
327+
" ".join(line for line in proc.stdout.splitlines() if line.startswith(" "))
328+
)
329+
assert "" not in path
330+
assert os.getcwd() in path
338331

339332
def test_sys_manipulations_when_running_cmd(self, tmp_path):
340333
# GIVEN
341334
out_file = tmp_path / "result.bin"
342-
other_file = tmp_path / "some_adjacent_module.py"
343-
other_file.write_text("import sys; print(sys.argv); print(sys.path[0])")
344-
env = os.environ.copy()
345-
env["PYTHONPATH"] = str(tmp_path) + ":" + os.environ.get("PYTHONPATH", "")
346335

347336
# WHEN
348337
proc = subprocess.run(
@@ -355,22 +344,20 @@ def test_sys_manipulations_when_running_cmd(self, tmp_path):
355344
"--output",
356345
str(out_file),
357346
"-c",
358-
"import some_adjacent_module",
359-
"some",
360-
"provided args",
347+
"import json, sys; print(json.dumps(sys.path))",
361348
],
362349
check=True,
363350
capture_output=True,
364351
text=True,
365-
env=env,
366352
)
367353

368354
# THEN
369-
assert proc.returncode == 0
370-
argv, path0 = proc.stdout.splitlines()
371-
assert argv == repr(["-c", "some", "provided args"])
372-
assert path0 == ""
373355
assert out_file.exists()
356+
assert proc.returncode == 0
357+
# Running `python -m` put cwd in sys.path; ensure we replaced it.
358+
path = json.loads(proc.stdout)
359+
assert os.getcwd() not in path
360+
assert "" in path
374361

375362
@pytest.mark.parametrize("option", [None, "--live", "--live-remote"])
376363
def test_run_file_that_is_not_python(self, capsys, option):

0 commit comments

Comments
 (0)