1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import imp 6import os.path 7import shutil 8import sys 9import tempfile 10import unittest 11 12def _GetDirAbove(dirname): 13 """Returns the directory "above" this file containing |dirname| (which must 14 also be "above" this file).""" 15 path = os.path.abspath(__file__) 16 while True: 17 path, tail = os.path.split(path) 18 assert tail 19 if tail == dirname: 20 return path 21 22try: 23 imp.find_module("mojom") 24except ImportError: 25 sys.path.append(os.path.join(_GetDirAbove("pylib"), "pylib")) 26from mojom import fileutil 27 28 29class FileUtilTest(unittest.TestCase): 30 31 def testEnsureDirectoryExists(self): 32 """Test that EnsureDirectoryExists fuctions correctly.""" 33 34 temp_dir = tempfile.mkdtemp() 35 try: 36 self.assertTrue(os.path.exists(temp_dir)) 37 38 # Directory does not exist, yet. 39 full = os.path.join(temp_dir, "foo", "bar") 40 self.assertFalse(os.path.exists(full)) 41 42 # Create the directory. 43 fileutil.EnsureDirectoryExists(full) 44 self.assertTrue(os.path.exists(full)) 45 46 # Trying to create it again does not cause an error. 47 fileutil.EnsureDirectoryExists(full) 48 self.assertTrue(os.path.exists(full)) 49 50 # Bypass check for directory existence to tickle error handling that 51 # occurs in response to a race. 52 fileutil.EnsureDirectoryExists(full, always_try_to_create=True) 53 self.assertTrue(os.path.exists(full)) 54 finally: 55 shutil.rmtree(temp_dir) 56