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.
8 from app_yaml_helper
import AppYamlHelper
9 from extensions_paths
import SERVER2
10 from host_file_system_provider
import HostFileSystemProvider
11 from mock_file_system
import MockFileSystem
12 from object_store_creator
import ObjectStoreCreator
13 from test_file_system
import MoveTo
, TestFileSystem
14 from test_util
import DisableLogging
16 _ExtractVersion
, _IsGreater
, _GenerateAppYaml
= (
17 AppYamlHelper
.ExtractVersion
,
18 AppYamlHelper
.IsGreater
,
19 AppYamlHelper
.GenerateAppYaml
)
21 class AppYamlHelperTest(unittest
.TestCase
):
22 def testExtractVersion(self
):
23 def run_test(version
):
24 self
.assertEqual(version
, _ExtractVersion(_GenerateAppYaml(version
)))
43 def testIsGreater(self
):
44 def assert_is_greater(lhs
, rhs
):
45 self
.assertTrue(_IsGreater(lhs
, rhs
), '%s is not > %s' % (lhs
, rhs
))
46 self
.assertFalse(_IsGreater(rhs
, lhs
),
47 '%s should not be > %s' % (rhs
, lhs
))
48 assert_is_greater('0-0', '0')
49 assert_is_greater('0-0-0', '0')
50 assert_is_greater('0-0-0', '0-0')
51 assert_is_greater('1', '0')
52 assert_is_greater('1', '0-0')
53 assert_is_greater('1', '0-0-0')
54 assert_is_greater('1-0', '0-0')
55 assert_is_greater('1-0-0-0', '0-0-0')
56 assert_is_greater('2-0-12', '2-0-9')
57 assert_is_greater('2-0-12', '2-0-9-0')
58 assert_is_greater('2-0-12-0', '2-0-9')
59 assert_is_greater('2-0-12-0', '2-0-9-0')
60 assert_is_greater('2-1', '2-0-9')
61 assert_is_greater('2-1', '2-0-12')
62 assert_is_greater('2-1-0', '2-0-9')
63 assert_is_greater('2-1-0', '2-0-12')
64 assert_is_greater('3-1-0', '2-1')
65 assert_is_greater('3-1-0', '2-1-0')
66 assert_is_greater('3-1-0', '2-11-0')
67 assert_is_greater('3-1-3', '3-1-0')
68 assert_is_greater('3-12-0', '3-1-0')
69 assert_is_greater('3-12-0', '3-1-3')
70 assert_is_greater('3-12-0', '3-1-3-0')
72 @DisableLogging('warning')
73 def testInstanceMethods(self
):
75 'app.yaml': _GenerateAppYaml('1-0'),
76 'app_yaml_helper.py': 'Copyright notice etc'
80 # Pass a specific file system at head to the HostFileSystemProvider so that
81 # we know it's always going to be backed by a MockFileSystem. The Provider
82 # may decide to wrap it in caching etc.
83 file_system_at_head
= MockFileSystem(
84 TestFileSystem(test_data
, relative_to
=SERVER2
))
86 def apply_update(update
):
87 update
= MoveTo(SERVER2
, update
)
88 file_system_at_head
.Update(update
)
89 updates
.append(update
)
91 def host_file_system_constructor(branch
, commit
=None):
92 self
.assertEqual('master', branch
)
93 self
.assertTrue(commit
is not None)
94 return MockFileSystem
.Create(
95 TestFileSystem(test_data
, relative_to
=SERVER2
), updates
[:int(commit
)])
97 object_store_creator
= ObjectStoreCreator
.ForTest()
98 host_file_system_provider
= HostFileSystemProvider(
100 default_master_instance
=file_system_at_head
,
101 constructor_for_test
=host_file_system_constructor
)
102 helper
= AppYamlHelper(object_store_creator
, host_file_system_provider
)
104 def assert_is_up_to_date(version
):
105 self
.assertTrue(helper
.IsUpToDate(version
),
106 '%s is not up to date' % version
)
107 self
.assertRaises(ValueError,
108 helper
.GetFirstRevisionGreaterThan
, version
)
110 self
.assertEqual(0, helper
.GetFirstRevisionGreaterThan('0-5-0'))
111 assert_is_up_to_date('1-0-0')
112 assert_is_up_to_date('1-5-0')
116 'app.yaml': _GenerateAppYaml('1-5-0')
119 self
.assertEqual(0, helper
.GetFirstRevisionGreaterThan('0-5-0'))
120 self
.assertEqual(1, helper
.GetFirstRevisionGreaterThan('1-0-0'))
121 assert_is_up_to_date('1-5-0')
122 assert_is_up_to_date('2-5-0')
126 'app_yaml_helper.py': 'fixed a bug'
129 self
.assertEqual(0, helper
.GetFirstRevisionGreaterThan('0-5-0'))
130 self
.assertEqual(1, helper
.GetFirstRevisionGreaterThan('1-0-0'))
131 assert_is_up_to_date('1-5-0')
132 assert_is_up_to_date('2-5-0')
136 'app.yaml': _GenerateAppYaml('1-6-0')
139 self
.assertEqual(0, helper
.GetFirstRevisionGreaterThan('0-5-0'))
140 self
.assertEqual(1, helper
.GetFirstRevisionGreaterThan('1-0-0'))
141 self
.assertEqual(3, helper
.GetFirstRevisionGreaterThan('1-5-0'))
142 assert_is_up_to_date('2-5-0')
146 'app.yaml': _GenerateAppYaml('1-8-0')
150 'app.yaml': _GenerateAppYaml('2-0-0')
154 'app.yaml': _GenerateAppYaml('2-2-0')
158 'app.yaml': _GenerateAppYaml('2-4-0')
162 'app.yaml': _GenerateAppYaml('2-6-0')
165 self
.assertEqual(0, helper
.GetFirstRevisionGreaterThan('0-5-0'))
166 self
.assertEqual(1, helper
.GetFirstRevisionGreaterThan('1-0-0'))
167 self
.assertEqual(3, helper
.GetFirstRevisionGreaterThan('1-5-0'))
168 self
.assertEqual(5, helper
.GetFirstRevisionGreaterThan('1-8-0'))
169 self
.assertEqual(6, helper
.GetFirstRevisionGreaterThan('2-0-0'))
170 self
.assertEqual(6, helper
.GetFirstRevisionGreaterThan('2-1-0'))
171 self
.assertEqual(7, helper
.GetFirstRevisionGreaterThan('2-2-0'))
172 self
.assertEqual(7, helper
.GetFirstRevisionGreaterThan('2-3-0'))
173 self
.assertEqual(8, helper
.GetFirstRevisionGreaterThan('2-4-0'))
174 self
.assertEqual(8, helper
.GetFirstRevisionGreaterThan('2-5-0'))
175 assert_is_up_to_date('2-6-0')
176 assert_is_up_to_date('2-7-0')
178 if __name__
== '__main__':