1b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#!/usr/bin/env python
2b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
3b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
4b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)# found in the LICENSE file.
5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
6b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)from copy import deepcopy
7b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)from file_system import FileNotFoundError, StatInfo
8b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)from mock_file_system import MockFileSystem
9b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)from test_file_system import TestFileSystem
10b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)import unittest
11b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
12b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)_TEST_DATA = {
13b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  '404.html': '404.html contents',
14b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  'apps': {
15b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    'a11y.html': 'a11y.html contents',
16b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    'about_apps.html': 'about_apps.html contents',
17b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    'fakedir': {
18b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      'file.html': 'file.html contents'
19b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    }
20b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  },
21b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  'extensions': {
22b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    'activeTab.html': 'activeTab.html contents',
23b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    'alarms.html': 'alarms.html contents'
24b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
25b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
26b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
27b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)def _Get(fn):
28b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  '''Returns a function which calls Future.Get on the result of |fn|.
29b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  '''
30b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return lambda *args: fn(*args).Get()
31b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
32b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)class MockFileSystemTest(unittest.TestCase):
33b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  def testCheckAndReset(self):
34b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    fs = MockFileSystem(TestFileSystem(deepcopy(_TEST_DATA)))
35b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
36b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset())
37b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(read_count=1))
38b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(stat_count=1))
39b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    future = fs.ReadSingle('apps/')
41b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset(read_count=1))
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    future.Get()
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset(read_resolve_count=1))
44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(read_count=1))
45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset())
46b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    future = fs.ReadSingle('apps/')
48b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(read_count=2))
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    future.Get()
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(read_resolve_count=2))
51b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    fs.ReadSingle('extensions/').Get()
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    fs.ReadSingle('extensions/').Get()
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset(read_count=2, read_resolve_count=2))
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(read_count=2, read_resolve_count=2))
56b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset())
57b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    fs.ReadSingle('404.html').Get()
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset(read_count=1, read_resolve_count=1))
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    future = fs.Read(['notfound.html', 'apps/'])
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset(read_count=1))
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertRaises(FileNotFoundError, future.Get)
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset(read_resolve_count=0))
64b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
65b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    fs.Stat('404.html')
66b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    fs.Stat('404.html')
67b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    fs.Stat('apps/')
68b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(stat_count=42))
69b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertFalse(*fs.CheckAndReset(stat_count=42))
70b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset())
71b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    fs.ReadSingle('404.html').Get()
73b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    fs.Stat('404.html')
74b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    fs.Stat('apps/')
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertTrue(
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        *fs.CheckAndReset(read_count=1, read_resolve_count=1, stat_count=2))
77b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    self.assertTrue(*fs.CheckAndReset())
78b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  def testUpdates(self):
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    fs = MockFileSystem(TestFileSystem(deepcopy(_TEST_DATA)))
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
82a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('0', child_versions={
83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      '404.html': '0',
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'apps/': '0',
85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'extensions/': '0'
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }), fs.Stat(''))
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('0'), fs.Stat('404.html'))
88a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('0', child_versions={
89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'a11y.html': '0',
90a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'about_apps.html': '0',
91a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'fakedir/': '0',
92a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }), fs.Stat('apps/'))
934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertEqual('404.html contents', fs.ReadSingle('404.html').Get())
94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    fs.Update({
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      '404.html': 'New version!'
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    })
98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('1', child_versions={
100a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      '404.html': '1',
101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'apps/': '0',
102a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'extensions/': '0'
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }), fs.Stat(''))
104a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('1'), fs.Stat('404.html'))
105a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('0', child_versions={
106a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'a11y.html': '0',
107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'about_apps.html': '0',
108a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'fakedir/': '0',
109a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }), fs.Stat('apps/'))
1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertEqual('New version!', fs.ReadSingle('404.html').Get())
111a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
112a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    fs.Update({
113a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      '404.html': 'Newer version!',
114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'apps': {
115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        'fakedir': {
116a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          'file.html': 'yo'
117a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        }
118a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      }
119a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    })
120a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
121a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('2', child_versions={
122a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      '404.html': '2',
123a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'apps/': '2',
124a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'extensions/': '0'
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }), fs.Stat(''))
126a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('2'), fs.Stat('404.html'))
127a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('2', child_versions={
128a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'a11y.html': '0',
129a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'about_apps.html': '0',
130a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'fakedir/': '2',
131a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }), fs.Stat('apps/'))
132a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('0'), fs.Stat('apps/a11y.html'))
133a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('2', child_versions={
134a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'file.html': '2'
135a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }), fs.Stat('apps/fakedir/'))
136a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('2'), fs.Stat('apps/fakedir/file.html'))
137a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    self.assertEqual(StatInfo('0', child_versions={
138a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'activeTab.html': '0',
139a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      'alarms.html': '0'
140a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }), fs.Stat('extensions/'))
1414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertEqual('Newer version!', fs.ReadSingle('404.html').Get())
1424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertEqual('yo', fs.ReadSingle('apps/fakedir/file.html').Get())
143a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
144b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)if __name__ == '__main__':
145b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  unittest.main()
146