|
17 | 17 | # limitations under the License.
|
18 | 18 | """Test execution of commands in parallel."""
|
19 | 19 |
|
20 |
| -import os |
21 | 20 | import subprocess
|
22 | 21 | import sys
|
| 22 | +import textwrap |
23 | 23 | import time
|
24 | 24 | from pathlib import Path
|
25 | 25 |
|
26 |
| -import pytest |
| 26 | +from tests.utils import write_and_commit_file |
27 | 27 |
|
28 | 28 |
|
29 | 29 | def test_run_in_isolation(runner, project, client, run, subdirectory):
|
@@ -51,61 +51,51 @@ def test_run_in_isolation(runner, project, client, run, subdirectory):
|
51 | 51 | assert client.repository.head.commit.hexsha != head
|
52 | 52 |
|
53 | 53 |
|
54 |
| -@pytest.mark.skip(reason="FIXME: Isolation requires merging the database metadata.") |
55 |
| -def test_file_modification_during_run( |
56 |
| - tmpdir, runner, project, client, run, subdirectory, no_lfs_size_limit, no_lfs_warning |
57 |
| -): |
| 54 | +def test_file_modification_during_run(tmp_path, runner, client, subdirectory, no_lfs_size_limit, no_lfs_warning): |
58 | 55 | """Test run in isolation."""
|
59 | 56 | script = client.path / "script.py"
|
60 | 57 | output = client.path / "output"
|
61 |
| - lock = Path(str(tmpdir.join("lock"))) |
62 |
| - |
63 |
| - with client.commit(): |
64 |
| - script.write_text( |
65 |
| - "import os, time, sys\n" |
66 |
| - 'open("{lock}", "a")\n' |
67 |
| - 'while os.path.exists("{lock}"):\n' |
68 |
| - " time.sleep(1)\n" |
69 |
| - "sys.stdout.write(sys.stdin.read())\n" |
70 |
| - "sys.stdout.flush()\n".format(lock=str(lock)) |
71 |
| - ) |
72 |
| - |
73 |
| - prefix = [ |
74 |
| - sys.executable, |
75 |
| - "-m", |
76 |
| - "renku.ui.cli", |
77 |
| - "run", |
78 |
| - "--isolation", |
79 |
| - ] |
80 |
| - cmd = ["python", os.path.relpath(script, os.getcwd())] |
81 |
| - |
82 |
| - previous = client.repository.head.commit |
| 58 | + lock_file = tmp_path / "lock" |
| 59 | + |
| 60 | + write_and_commit_file( |
| 61 | + client.repository, |
| 62 | + script, |
| 63 | + textwrap.dedent( |
| 64 | + f""" |
| 65 | + import os, time, sys |
| 66 | + open("{lock_file}", "a") |
| 67 | + while os.path.exists("{lock_file}"): |
| 68 | + time.sleep(0.1) |
| 69 | + sys.stdout.write(sys.stdin.read()) |
| 70 | + sys.stdout.flush() |
| 71 | + """ |
| 72 | + ), |
| 73 | + ) |
83 | 74 |
|
84 | 75 | with output.open("wb") as stdout:
|
85 |
| - process = subprocess.Popen(prefix + cmd, stdin=subprocess.PIPE, stdout=stdout) |
| 76 | + command = [sys.executable, "-m", "renku.ui.cli", "run", "--isolation", "--", "python", script] |
| 77 | + process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=stdout) |
86 | 78 |
|
87 |
| - while not lock.exists() and process.poll() is None: |
88 |
| - time.sleep(1) |
| 79 | + while not lock_file.exists() and process.poll() is None: |
| 80 | + time.sleep(0.1) |
89 | 81 |
|
90 | 82 | assert process.poll() is None, "Subprocess exited prematurely"
|
91 | 83 |
|
92 |
| - with script.open("w") as fp: |
93 |
| - fp.write('print("edited")') |
| 84 | + # NOTE: Modify ``script.py`` in the current worktree |
| 85 | + script.write_text("print('edited')") |
94 | 86 |
|
95 |
| - lock.unlink() |
| 87 | + # NOTE: Signal the isolated run to continue |
| 88 | + lock_file.unlink() |
96 | 89 |
|
97 | 90 | process.communicate(input=b"test")
|
98 | 91 | assert 0 == process.wait()
|
99 | 92 |
|
100 |
| - with output.open("r") as fp: |
101 |
| - assert "test" == fp.read().strip() |
| 93 | + # NOTE: ``script.py`` is modified in the current worktree |
| 94 | + assert {"script.py"} == {c.a_path for c in client.repository.unstaged_changes} |
102 | 95 |
|
103 |
| - # TODO: Make sure that this test fails before fixing it |
104 |
| - # NOTE: Calculate the diff between HEAD and previous commit; this is not the exact same diff but should be ok |
105 |
| - diff = [ |
106 |
| - c |
107 |
| - for commit in client.repository.iterate_commits(revision=f"{previous.hexsha}..HEAD") |
108 |
| - for c in commit.get_changes() |
109 |
| - ] |
110 |
| - modifications = [modification for modification in diff if modification.change_type == "M"] |
111 |
| - assert 0 == len(modifications), f"{[(f.a_path, f.change_type) for f in diff]}" |
| 96 | + # NOTE: Isolated run finished with the expected result |
| 97 | + assert "test" == output.read_text().strip() |
| 98 | + # NOTE: Isolated run committed its results |
| 99 | + committed_changed_files_in_run = {c.a_path for c in client.repository.head.commit.get_changes()} |
| 100 | + assert "output" in committed_changed_files_in_run |
| 101 | + assert "script.py" not in committed_changed_files_in_run |
0 commit comments