Skip to content

Commit

Permalink
fix: force some test cases using mock skipped in py2 env
Browse files Browse the repository at this point in the history
  • Loading branch information
ssato committed Sep 26, 2020
1 parent c9af954 commit 8040230
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
8 changes: 7 additions & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
try:
from unittest import mock
except ImportError: # for python 2.7
import mock
mock = False

from tests.plugins import example

Expand All @@ -45,13 +45,19 @@ def load(self):


class PluginFunctionsTestCase(unittest.TestCase):

def test_validate_rule_module(self):
fun = yamllint.plugins.validate_rule_module
rule_mod = example.override_comments

self.assertFalse(fun(object()))
self.assertTrue(fun(rule_mod))

@unittest.skipIf(not mock, "unittest.mock is not available")
def test_validate_rule_module_using_mock(self):
fun = yamllint.plugins.validate_rule_module
rule_mod = example.override_comments

with mock.patch.object(rule_mod, "ID", False):
self.assertFalse(fun(rule_mod))

Expand Down
25 changes: 15 additions & 10 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
try:
from unittest import mock
except ImportError: # for python 2.7
import mock
mock = False

from tests.plugins import example

Expand All @@ -40,20 +40,25 @@ def test_get_rule_does_not_exist(self):
with self.assertRaises(ValueError):
yamllint.rules.get(RULE_NEVER_EXISTS)

@mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES)

@unittest.skipIf(not mock, "unittest.mock is not available")
class TestCaseUsingMock(unittest.TestCase):
"""Test cases for yamllint.rules.__init__.* using mock.
"""
def test_get_default_rule_with_plugins(self):
self.assertEqual(yamllint.rules.get(yamllint.rules.braces.ID),
yamllint.rules.braces)
with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
self.assertEqual(yamllint.rules.get(yamllint.rules.braces.ID),
yamllint.rules.braces)

@mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES)
def test_get_plugin_rules(self):
plugin_rule_id = example.override_comments.ID
plugin_rule_mod = example.override_comments

self.assertEqual(yamllint.rules.get(plugin_rule_id),
plugin_rule_mod)
with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
self.assertEqual(yamllint.rules.get(plugin_rule_id),
plugin_rule_mod)

@mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES)
def test_get_rule_does_not_exist_with_plugins(self):
with self.assertRaises(ValueError):
yamllint.rules.get(RULE_NEVER_EXISTS)
with mock.patch.dict(yamllint.rules._EXTERNAL_RULES, PLUGIN_RULES):
with self.assertRaises(ValueError):
yamllint.rules.get(RULE_NEVER_EXISTS)

0 comments on commit 8040230

Please sign in to comment.