Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / subversion_file_system_test.py
blobc07dde0f51db90c4be80457c866850e88f514399
1 #!/usr/bin/env python
2 # Copyright (c) 2012 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 import json
7 import os
8 import sys
9 import unittest
11 from fake_url_fetcher import FakeUrlFetcher
12 from file_system import FileSystemError, StatInfo
13 from subversion_file_system import SubversionFileSystem
14 import test_util
16 _SHARED_FILE_SYSTEM_TEST_DATA = os.path.join(
17 sys.path[0], 'test_data', 'file_system')
18 _SUBVERSION_FILE_SYSTEM_TEST_DATA = os.path.join(
19 sys.path[0], 'test_data', 'subversion_file_system')
21 def _CreateSubversionFileSystem(path):
22 fetcher = FakeUrlFetcher(path)
23 return SubversionFileSystem(fetcher, fetcher, path), fetcher
25 class SubversionFileSystemTest(unittest.TestCase):
26 def testReadFiles(self):
27 expected = {
28 'test1.txt': 'test1\n',
29 'test2.txt': 'test2\n',
30 'test3.txt': 'test3\n',
32 file_system, fetcher = _CreateSubversionFileSystem(
33 _SHARED_FILE_SYSTEM_TEST_DATA)
34 read_future = file_system.Read(['test1.txt', 'test2.txt', 'test3.txt'])
35 self.assertTrue(*fetcher.CheckAndReset(async_count=3))
36 self.assertEqual(expected, read_future.Get())
37 self.assertTrue(*fetcher.CheckAndReset(async_resolve_count=3))
39 def testListDir(self):
40 expected = ['dir/'] + ['file%d.html' % i for i in range(7)]
41 file_system, fetcher = _CreateSubversionFileSystem(
42 _SHARED_FILE_SYSTEM_TEST_DATA)
43 list_future = file_system.ReadSingle('list/')
44 self.assertTrue(*fetcher.CheckAndReset(async_count=1))
45 self.assertEqual(expected, sorted(list_future.Get()))
46 self.assertTrue(*fetcher.CheckAndReset(async_resolve_count=1))
48 def testListSubDir(self):
49 expected = ['empty.txt'] + ['file%d.html' % i for i in range(3)]
50 file_system, fetcher = _CreateSubversionFileSystem(
51 _SHARED_FILE_SYSTEM_TEST_DATA)
52 list_future = file_system.ReadSingle('list/dir/')
53 self.assertTrue(*fetcher.CheckAndReset(async_count=1))
54 self.assertEqual(expected, sorted(list_future.Get()))
55 self.assertTrue(*fetcher.CheckAndReset(async_resolve_count=1))
57 def testDirStat(self):
58 file_system, fetcher = _CreateSubversionFileSystem(
59 _SHARED_FILE_SYSTEM_TEST_DATA)
60 stat_info = file_system.Stat('stat/')
61 self.assertTrue(*fetcher.CheckAndReset(sync_count=1))
62 expected = StatInfo(
63 '151113',
64 child_versions=json.loads(test_util.ReadFile('%s/stat_result.json' %
65 _SHARED_FILE_SYSTEM_TEST_DATA)))
66 self.assertEqual(expected, stat_info)
68 def testFileStat(self):
69 file_system, fetcher = _CreateSubversionFileSystem(
70 _SHARED_FILE_SYSTEM_TEST_DATA)
71 stat_info = file_system.Stat('stat/extension_api.h')
72 self.assertTrue(*fetcher.CheckAndReset(sync_count=1))
73 self.assertEqual(StatInfo('146163'), stat_info)
75 def testRevisions(self):
76 # This is a super hacky test. Record the path that was fetched then exit the
77 # test. Compare.
78 class ValueErrorFetcher(object):
79 def __init__(self):
80 self.last_fetched = None
82 def FetchAsync(self, path):
83 class ThrowsValueError(object):
84 def Get(self): raise ValueError()
85 self.last_fetched = path
86 return ThrowsValueError()
88 def Fetch(self, path, **kwargs):
89 self.last_fetched = path
90 raise ValueError()
92 file_fetcher = ValueErrorFetcher()
93 stat_fetcher = ValueErrorFetcher()
94 svn_path = 'svn:'
96 svn_file_system = SubversionFileSystem(file_fetcher,
97 stat_fetcher,
98 svn_path,
99 revision=42)
101 self.assertRaises(FileSystemError,
102 svn_file_system.ReadSingle('dir/file').Get)
103 self.assertEqual('dir/file?p=42', file_fetcher.last_fetched)
104 # Stat() will always stat directories.
105 self.assertRaises(FileSystemError, svn_file_system.Stat, 'dir/file')
106 self.assertEqual('dir?pathrev=42', stat_fetcher.last_fetched)
108 self.assertRaises(FileSystemError,
109 svn_file_system.ReadSingle('dir/').Get)
110 self.assertEqual('dir/?p=42', file_fetcher.last_fetched)
111 self.assertRaises(FileSystemError, svn_file_system.Stat, 'dir/')
112 self.assertEqual('dir?pathrev=42', stat_fetcher.last_fetched)
114 def testDirectoryVersionOnDeletion(self):
115 '''Tests the case when the most recent operation on a directory is the
116 deletion of a file. Here it is not enough to take the maximum version of all
117 files in the directory, as we used to, for obvious reasons.
119 file_system, _ = _CreateSubversionFileSystem(
120 _SUBVERSION_FILE_SYSTEM_TEST_DATA)
121 dir_stat = file_system.Stat('docs_public_extensions_214898/')
122 self.assertEqual('214692', dir_stat.version)
124 def testEmptyDirectory(self):
125 file_system, _ = _CreateSubversionFileSystem(
126 _SUBVERSION_FILE_SYSTEM_TEST_DATA)
127 dir_stat = file_system.Stat('api_icons_214898/')
128 self.assertEqual('193838', dir_stat.version)
129 self.assertEqual({}, dir_stat.child_versions)
131 if __name__ == '__main__':
132 unittest.main()