From 5ab1ddbad9036746134b072b4005d9e7c8b907b5 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 2 Jun 2020 21:17:06 -0700 Subject: [PATCH] test_zulint_custom_rules: Fix open mock. Signed-off-by: Anders Kaseorg --- tools/tests/test_zulint_custom_rules.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/tests/test_zulint_custom_rules.py b/tools/tests/test_zulint_custom_rules.py index 8875db0979..86d26eb3ad 100644 --- a/tools/tests/test_zulint_custom_rules.py +++ b/tools/tests/test_zulint_custom_rules.py @@ -1,3 +1,4 @@ +from io import StringIO import os from unittest.mock import patch @@ -42,14 +43,14 @@ class TestRuleList(TestCase): pattern = rule['pattern'] for line in rule.get('good_lines', []): # 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', ''), "The pattern '{}' matched the line '{}' while it shouldn't.".format(pattern, line)) for line in rule.get('bad_lines', []): # 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), 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] self.assertTrue(RuleList([], [rule]).custom_check_file(filename, 'baz', ''), "The pattern '{}' didn't match the line '{}' while it should.".format(pattern, line))