Skip to content

Commit 3096054

Browse files
authored
Bugfix for mila code in WSL (#131)
* Add test to reproduce issue Signed-off-by: Fabrice Normandin <[email protected]> * Bugfix for mila code in WSL Signed-off-by: Fabrice Normandin <[email protected]> * Fix issue in pre-commit hook - PyCQA/docformatter#289 - PyCQA/docformatter#287 Signed-off-by: Fabrice Normandin <[email protected]> * Remove unnecessary mock for RemoteV2 in test Signed-off-by: Fabrice Normandin <[email protected]> * Remove unnecessary xfail mark on Windows Signed-off-by: Fabrice Normandin <[email protected]> * Adjust the regression test files following changes Signed-off-by: Fabrice Normandin <[email protected]> * Add xfail for flaky integration test Signed-off-by: Fabrice Normandin <[email protected]> --------- Signed-off-by: Fabrice Normandin <[email protected]>
1 parent fee25dc commit 3096054

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- uses: actions/setup-python@v4
2626
with:
2727
python-version: '3.10'
28-
- run: pip install pre-commit
28+
- run: pip install "pre-commit<4.0.0"
2929
- run: pre-commit --version
3030
- run: pre-commit install
3131
- run: pre-commit run --all-files

milatools/cli/code.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
MilatoolsUserError,
1414
currently_in_a_test,
1515
internet_on_compute_nodes,
16+
running_inside_WSL,
1617
)
1718
from milatools.utils.compute_node import ComputeNode, salloc, sbatch
1819
from milatools.utils.disk_quota import check_disk_quota
@@ -193,6 +194,9 @@ async def launch_vscode_loop(code_command: str, compute_node: ComputeNode, path:
193194
f"ssh-remote+{compute_node.hostname}",
194195
path,
195196
)
197+
if running_inside_WSL():
198+
code_command_to_run = ("powershell.exe", *code_command_to_run)
199+
196200
await LocalV2.run_async(code_command_to_run, display=True)
197201
# TODO: BUG: This now requires two Ctrl+C's instead of one!
198202
console.print(

tests/cli/test_code.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Unit tests for the `milatools.cli.code` module.
2+
3+
TODO: There are quite a few tests in `tests/integration/test_code.py` that could be
4+
moved here, since some of them aren't exactly "integration" tests.
5+
"""
6+
7+
from unittest.mock import AsyncMock, Mock
8+
9+
import pytest
10+
11+
import milatools.cli.code
12+
import milatools.cli.utils
13+
from milatools.cli.utils import running_inside_WSL
14+
from milatools.utils.compute_node import ComputeNode
15+
from milatools.utils.local_v2 import LocalV2
16+
17+
18+
@pytest.fixture
19+
def pretend_to_be_in_WSL(
20+
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch
21+
):
22+
# By default, pretend to be in WSL. Indirect parametrization can be used to
23+
# overwrite this value for a given test (as is done below).
24+
in_wsl = getattr(request, "param", True)
25+
26+
_mock_running_inside_WSL = Mock(spec=running_inside_WSL, return_value=in_wsl)
27+
monkeypatch.setattr(
28+
milatools.cli.utils,
29+
running_inside_WSL.__name__, # type: ignore
30+
_mock_running_inside_WSL,
31+
)
32+
monkeypatch.setattr(
33+
milatools.cli.code,
34+
running_inside_WSL.__name__, # type: ignore
35+
_mock_running_inside_WSL,
36+
)
37+
return in_wsl
38+
39+
40+
@pytest.mark.parametrize("pretend_to_be_in_WSL", [True, False], indirect=True)
41+
@pytest.mark.asyncio
42+
async def test_code_from_WSL(
43+
monkeypatch: pytest.MonkeyPatch, pretend_to_be_in_WSL: bool
44+
):
45+
# Mock the LocalV2 class so that we can inspect the call to `LocalV2.run_async`.
46+
mock_localv2 = Mock(spec=LocalV2)
47+
monkeypatch.setattr(milatools.cli.code, LocalV2.__name__, mock_localv2)
48+
49+
await milatools.cli.code.launch_vscode_loop(
50+
"code", Mock(spec=ComputeNode, hostname="foo"), "/bob/path"
51+
)
52+
assert isinstance(mock_localv2.run_async, AsyncMock)
53+
mock_localv2.run_async.assert_called_once_with(
54+
(
55+
*(("powershell.exe",) if pretend_to_be_in_WSL else ()),
56+
"code",
57+
"--new-window",
58+
"--wait",
59+
"--remote",
60+
"ssh-remote+foo",
61+
"/bob/path",
62+
),
63+
display=True,
64+
)

tests/integration/test_code/test_code_mila__salloc_.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Checking disk quota on $HOME...
22
Disk usage: X / LIMIT GiB and X / LIMIT files
33
(mila) $ cd $SCRATCH && salloc --wckey=milatools_test --account=SLURM_ACCOUNT --nodes=1 --ntasks=1 --cpus-per-task=1 --mem=1G --time=0:05:00 --oversubscribe --job-name=mila-code
44
salloc: --------------------------------------------------------------------------------------------------
5-
salloc: # Using default long partition
5+
salloc: # Using default long-cpu partition (CPU-only)
66
salloc: --------------------------------------------------------------------------------------------------
77
salloc: Granted job allocation JOB_ID
88
Waiting for job JOB_ID to start.

tests/integration/test_code/test_code_mila__sbatch_.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Disk usage: X / LIMIT GiB and X / LIMIT files
55
JOB_ID
66

77
sbatch: --------------------------------------------------------------------------------------------------
8-
sbatch: # Using default long partition
8+
sbatch: # Using default long-cpu partition (CPU-only)
99
sbatch: --------------------------------------------------------------------------------------------------
1010

1111
(localhost) $ echo --new-window --wait --remote ssh-remote+COMPUTE_NODE $HOME/bob

tests/integration/test_sync_command.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
_install_vscode_extensions_task_function,
2020
sync_vscode_extensions,
2121
)
22+
from tests.integration.conftest import SLURM_CLUSTER
2223

2324
from ..cli.common import (
2425
requires_ssh_to_localhost,
@@ -28,6 +29,10 @@
2829
logger = get_logger(__name__)
2930

3031

32+
@pytest.mark.xfail(
33+
SLURM_CLUSTER == "mila",
34+
reason="`code-server` procs are killed on the login nodes of the Mila cluster.",
35+
)
3136
@pytest.mark.slow
3237
@pytest.mark.parametrize(
3338
"source",

0 commit comments

Comments
 (0)