2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Tests for pyauto_utils."""
17 class ExistingPathReplacerTest(unittest
.TestCase
):
18 """Tests for ExistingPathReplacer."""
21 self
._workdir
= tempfile
.mkdtemp()
22 self
.assertEqual(0, len(os
.listdir(self
._workdir
)))
25 shutil
.rmtree(self
._workdir
, ignore_errors
=True)
27 def _CreateFile(self
, path
):
32 def _IsOrigFile(self
, path
):
33 if not os
.path
.isfile(path
):
35 return open(path
).read() == 'magic'
37 def testNonExistingFile(self
):
38 """Test when the requested file does not exist."""
39 myfile
= os
.path
.join(self
._workdir
, 'myfile.txt')
40 self
.assertFalse(os
.path
.isfile(myfile
))
41 r
= pyauto_utils
.ExistingPathReplacer(myfile
, path_type
='file')
42 self
.assertTrue(os
.path
.isfile(myfile
))
44 self
.assertEqual(0, len(os
.listdir(self
._workdir
)))
46 def testExistingFile(self
):
47 """Test when the requested file exists."""
48 myfile
= os
.path
.join(self
._workdir
, 'myfile.txt')
49 self
._CreateFile
(myfile
)
50 self
.assertTrue(self
._IsOrigFile
(myfile
))
51 r
= pyauto_utils
.ExistingPathReplacer(myfile
, path_type
='file')
52 self
.assertFalse(self
._IsOrigFile
(myfile
))
53 self
.assertEqual(2, len(os
.listdir(self
._workdir
)))
55 self
.assertEqual(1, len(os
.listdir(self
._workdir
)))
56 self
.assertTrue(self
._IsOrigFile
(myfile
))
58 def testNonExistingDir(self
):
59 """Test when the requested dir does not exist."""
60 mydir
= os
.path
.join(self
._workdir
, 'mydir')
61 self
.assertFalse(os
.path
.isdir(mydir
))
62 r
= pyauto_utils
.ExistingPathReplacer(mydir
, path_type
='dir')
63 self
.assertTrue(os
.path
.isdir(mydir
))
64 self
.assertEqual(0, len(os
.listdir(mydir
)))
66 self
.assertFalse(os
.path
.isdir(mydir
))
68 def testExistingDir(self
):
69 """Test when the requested dir exists."""
70 # Create a dir with one file
71 mydir
= os
.path
.join(self
._workdir
, 'mydir')
73 self
.assertEqual(1, len(os
.listdir(self
._workdir
)))
74 myfile
= os
.path
.join(mydir
, 'myfile.txt')
75 open(myfile
, 'w').close()
76 self
.assertTrue(os
.path
.isfile(myfile
))
77 r
= pyauto_utils
.ExistingPathReplacer(mydir
)
78 self
.assertEqual(2, len(os
.listdir(self
._workdir
)))
79 self
.assertFalse(os
.path
.isfile(myfile
))
81 self
.assertEqual(1, len(os
.listdir(self
._workdir
)))
82 self
.assertTrue(os
.path
.isfile(myfile
))
85 if __name__
== '__main__':