summaryrefslogtreecommitdiff
path: root/systrace/catapult/common/py_vulcanize/py_vulcanize/fake_fs_unittest.py
blob: 08250138a5e397547d750c3de6e152f47f54ff36 (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
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import unittest

from py_vulcanize import fake_fs


class FakeFSUnittest(unittest.TestCase):

  def testBasic(self):
    fs = fake_fs.FakeFS()
    fs.AddFile('/blah/x', 'foobar')
    with fs:
      assert os.path.exists(os.path.normpath('/blah/x'))
      self.assertEquals(
          'foobar',
          open(os.path.normpath('/blah/x'), 'r').read())

  def testWithableOpen(self):
    fs = fake_fs.FakeFS()
    fs.AddFile('/blah/x', 'foobar')
    with fs:
      with open(os.path.normpath('/blah/x'), 'r') as f:
        self.assertEquals('foobar', f.read())

  def testWalk(self):
    fs = fake_fs.FakeFS()
    fs.AddFile('/x/w2/w3/z3.txt', '')
    fs.AddFile('/x/w/z.txt', '')
    fs.AddFile('/x/y.txt', '')
    fs.AddFile('/a.txt', 'foobar')
    with fs:
      gen = os.walk(os.path.normpath('/'))
      r = gen.next()
      self.assertEquals((os.path.normpath('/'), ['x'], ['a.txt']), r)

      r = gen.next()
      self.assertEquals((os.path.normpath('/x'), ['w', 'w2'], ['y.txt']), r)

      r = gen.next()
      self.assertEquals((os.path.normpath('/x/w'), [], ['z.txt']), r)

      r = gen.next()
      self.assertEquals((os.path.normpath('/x/w2'), ['w3'], []), r)

      r = gen.next()
      self.assertEquals((os.path.normpath('/x/w2/w3'), [], ['z3.txt']), r)

      self.assertRaises(StopIteration, gen.next)