blob: fa65616d9a32bc24c62c53f930ab54d06265539c [file] [log] [blame]
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Test Utils library."""
from __future__ import print_function
import os
from chromite.lib import cros_test_lib
from chromite.lib.paygen import utils
class TestUtils(cros_test_lib.TempDirTestCase):
"""Test utils methods."""
def testCreateTmpInvalidPath(self):
"""Test that we create a tmp eventually even with invalid paths."""
tmps = ['/usr/local/nope', '/tmp']
tmp = utils.CreateTmpDir(tmps=tmps)
def testCreateTmpRaiseException(self):
"""Test that we raise an exception when we do not have enough space."""
self.assertRaises(utils.UnableToCreateTmpDir, utils.CreateTmpDir,
minimum_size=2 ** 50)
def testCreateTempFileWithContents(self):
"""Verify that we create a temp file with the right message in it."""
message = 'Test Message With Rocks In'
# Create the temp file.
with utils.CreateTempFileWithContents(message) as temp_file:
temp_name =
# Verify the name is valid.
# Verify it has the right contents
with open(temp_name, 'r') as f:
contents = f.readlines()
self.assertEqual([message], contents)
# Verify the temp file goes away when we close it.
# pylint: disable=E1101
def testListdirFullpath(self):
file_a = os.path.join(self.tempdir, 'a')
file_b = os.path.join(self.tempdir, 'b')
with file(file_a, 'w+'):
with file(file_b, 'w+'):
[file_a, file_b])