Skip to content

Commit

Permalink
MAINT: mock slowest test. (#13885)
Browse files Browse the repository at this point in the history
The slowest test of our CI is ~3sec as it use subprocess os/system.
Mocking make it instantaneous.

With our big elements matrix in CI that should save us a bunch of time
in total from PR submission to green.
  • Loading branch information
Carreau committed Feb 9, 2023
2 parents 7dab272 + 8655912 commit 0694b08
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -17,6 +17,7 @@
import sys
import tempfile
import unittest
import pytest
from unittest import mock

from os.path import join
Expand Down Expand Up @@ -635,10 +636,23 @@ def test_control_c(self, *mocks):
)
self.assertEqual(ip.user_ns["_exit_code"], -signal.SIGINT)

def test_magic_warnings(self):
for magic_cmd in ("pip", "conda", "cd"):
with self.assertWarnsRegex(Warning, "You executed the system command"):
ip.system_raw(magic_cmd)

@pytest.mark.parametrize("magic_cmd", ["pip", "conda", "cd"])
def test_magic_warnings(magic_cmd):
if sys.platform == "win32":
to_mock = "os.system"
expected_arg, expected_kwargs = magic_cmd, dict()
else:
to_mock = "subprocess.call"
expected_arg, expected_kwargs = magic_cmd, dict(
shell=True, executable=os.environ.get("SHELL", None)
)

with mock.patch(to_mock, return_value=0) as mock_sub:
with pytest.warns(Warning, match=r"You executed the system command"):
ip.system_raw(magic_cmd)
mock_sub.assert_called_once_with(expected_arg, **expected_kwargs)


# TODO: Exit codes are currently ignored on Windows.
class TestSystemPipedExitCode(ExitCodeChecks):
Expand Down Expand Up @@ -1089,9 +1103,12 @@ def test_run_cell_asyncio_run():


def test_should_run_async():
assert not ip.should_run_async("a = 5")
assert ip.should_run_async("await x")
assert ip.should_run_async("import asyncio; await asyncio.sleep(1)")
assert not ip.should_run_async("a = 5", transformed_cell="a = 5")
assert ip.should_run_async("await x", transformed_cell="await x")
assert ip.should_run_async(
"import asyncio; await asyncio.sleep(1)",
transformed_cell="import asyncio; await asyncio.sleep(1)",
)


def test_set_custom_completer():
Expand Down

0 comments on commit 0694b08

Please sign in to comment.