Commit 582ed043 authored by Guido Trotter's avatar Guido Trotter
Browse files

Remove oneline= parameter from utils.ReadFile

This partially reverts commit b774bb10

.
Unittests unrelated to that particular functionality but introduced in
that commit are left untouched. Since the temporary directory is now
only used to check for a non-existing file, it is removed as well, and
a convenient very-very-unlikely-to-exist file is used instead.
Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent a425810f
......@@ -1383,28 +1383,21 @@ def EnsureDirs(dirs):
raise errors.GenericError("%s is not a directory" % dir_name)
def ReadFile(file_name, size=-1, oneline=False):
def ReadFile(file_name, size=-1):
"""Reads a file.
@type size: int
@param size: Read at most size bytes (if negative, entire file)
@type oneline: bool
@param oneline: Whether to read only one line (newline char is not included)
@rtype: str
@return: the (possibly partial) content of the file
"""
f = open(file_name, "r")
try:
if oneline:
data = f.readline(size).rstrip("\r\n")
else:
data = f.read(size)
return f.read(size)
finally:
f.close()
return data
def WriteFile(file_name, fn=None, data=None,
mode=None, uid=-1, gid=-1,
......
......@@ -518,16 +518,6 @@ class TestMatchNameComponent(unittest.TestCase):
class TestReadFile(testutils.GanetiTestCase):
def setUp(self):
testutils.GanetiTestCase.setUp(self)
self.tmpdir = tempfile.mkdtemp()
self.fname = utils.PathJoin(self.tmpdir, "data1")
def tearDown(self):
testutils.GanetiTestCase.tearDown(self)
shutil.rmtree(self.tmpdir)
def testReadAll(self):
data = utils.ReadFile(self._TestDataFilename("cert1.pem"))
......@@ -546,51 +536,9 @@ class TestReadFile(testutils.GanetiTestCase):
h.update(data)
self.assertEqual(h.hexdigest(), "893772354e4e690b9efd073eed433ce7")
def testReadOneline(self):
data = utils.ReadFile(self._TestDataFilename("cert1.pem"),
oneline=True)
self.assertEqual(len(data), 27)
self.assertEqual(data, "-----BEGIN CERTIFICATE-----")
def testReadOnelineSize(self):
dummydata = (1024 * "Hello World! ")
self.assertFalse(set("\r\n") & set(dummydata))
utils.WriteFile(self.fname, data=dummydata)
data = utils.ReadFile(self.fname, oneline=True, size=555)
self.assertEqual(len(data), 555)
self.assertEqual(data, dummydata[:555])
self.assertFalse(set("\r\n") & set(data))
def testReadOnelineSize2(self):
for end in ["\n", "\r\n"]:
dummydata = (1024 * ("Hello World%s" % end))
self.assert_(set("\r\n") & set(dummydata))
utils.WriteFile(self.fname, data=dummydata)
data = utils.ReadFile(self.fname, oneline=True, size=555)
self.assertEqual(len(data), len("Hello World"))
self.assertEqual(data, dummydata[:11])
self.assertFalse(set("\r\n") & set(data))
def testReadOnelineWhitespace(self):
for ws in [" ", "\t", "\t\t \t", "\t "]:
dummydata = (1024 * ("Foo bar baz %s\n" % ws))
self.assert_(set("\r\n") & set(dummydata))
utils.WriteFile(self.fname, data=dummydata)
data = utils.ReadFile(self.fname, oneline=True, size=555)
explen = len("Foo bar baz ") + len(ws)
self.assertEqual(len(data), explen)
self.assertEqual(data, dummydata[:explen])
self.assertFalse(set("\r\n") & set(data))
def testError(self):
self.assertRaises(EnvironmentError, utils.ReadFile,
utils.PathJoin(self.tmpdir, "does-not-exist"))
"/dev/null/does-not-exist")
class TestTimestampForFilename(unittest.TestCase):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment