On Windows, there is no tzset function, skip on unittest.
[jelmer/subvertpy.git] / subvertpy / tests / test_properties.py
1 # Copyright (C) 2005-2007 Jelmer Vernooij <jelmer@samba.org>
2  
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU Lesser General Public License as published by
5 # the Free Software Foundation; either version 2.1 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU Lesser General Public License for more details.
12
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 """Subversion core library tests."""
17
18 import os
19 import time
20 from subvertpy import properties
21 from subvertpy.tests import TestCase
22
23 class TestProperties(TestCase):
24
25     def setUp(self):
26         super(TestProperties, self).setUp()
27
28     def test_time_from_cstring(self):
29         self.assertEquals(1225704780716938L, properties.time_from_cstring("2008-11-03T09:33:00.716938Z"))
30
31     def test_time_from_cstring_independent_from_dst(self):
32         old_tz = os.environ.get('TZ', None)
33         # On Windows, there is no tzset function, so skip this test.
34         if not hasattr(time, 'tzset'):
35             return
36
37         try:
38             # First specify a fixed timezone with known DST (late March to late October)
39             os.environ['TZ'] = 'Europe/London'
40             time.tzset()
41             # Now test a time within that DST
42             self.assertEquals(1275295762430000L, properties.time_from_cstring("2010-05-31T08:49:22.430000Z"))
43         finally:
44             if old_tz is None:
45                 del os.environ['TZ']
46             else:
47                 os.environ['TZ'] = old_tz
48             time.tzset()
49
50     def test_time_to_cstring(self):
51         self.assertEquals("2008-11-03T09:33:00.716938Z", properties.time_to_cstring(1225704780716938L))
52
53
54 class TestExternalsParser(TestCase):
55     def test_parse_root_relative_externals(self):
56         self.assertRaises(NotImplementedError, properties.parse_externals_description, 
57                     "http://example.com", "third-party/skins              ^/foo")
58
59     def test_parse_scheme_relative_externals(self):
60         self.assertRaises(NotImplementedError, properties.parse_externals_description, 
61                     "http://example.com", "third-party/skins              //foo")
62
63     def test_parse_externals(self):
64         self.assertEqual({
65             'third-party/sounds': (None, "http://sounds.red-bean.com/repos"),
66             'third-party/skins': (None, "http://skins.red-bean.com/repositories/skinproj"),
67             'third-party/skins/toolkit': (21, "http://svn.red-bean.com/repos/skin-maker")},
68             properties.parse_externals_description("http://example.com",
69 """third-party/sounds             http://sounds.red-bean.com/repos
70 third-party/skins              http://skins.red-bean.com/repositories/skinproj
71 third-party/skins/toolkit -r21 http://svn.red-bean.com/repos/skin-maker"""))
72
73     def test_parse_externals_space_revno(self):
74         self.assertEqual({
75             'third-party/skins/toolkit': (21, "http://svn.red-bean.com/repos/skin-maker")},
76             properties.parse_externals_description("http://example.com",
77 """third-party/skins/toolkit -r 21 http://svn.red-bean.com/repos/skin-maker"""))
78
79     def test_parse_externals_swapped(self):
80         self.assertEqual({'third-party/sounds': (None, "http://sounds.red-bean.com/repos")},
81             properties.parse_externals_description("http://example.com",
82 """http://sounds.red-bean.com/repos         third-party/sounds
83 """))
84
85     def test_parse_comment(self):
86         self.assertEqual({
87             'third-party/sounds': (None, "http://sounds.red-bean.com/repos")
88                 },
89             properties.parse_externals_description("http://example.com/",
90 """
91
92 third-party/sounds             http://sounds.red-bean.com/repos
93 #third-party/skins              http://skins.red-bean.com/repositories/skinproj
94 #third-party/skins/toolkit -r21 http://svn.red-bean.com/repos/skin-maker"""))
95
96     def test_parse_relative(self):
97         self.assertEqual({
98             'third-party/sounds': (None, "http://example.com/branches/other"),
99                 },
100             properties.parse_externals_description("http://example.com/trunk",
101 "third-party/sounds             ../branches/other"))
102
103     def test_parse_repos_root_relative(self):
104         self.assertEqual({
105             'third-party/sounds': (None, "http://example.com/bar/bla/branches/other"),
106                 },
107             properties.parse_externals_description("http://example.com/trunk",
108 "third-party/sounds             /bar/bla/branches/other"))
109
110     def test_parse_invalid_missing_url(self):
111         """No URL specified."""
112         self.assertRaises(properties.InvalidExternalsDescription, 
113             lambda: properties.parse_externals_description("http://example.com/", "bla"))
114             
115     def test_parse_invalid_too_much_data(self):
116         """No URL specified."""
117         self.assertRaises(properties.InvalidExternalsDescription, 
118             lambda: properties.parse_externals_description(None, "bla -R40 http://bla/"))
119  
120
121 class MergeInfoPropertyParserTests(TestCase):
122     def test_simple_range(self):
123         self.assertEquals({"/trunk": [(1, 2, True)]}, properties.parse_mergeinfo_property("/trunk:1-2\n"))
124
125     def test_simple_range_uninheritable(self):
126         self.assertEquals({"/trunk": [(1, 2, False)]}, properties.parse_mergeinfo_property("/trunk:1-2*\n"))
127
128     def test_simple_individual(self):
129         self.assertEquals({"/trunk": [(1, 1, True)]}, properties.parse_mergeinfo_property("/trunk:1\n"))
130
131     def test_empty(self):
132         self.assertEquals({}, properties.parse_mergeinfo_property(""))
133        
134
135 class MergeInfoPropertyCreatorTests(TestCase):
136     def test_simple_range(self):
137         self.assertEquals("/trunk:1-2\n", properties.generate_mergeinfo_property({"/trunk": [(1, 2, True)]}))
138
139     def test_simple_individual(self):
140         self.assertEquals("/trunk:1\n", properties.generate_mergeinfo_property({"/trunk": [(1, 1, True)]}))
141
142     def test_empty(self):
143         self.assertEquals("", properties.generate_mergeinfo_property({}))
144
145
146 class RevnumRangeTests(TestCase):
147     def test_add_revnum_empty(self):
148         self.assertEquals([(1, 1, True)], properties.range_add_revnum([], 1))
149
150     def test_add_revnum_before(self):
151         self.assertEquals([(2, 2, True), (8, 8, True)], properties.range_add_revnum([(2, 2, True)], 8))
152
153     def test_add_revnum_included(self):
154         self.assertEquals([(1, 3, True)], properties.range_add_revnum([(1, 3, True)], 2))
155         
156     def test_add_revnum_after(self):
157         self.assertEquals([(1, 3, True), (5, 5, True)], properties.range_add_revnum([(1, 3, True)], 5))
158
159     def test_add_revnum_extend_before(self):
160         self.assertEquals([(1, 3, True)], properties.range_add_revnum([(2, 3, True)], 1))
161
162     def test_add_revnum_extend_after(self):
163         self.assertEquals([(1, 3, True)], properties.range_add_revnum([(1, 2, True)], 3))
164
165     def test_revnum_includes_empty(self):
166         self.assertFalse(properties.range_includes_revnum([], 2))
167
168     def test_revnum_includes_oor(self):
169         self.assertFalse(properties.range_includes_revnum([(1, 3, True), (4, 5, True)], 10))
170
171     def test_revnum_includes_in(self):
172         self.assertTrue(properties.range_includes_revnum([(1, 3, True), (4, 5, True)], 2))
173
174
175 class MergeInfoIncludeTests(TestCase):
176     def test_includes_individual(self):
177         self.assertTrue(properties.mergeinfo_includes_revision({"/trunk": [(1, 1, True)]}, "/trunk", 1))
178
179     def test_includes_range(self):
180         self.assertTrue(properties.mergeinfo_includes_revision({"/trunk": [(1, 5, True)]}, "/trunk", 3))
181
182     def test_includes_invalid_path(self):
183         self.assertFalse(properties.mergeinfo_includes_revision({"/somepath": [(1, 5, True)]}, "/trunk", 3))
184
185     def test_includes_invalid_revnum(self):
186         self.assertFalse(properties.mergeinfo_includes_revision({"/trunk": [(1, 5, True)]}, "/trunk", 30))