提交 0c040df5 编写于 作者: C Caio Carrara

utils.genio: Refactor is_patch_in_file function and add tests

Signed-off-by: NCaio Carrara <ccarrara@redhat.com>
上级 3d18d952
......@@ -203,21 +203,22 @@ def write_file_or_fail(filename, data):
filename, details))
def find_pattern(filename, pattern):
def is_pattern_in_file(filename, pattern):
"""
Module for match pattern in a specified file
Check if a pattern matches in a specified file. If a non
regular file be informed a GenIOError will be raised.
:param filename: Path to file
:type filename: str
:param pattern: pattern that need to match in file
:param pattern: Pattern that need to match in file
:type pattern: str
:return: True when pattern matches in file if not
retun False
return False
:rtype: boolean
"""
if not os.path.isfile(filename):
raise GenIOError('invalid file %s to match pattern %s'
% (filename, pattern))
with open(filename, 'r') as content_file:
if re.search(pattern, content_file.read(), re.MULTILINE):
return True
......
import os
import tempfile
import unittest
from avocado.utils import genio
class TestGenio(unittest.TestCase):
def test_check_pattern_in_directory(self):
tempdirname = tempfile.mkdtemp()
with self.assertRaises(genio.GenIOError):
genio.is_pattern_in_file(tempdirname, 'something')
os.rmdir(tempdirname)
def test_check_simple_pattern_in_file_successfully(self):
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write('Hello World')
temp_file.seek(0)
self.assertTrue(genio.is_pattern_in_file(temp_file.name, 'Hello'))
def test_check_pattern_in_file_successfully(self):
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write('123')
temp_file.seek(0)
self.assertTrue(genio.is_pattern_in_file(temp_file.name, r'\d{3}'))
def test_check_pattern_in_file_unsuccessfully(self):
with tempfile.NamedTemporaryFile(mode='w') as temp_file:
temp_file.write('123')
temp_file.seek(0)
self.assertFalse(genio.is_pattern_in_file(temp_file.name, r'\D{3}'))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册