aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/third_party/pyfakefs/pyfakefs/fake_filesystem_glob_test.py
blob: b08f982068130e7611628007023340a67f607243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/env python
#
# Copyright 2009 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test for fake_filesystem_glob."""

import doctest
import sys
if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

import fake_filesystem
import fake_filesystem_glob


class FakeGlobUnitTest(unittest.TestCase):

  def setUp(self):
    self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/')
    self.glob = fake_filesystem_glob.FakeGlobModule(self.filesystem)
    directory = './xyzzy'
    self.filesystem.CreateDirectory(directory)
    self.filesystem.CreateDirectory('%s/subdir' % directory)
    self.filesystem.CreateDirectory('%s/subdir2' % directory)
    self.filesystem.CreateFile('%s/subfile' % directory)
    self.filesystem.CreateFile('[Temp]')

  def testGlobEmpty(self):
    self.assertEqual(self.glob.glob(''), [])

  def testGlobStar(self):
    self.assertEqual(['/xyzzy/subdir', '/xyzzy/subdir2', '/xyzzy/subfile'],
                     self.glob.glob('/xyzzy/*'))

  def testGlobExact(self):
    self.assertEqual(['/xyzzy'], self.glob.glob('/xyzzy'))
    self.assertEqual(['/xyzzy/subfile'], self.glob.glob('/xyzzy/subfile'))

  def testGlobQuestion(self):
    self.assertEqual(['/xyzzy/subdir', '/xyzzy/subdir2', '/xyzzy/subfile'],
                     self.glob.glob('/x?zz?/*'))

  def testGlobNoMagic(self):
    self.assertEqual(['/xyzzy'], self.glob.glob('/xyzzy'))
    self.assertEqual(['/xyzzy/subdir'], self.glob.glob('/xyzzy/subdir'))

  def testNonExistentPath(self):
    self.assertEqual([], self.glob.glob('nonexistent'))

  def testDocTest(self):
    self.assertFalse(doctest.testmod(fake_filesystem_glob)[0])

  def testMagicDir(self):
    self.assertEqual(['/[Temp]'], self.glob.glob('/*emp*'))

  def testRootGlob(self):
    self.assertEqual(['[Temp]', 'xyzzy'], self.glob.glob('*'))

  def testGlob1(self):
    self.assertEqual(['[Temp]'], self.glob.glob1('/', '*Tem*'))

  def testHasMagic(self):
    self.assertTrue(self.glob.has_magic('['))
    self.assertFalse(self.glob.has_magic('a'))


if __name__ == '__main__':
  unittest.main()