blob: bd8c8b0534e14ec8cb7c88ecdb5aa15d0a7e0be2 [file] [log] [blame]
Antoine Musso45dd2cb2014-01-29 17:17:43 +01001# Copyright 2014 Antoine "hashar" Musso
2# Copyright 2014 Wikimedia Foundation Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Antoine Musso45dd2cb2014-01-29 17:17:43 +010016import testtools
17from zuul.lib.clonemapper import CloneMapper
18
Antoine Musso45dd2cb2014-01-29 17:17:43 +010019
20class TestCloneMapper(testtools.TestCase):
21
22 def test_empty_mapper(self):
23 """Given an empty map, the slashes in project names are directory
24 separators"""
25 cmap = CloneMapper(
26 {},
27 [
28 'project1',
29 'plugins/plugin1'
30 ])
31
32 self.assertEqual(
33 {'project1': '/basepath/project1',
34 'plugins/plugin1': '/basepath/plugins/plugin1'},
35 cmap.expand('/basepath')
36 )
37
38 def test_map_to_a_dot_dir(self):
39 """Verify we normalize path, hence '.' refers to the basepath"""
40 cmap = CloneMapper(
41 [{'name': 'mediawiki/core', 'dest': '.'}],
42 ['mediawiki/core'])
43 self.assertEqual(
44 {'mediawiki/core': '/basepath'},
45 cmap.expand('/basepath'))
46
47 def test_map_using_regex(self):
48 """One can use regex in maps and use \\1 to forge the directory"""
49 cmap = CloneMapper(
50 [{'name': 'plugins/(.*)', 'dest': 'project/plugins/\\1'}],
51 ['plugins/PluginFirst'])
52 self.assertEqual(
53 {'plugins/PluginFirst': '/basepath/project/plugins/PluginFirst'},
54 cmap.expand('/basepath'))
55
56 def test_map_discarding_regex_group(self):
57 cmap = CloneMapper(
58 [{'name': 'plugins/(.*)', 'dest': 'project/'}],
59 ['plugins/Plugin_1'])
60 self.assertEqual(
61 {'plugins/Plugin_1': '/basepath/project'},
62 cmap.expand('/basepath'))
63
64 def test_cant_dupe_destinations(self):
65 """We cant clone multiple projects in the same directory"""
66 cmap = CloneMapper(
67 [{'name': 'plugins/(.*)', 'dest': 'catchall/'}],
68 ['plugins/plugin1', 'plugins/plugin2']
69 )
70 self.assertRaises(Exception, cmap.expand, '/basepath')
71
72 def test_map_with_dot_and_regex(self):
73 """Combining relative path and regex"""
74 cmap = CloneMapper(
75 [{'name': 'plugins/(.*)', 'dest': './\\1'}],
76 ['plugins/PluginInBasePath'])
77 self.assertEqual(
78 {'plugins/PluginInBasePath': '/basepath/PluginInBasePath'},
79 cmap.expand('/basepath'))