1#!/usr/bin/env python
2# Copyright 2013 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.
5
6import unittest
7
8from test_file_system import TestFileSystem
9
10file_system = TestFileSystem({
11  'file.txt': '',
12  'templates': {
13    'README': '',
14    'public': {
15      'apps': {
16        '404.html': '',
17        'a11y.html': ''
18      },
19      'extensions': {
20        '404.html': '',
21        'cookies.html': ''
22      },
23      'redirects.json': 'redirect'
24    },
25    'json': {
26      'manifest.json': 'manifest'
27    }
28  }
29})
30
31class FileSystemTest(unittest.TestCase):
32  def testWalk(self):
33    expected_files = [
34      '^/file.txt',
35      'templates/README',
36      'templates/public/apps/404.html',
37      'templates/public/apps/a11y.html',
38      'templates/public/extensions/404.html',
39      'templates/public/extensions/cookies.html',
40      'templates/public/redirects.json',
41      'templates/json/manifest.json'
42    ]
43
44    expected_dirs = [
45      '^/templates/',
46      'templates/public/',
47      'templates/public/apps/',
48      'templates/public/extensions/',
49      'templates/json/'
50    ]
51
52    all_files = []
53    all_dirs = []
54    for root, dirs, files in file_system.Walk(''):
55      if not root: root = '^'
56      all_files += [root + '/' + name for name in files]
57      all_dirs += [root + '/' + name for name in dirs]
58
59    self.assertEqual(sorted(expected_files), sorted(all_files))
60    self.assertEqual(sorted(expected_dirs), sorted(all_dirs))
61
62  def testSubWalk(self):
63    expected_files = set([
64      '/redirects.json',
65      'apps/404.html',
66      'apps/a11y.html',
67      'extensions/404.html',
68      'extensions/cookies.html'
69    ])
70
71    all_files = set()
72    for root, dirs, files in file_system.Walk('templates/public/'):
73      all_files.update(root + '/' + name for name in files)
74
75    self.assertEqual(expected_files, all_files)
76
77  def testExists(self):
78    def exists(path):
79      return file_system.Exists(path).Get()
80
81    # Root directory.
82    self.assertTrue(exists(''))
83
84    # Directories (are not files).
85    self.assertFalse(exists('templates'))
86    self.assertTrue(exists('templates/'))
87    self.assertFalse(exists('templates/public'))
88    self.assertTrue(exists('templates/public/'))
89    self.assertFalse(exists('templates/public/apps'))
90    self.assertTrue(exists('templates/public/apps/'))
91
92    # Files (are not directories).
93    self.assertTrue(exists('file.txt'))
94    self.assertFalse(exists('file.txt/'))
95    self.assertTrue(exists('templates/README'))
96    self.assertFalse(exists('templates/README/'))
97    self.assertTrue(exists('templates/public/redirects.json'))
98    self.assertFalse(exists('templates/public/redirects.json/'))
99    self.assertTrue(exists('templates/public/apps/a11y.html'))
100    self.assertFalse(exists('templates/public/apps/a11y.html/'))
101
102
103if __name__ == '__main__':
104  unittest.main()
105