1
# -*- coding: utf-8 -*-
2
# Copyright: (C) 2013 Canonical
7
# This program is free software; you can redistribute it and/or modify it under
8
# the terms of the GNU General Public License as published by the Free Software
9
# Foundation; version 3.
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
from . import BaseUnitTestCase
22
from cupstream2distro import tools
29
def __init__(self, source_branch, prereq_branch=None):
30
self.source_branch = MockBranch(source_branch)
32
self.prerequisite_branch = MockBranch(prereq_branch)
34
self.prerequisite_branch = None
37
def __init__(self, bzr_identity):
38
self.bzr_identity = bzr_identity
40
class ToolsTests(BaseUnitTestCase):
43
'''add some default files'''
44
super(ToolsTests, self).setUp()
45
self.artefacts_dir = os.path.join(self.data_dir, 'artefacts')
47
def test_get_previous_distro_version_from_config(self):
48
'''We load and return the previous distro version from config'''
49
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
50
self.assertEquals(tools.get_previous_distro_version_from_config('foo'), '6.12.0-0ubuntu1')
52
def test_return_exception_if_config_doesnt_exist(self):
53
'''We return an exception if the config files does not exist'''
54
with self.assertRaises(Exception):
55
tools.get_previous_distro_version_from_config('foo')
57
def test_save_correct_project_file(self):
58
'''We save the correct profiles file'''
59
tools.save_project_config('foo', 'lp:foo', '42', '6.12.0-0ubuntu1', '6.12.0daily13.02.27-0ubuntu1')
60
self.assertFilesAreIdenticals('foo.project', os.path.join(self.project_file_dir, 'foo.project'))
62
def test_packaging_diff_filename(self):
63
'''Return the right packaging diff name'''
64
self.assertEquals(tools.get_packaging_diff_filename("foo", "1.0daily~-0ubuntu1"), "packaging_changes_foo_1.0daily~-0ubuntu1.diff")
66
def test_generate_xml_artefacts_no_issue(self):
67
'''Generate the xml jenkins artefacts when no issue occured'''
68
tools.generate_xml_artefacts("Test Name", [], 'file.xml')
69
self.assertFilesAreIdenticals('file.xml', os.path.join(self.artefacts_dir, 'nofailure.xml'))
71
def test_generate_xml_artefacts_one_failure(self):
72
'''Generate the xml jenkins artefacts when there is one failure'''
73
tools.generate_xml_artefacts("Test Name", ["one issue"], 'file.xml')
74
self.assertFilesAreIdenticals('file.xml', os.path.join(self.artefacts_dir, 'onefailure.xml'))
76
def test_generate_xml_artefacts_two_failures(self):
77
'''Generate the xml jenkins artefacts when there is more than one failure'''
78
tools.generate_xml_artefacts("Test Name", ["one issue", "a second issue"], 'file.xml')
79
self.assertFilesAreIdenticals('file.xml', os.path.join(self.artefacts_dir, 'twofailures.xml'))
81
def test_mark_project_as_published(self):
82
'''Rename current project filename once published to the unique project file to not republish it'''
83
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
84
tools.mark_project_as_published('foo', '4.2dailysomething-0ubuntu1')
85
self.assertTrue(os.path.isfile('foo.project_4.2dailysomething-0ubuntu1'))
87
def test_mark_project_and_diff_as_published(self):
88
'''Rename current project filename and diff file once published to the unique project file to not republish them'''
89
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
90
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), 'packaging_changes_foo_4.2dailysomething-0ubuntu1.diff')
91
tools.mark_project_as_published('foo', '4.2dailysomething-0ubuntu1')
92
self.assertTrue(os.path.isfile('packaging_changes_foo_4.2dailysomething-0ubuntu1.diff.published'))
94
def test_no_project_published(self):
95
'''Get no project published if nothing is published'''
96
self.assertEquals(tools.get_published_to_distro_projects(), {})
98
def test_one_project_published(self):
99
'''Get one project published with one version'''
100
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
101
tools.mark_project_as_published('foo', '42-0ubuntu1')
102
self.assertEquals(tools.get_published_to_distro_projects(), {'foo': ['42-0ubuntu1']})
104
def test_one_project_published_multiple_times(self):
105
'''Get one project published with multiple published versions'''
106
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
107
tools.mark_project_as_published('foo', '42-0ubuntu1')
108
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
109
tools.mark_project_as_published('foo', '43-0ubuntu1')
110
published_projects = tools.get_published_to_distro_projects()
111
self.assertEqual(published_projects.keys(), ['foo'])
112
versions = set(published_projects['foo'])
113
self.assertEqual(len(versions), 2)
114
self.assertEqual(set(['42-0ubuntu1', '43-0ubuntu1']).difference(versions), set([]))
116
def test_multiple_projects_published(self):
117
'''Get one project published with multiple published versions'''
118
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
119
tools.mark_project_as_published('foo', '42-0ubuntu1')
120
shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), 'bar.project')
121
tools.mark_project_as_published('bar', '43-0ubuntu1')
122
self.assertDictEqual(tools.get_published_to_distro_projects(), {'foo': ['42-0ubuntu1'], 'bar': ['43-0ubuntu1']})
124
def test_parse_and_clean_entry_space(self):
125
'''Get an entry only separated by spaces'''
126
self.assertEqual(tools.parse_and_clean_entry("foo1 foo2 foo3"),
127
["foo1", "foo2", "foo3"])
129
def test_parse_and_clean_entry_comma(self):
130
'''Get an entry only separated by commas'''
131
self.assertEqual(tools.parse_and_clean_entry("foo1, foo2, foo3"),
132
["foo1", "foo2", "foo3"])
134
def test_parse_and_clean_entry_comma_and_spaces(self):
135
'''Get an entry only separated with a sep (commas) and a lot of additional spaces'''
136
self.assertEqual(tools.parse_and_clean_entry(" foo1, foo2 , foo3"),
137
["foo1", "foo2", "foo3"])
140
def test_parse_and_clean_entry_slash(self):
141
'''Get an entry only separated by slash'''
142
self.assertEqual(tools.parse_and_clean_entry("foo1/foo2/foo3", slash_as_sep=True),
143
["foo1", "foo2", "foo3"])
145
def test_parse_and_clean_entry_return(self):
146
'''Get an entry only separated by commas and space'''
147
self.assertEqual(tools.parse_and_clean_entry("foo1\nfoo2\nfoo3"),
148
["foo1", "foo2", "foo3"])
150
def test_parse_and_clean_entry_all_sep_but_slash(self):
151
'''Get an entry only separated by all possible sep'''
152
self.assertEqual(tools.parse_and_clean_entry("foo1, foo2\nfoo3\n foo4"),
153
["foo1", "foo2", "foo3", "foo4"])
155
def test_parse_and_clean_entry_all_sep(self):
156
'''Get an entry only separated by all possible sep'''
157
self.assertEqual(tools.parse_and_clean_entry("foo1, foo2\nfoo3\n foo4 / foo5 ", slash_as_sep=True),
158
["foo1", "foo2", "foo3", "foo4", "foo5"])
160
def test_parse_and_clean_entry_no_slash_sep(self):
161
'''Get an entry only separated by commas. Entries contains slash and shouldn't be separated'''
162
self.assertEqual(tools.parse_and_clean_entry("fo/o1, foo2, foo3"),
163
["fo/o1", "foo2", "foo3"])
165
def test_reorder_branches_regarding_prereqs(self):
166
'''Check reorder projects according to prerequisite branches'''
167
original_component_list = [ MockMerge("0"), MockMerge("2", "1"), MockMerge("1"), MockMerge("4"), MockMerge("3", "2") ]
168
resulting_component_list = tools.reorder_branches_regarding_prereqs(original_component_list)
169
self.assertEqual(len(resulting_component_list), 5)
170
self.assertEqual(resulting_component_list[0].source_branch.bzr_identity, "0")
171
self.assertEqual(resulting_component_list[1].source_branch.bzr_identity, "1")
172
self.assertEqual(resulting_component_list[2].source_branch.bzr_identity, "2")
173
self.assertEqual(resulting_component_list[3].source_branch.bzr_identity, "3")
174
self.assertEqual(resulting_component_list[4].source_branch.bzr_identity, "4")
176
def test_reorder_branches_regarding_prereqs_no_change(self):
177
'''Check that no reorder happens when there are no prerequisite branches used'''
178
original_component_list = [ MockMerge("1"), MockMerge("2"), MockMerge("3") ]
179
resulting_component_list = tools.reorder_branches_regarding_prereqs(original_component_list)
180
self.assertEqual(len(resulting_component_list), 3)
181
self.assertEqual(resulting_component_list[0].source_branch.bzr_identity, "1")
182
self.assertEqual(resulting_component_list[1].source_branch.bzr_identity, "2")
183
self.assertEqual(resulting_component_list[2].source_branch.bzr_identity, "3")