lorax/tests/pylorax/test_buildstamp.py
Alexander Todorov d71290153e Fix mocking the built-in open function for Python2
- fix import of mock module
- account for io.StringIO differences in Python 2
2018-06-11 16:54:57 -07:00

29 lines
1.1 KiB
Python

import io
import unittest
from mock import patch
from pylorax import buildstamp
class BuildStampTestCase(unittest.TestCase):
def setUp(self):
self.bstamp = buildstamp.BuildStamp(
'Lorax Tests',
'0.1',
'https://github.com/rhinstaller/lorax/issues',
True,
'noarch'
)
def test_write_produces_file_with_expected_content(self):
out_file = io.StringIO()
# io.StringIO.write accepts unicode,
# but on Python 2 BuildStamp.write() passes strings to this method
out_file.orig_write = out_file.write
out_file.write = lambda s: out_file.orig_write(unicode(s))
with patch.object(out_file, 'close'):
with patch('pylorax.buildstamp.open', create=True, return_value=out_file):
self.bstamp.write('/tmp/stamp.ini')
self.assertIn("[Main]\nProduct=Lorax Tests\nVersion=0.1\nBugURL=https://github.com/rhinstaller/lorax/issues\nIsFinal=True", out_file.getvalue())
self.assertIn("[Compose]\nLorax=devel", out_file.getvalue())