Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mock away the copier module on linux
Browse files Browse the repository at this point in the history
hukkin committed Jan 18, 2025
1 parent f9d2419 commit 7020d89
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions tests/test_em.py
Original file line number Diff line number Diff line change
@@ -2,11 +2,36 @@

import argparse
import random
import sys
from unittest.mock import MagicMock, call, patch

import pytest

from em_keyboard import cli, copier # type: ignore[import-untyped]
import em_keyboard # type: ignore[import-untyped]

if sys.platform == "linux" and em_keyboard.copier:

class MockCopier:

Check warning on line 14 in tests/test_em.py

Codecov / codecov/patch

tests/test_em.py#L14

Added line #L14 was not covered by tests
"""A mock `copier` module.
On Linux, it seems difficult to have the `copier` module run
successfully in tox and CI environments, so mock it away,
for now.
The problems to solve, if undoing this mocking probably include:
- pytest and/or tox capturing stderr/stdout/stdin
- using `allowlist_externals` tox conf to allow the various
clipboard tools (xclip, wl-copy, wl-paste etc.)
- CI environment not having a clipboard
Note that this list mostly consists of guesses by someone
who doesn't understand the problem very well :)
"""

def copy(self, s: str) -> None:
pass

Check warning on line 32 in tests/test_em.py

Codecov / codecov/patch

tests/test_em.py#L31-L32

Added lines #L31 - L32 were not covered by tests

em_keyboard.copier = MockCopier()

Check warning on line 34 in tests/test_em.py

Codecov / codecov/patch

tests/test_em.py#L34

Added line #L34 was not covered by tests


@pytest.mark.parametrize(
@@ -28,10 +53,10 @@ def test_star(mock_print: MagicMock, mock_argparse: MagicMock, test_name: str) -

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
if copier:
if em_keyboard.copier:
mock_print.assert_called_once_with("Copied! ⭐")
else:
mock_print.assert_called_once_with("⭐")
@@ -48,7 +73,7 @@ def test_not_found(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
)

with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
mock_print.assert_called_once_with("")
@@ -66,7 +91,7 @@ def test_no_copy(mock_print: MagicMock, mock_argparse: MagicMock) -> None:

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
mock_print.assert_called_once_with("⭐")
@@ -89,7 +114,7 @@ def test_search_star(mock_print: MagicMock, mock_argparse: MagicMock) -> None:

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
for arg in expected:
@@ -110,10 +135,10 @@ def test_search_single_result_is_copied(

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
if copier:
if em_keyboard.copier:
mock_print.assert_called_once_with("Copied! 🇺🇦 flag_ukraine")
else:
mock_print.assert_called_once_with("🇺🇦 flag_ukraine")
@@ -131,7 +156,7 @@ def test_search_not_found(mock_print: MagicMock, mock_argparse: MagicMock) -> No

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
mock_print.assert_not_called()
@@ -150,10 +175,10 @@ def test_random(mock_print: MagicMock, mock_argparse: MagicMock) -> None:

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
if copier:
if em_keyboard.copier:
mock_print.assert_called_once_with("Copied! 😽 kissing_cat")
else:
mock_print.assert_called_once_with("😽 kissing_cat")
@@ -172,7 +197,7 @@ def test_random_no_copy(mock_print: MagicMock, mock_argparse: MagicMock) -> None

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
mock_print.assert_called_once_with("😽 kissing_cat")
@@ -190,7 +215,7 @@ def test_no_name(mock_print: MagicMock, mock_argparse: MagicMock) -> None:

# Act
with pytest.raises(SystemExit) as e:
cli()
em_keyboard.cli()

# Assert
mock_print.assert_not_called()

0 comments on commit 7020d89

Please sign in to comment.