OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Tests for pyauto_utils.""" |
| 8 |
| 9 import glob |
| 10 import os |
| 11 import shutil |
| 12 import tempfile |
| 13 import unittest |
| 14 |
| 15 import pyauto_utils |
| 16 |
| 17 |
| 18 class ExistingPathReplacerTest(unittest.TestCase): |
| 19 """Tests for ExistingPathReplacer.""" |
| 20 |
| 21 def setUp(self): |
| 22 self._workdir = tempfile.mkdtemp() |
| 23 self.assertEqual(0, len(os.listdir(self._workdir))) |
| 24 |
| 25 def tearDown(self): |
| 26 shutil.rmtree(self._workdir, ignore_errors=True) |
| 27 |
| 28 def _CreateFile(self, path): |
| 29 fp = open(path, 'w') |
| 30 fp.write('magic') |
| 31 fp.close() |
| 32 |
| 33 def _IsOrigFile(self, path): |
| 34 if not os.path.isfile(path): |
| 35 return False |
| 36 return open(path).read() == 'magic' |
| 37 |
| 38 def testNonExistingFile(self): |
| 39 """Test when the requested file does not exist.""" |
| 40 myfile = os.path.join(self._workdir, 'myfile.txt') |
| 41 self.assertFalse(os.path.isfile(myfile)) |
| 42 r = pyauto_utils.ExistingPathReplacer(myfile, path_type='file') |
| 43 self.assertTrue(os.path.isfile(myfile)) |
| 44 del r |
| 45 self.assertEqual(0, len(os.listdir(self._workdir))) |
| 46 |
| 47 def testExistingFile(self): |
| 48 """Test when the requested file exists.""" |
| 49 myfile = os.path.join(self._workdir, 'myfile.txt') |
| 50 self._CreateFile(myfile) |
| 51 self.assertTrue(self._IsOrigFile(myfile)) |
| 52 r = pyauto_utils.ExistingPathReplacer(myfile, path_type='file') |
| 53 self.assertFalse(self._IsOrigFile(myfile)) |
| 54 self.assertEqual(2, len(os.listdir(self._workdir))) |
| 55 del r |
| 56 self.assertEqual(1, len(os.listdir(self._workdir))) |
| 57 self.assertTrue(self._IsOrigFile(myfile)) |
| 58 |
| 59 def testNonExistingDir(self): |
| 60 """Test when the requested dir does not exist.""" |
| 61 mydir = os.path.join(self._workdir, 'mydir') |
| 62 self.assertFalse(os.path.isdir(mydir)) |
| 63 r = pyauto_utils.ExistingPathReplacer(mydir, path_type='dir') |
| 64 self.assertTrue(os.path.isdir(mydir)) |
| 65 self.assertEqual(0, len(os.listdir(mydir))) |
| 66 del r |
| 67 self.assertFalse(os.path.isdir(mydir)) |
| 68 |
| 69 def testExistingDir(self): |
| 70 """Test when the requested dir exists.""" |
| 71 # Create a dir with one file |
| 72 mydir = os.path.join(self._workdir, 'mydir') |
| 73 os.makedirs(mydir) |
| 74 self.assertEqual(1, len(os.listdir(self._workdir))) |
| 75 myfile = os.path.join(mydir, 'myfile.txt') |
| 76 open(myfile, 'w').close() |
| 77 self.assertTrue(os.path.isfile(myfile)) |
| 78 r = pyauto_utils.ExistingPathReplacer(mydir) |
| 79 self.assertEqual(2, len(os.listdir(self._workdir))) |
| 80 self.assertFalse(os.path.isfile(myfile)) |
| 81 del r |
| 82 self.assertEqual(1, len(os.listdir(self._workdir))) |
| 83 self.assertTrue(os.path.isfile(myfile)) |
| 84 |
| 85 |
| 86 if __name__ == '__main__': |
| 87 unittest.main() |
OLD | NEW |