replace module unit test coverage
Also fixed file closing after reading. Change-Id: I8485fd35a2c7346ef2d38ffd7278fdaab6dc57ad Partial-Bug: #1365391
This commit is contained in:
parent
827363920d
commit
8973df1bfc
@ -16,7 +16,8 @@ import re
|
||||
|
||||
|
||||
def replaceInFile(filename, orig, new):
|
||||
lines = open(filename).readlines()
|
||||
with open(filename) as f:
|
||||
lines = f.readlines()
|
||||
for lineno, line in enumerate(lines):
|
||||
lines[lineno] = re.sub(orig, new, line)
|
||||
with open(filename, 'w') as f:
|
||||
|
47
fuelmenu/tests/test_common_replace.py
Normal file
47
fuelmenu/tests/test_common_replace.py
Normal file
@ -0,0 +1,47 @@
|
||||
# Copyright 2016 Mirantis, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
import unittest
|
||||
|
||||
from fuelmenu.common import replace
|
||||
|
||||
|
||||
def custom_mock_open(lines):
|
||||
"""Customizing mock open
|
||||
|
||||
:param lines: list of lines
|
||||
:return: mock object
|
||||
"""
|
||||
m_open = mock.mock_open()
|
||||
m_open.return_value.readlines.return_value = [
|
||||
'{}\n'.format(line) for line in lines
|
||||
]
|
||||
return m_open
|
||||
|
||||
|
||||
class TestReplace(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super(TestReplace, self).setUp()
|
||||
self.data = ['line1', 'line2', 'line3']
|
||||
|
||||
def test_replace(self):
|
||||
filename = 'filename'
|
||||
with mock.patch('__builtin__.open', custom_mock_open(self.data)) \
|
||||
as m_open:
|
||||
replace.replaceInFile(filename, 'line2', 'line4')
|
||||
self.assertEqual(m_open.call_args_list,
|
||||
[mock.call(filename), mock.call(filename, 'w')])
|
||||
m_open.return_value.write.assert_called_once_with(
|
||||
'line1\nline4\nline3\n')
|
Loading…
Reference in New Issue
Block a user