Antoine Musso | 45dd2cb | 2014-01-29 17:17:43 +0100 | [diff] [blame] | 1 | # 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 | |
| 16 | import logging |
| 17 | import testtools |
| 18 | from zuul.lib.clonemapper import CloneMapper |
| 19 | |
| 20 | logging.basicConfig(level=logging.DEBUG, |
| 21 | format='%(asctime)s %(name)-17s ' |
| 22 | '%(levelname)-8s %(message)s') |
| 23 | |
| 24 | |
| 25 | class TestCloneMapper(testtools.TestCase): |
| 26 | |
| 27 | def test_empty_mapper(self): |
| 28 | """Given an empty map, the slashes in project names are directory |
| 29 | separators""" |
| 30 | cmap = CloneMapper( |
| 31 | {}, |
| 32 | [ |
| 33 | 'project1', |
| 34 | 'plugins/plugin1' |
| 35 | ]) |
| 36 | |
| 37 | self.assertEqual( |
| 38 | {'project1': '/basepath/project1', |
| 39 | 'plugins/plugin1': '/basepath/plugins/plugin1'}, |
| 40 | cmap.expand('/basepath') |
| 41 | ) |
| 42 | |
| 43 | def test_map_to_a_dot_dir(self): |
| 44 | """Verify we normalize path, hence '.' refers to the basepath""" |
| 45 | cmap = CloneMapper( |
| 46 | [{'name': 'mediawiki/core', 'dest': '.'}], |
| 47 | ['mediawiki/core']) |
| 48 | self.assertEqual( |
| 49 | {'mediawiki/core': '/basepath'}, |
| 50 | cmap.expand('/basepath')) |
| 51 | |
| 52 | def test_map_using_regex(self): |
| 53 | """One can use regex in maps and use \\1 to forge the directory""" |
| 54 | cmap = CloneMapper( |
| 55 | [{'name': 'plugins/(.*)', 'dest': 'project/plugins/\\1'}], |
| 56 | ['plugins/PluginFirst']) |
| 57 | self.assertEqual( |
| 58 | {'plugins/PluginFirst': '/basepath/project/plugins/PluginFirst'}, |
| 59 | cmap.expand('/basepath')) |
| 60 | |
| 61 | def test_map_discarding_regex_group(self): |
| 62 | cmap = CloneMapper( |
| 63 | [{'name': 'plugins/(.*)', 'dest': 'project/'}], |
| 64 | ['plugins/Plugin_1']) |
| 65 | self.assertEqual( |
| 66 | {'plugins/Plugin_1': '/basepath/project'}, |
| 67 | cmap.expand('/basepath')) |
| 68 | |
| 69 | def test_cant_dupe_destinations(self): |
| 70 | """We cant clone multiple projects in the same directory""" |
| 71 | cmap = CloneMapper( |
| 72 | [{'name': 'plugins/(.*)', 'dest': 'catchall/'}], |
| 73 | ['plugins/plugin1', 'plugins/plugin2'] |
| 74 | ) |
| 75 | self.assertRaises(Exception, cmap.expand, '/basepath') |
| 76 | |
| 77 | def test_map_with_dot_and_regex(self): |
| 78 | """Combining relative path and regex""" |
| 79 | cmap = CloneMapper( |
| 80 | [{'name': 'plugins/(.*)', 'dest': './\\1'}], |
| 81 | ['plugins/PluginInBasePath']) |
| 82 | self.assertEqual( |
| 83 | {'plugins/PluginInBasePath': '/basepath/PluginInBasePath'}, |
| 84 | cmap.expand('/basepath')) |