test_zulint_custom_rules: Fix open mock.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-06-02 21:17:06 -07:00 committed by Tim Abbott
parent 10805abd6e
commit 5ab1ddbad9
1 changed files with 3 additions and 2 deletions

View File

@ -1,3 +1,4 @@
from io import StringIO
import os import os
from unittest.mock import patch from unittest.mock import patch
@ -42,14 +43,14 @@ class TestRuleList(TestCase):
pattern = rule['pattern'] pattern = rule['pattern']
for line in rule.get('good_lines', []): for line in rule.get('good_lines', []):
# create=True is superfluous when mocking built-ins in Python >= 3.5 # create=True is superfluous when mocking built-ins in Python >= 3.5
with patch('builtins.open', return_value=iter((line+'\n\n').splitlines()), create=True, autospec=True): with patch('builtins.open', return_value=StringIO(line + '\n\n'), create=True, autospec=True):
self.assertFalse(RuleList([], [rule]).custom_check_file('foo.bar', 'baz', ''), self.assertFalse(RuleList([], [rule]).custom_check_file('foo.bar', 'baz', ''),
"The pattern '{}' matched the line '{}' while it shouldn't.".format(pattern, line)) "The pattern '{}' matched the line '{}' while it shouldn't.".format(pattern, line))
for line in rule.get('bad_lines', []): for line in rule.get('bad_lines', []):
# create=True is superfluous when mocking built-ins in Python >= 3.5 # create=True is superfluous when mocking built-ins in Python >= 3.5
with patch('builtins.open', with patch('builtins.open',
return_value=iter((line+'\n\n').splitlines()), create=True, autospec=True), patch('builtins.print'): return_value=StringIO(line + '\n\n'), create=True, autospec=True), patch('builtins.print'):
filename = list(rule.get('include_only', {'foo.bar'}))[0] filename = list(rule.get('include_only', {'foo.bar'}))[0]
self.assertTrue(RuleList([], [rule]).custom_check_file(filename, 'baz', ''), self.assertTrue(RuleList([], [rule]).custom_check_file(filename, 'baz', ''),
"The pattern '{}' didn't match the line '{}' while it should.".format(pattern, line)) "The pattern '{}' didn't match the line '{}' while it should.".format(pattern, line))