summaryrefslogtreecommitdiff
path: root/morphlib/buildorder_tests.py
blob: ab1a7e1c185cb5f0a722df130a0d64503ca53856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Copyright (C) 2012  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import unittest

import morphlib


class FakeSource(object):
    pass


class BuildOrderTests(unittest.TestCase):

    def test_empty_list_results_in_no_groups_at_all(self):
        order = morphlib.buildorder.BuildOrder([])
        self.assertEqual(len(order.groups), 0)

    def test_list_with_one_artifact_results_in_one_group(self):
        chunk = FakeSource()
        artifact = morphlib.artifact.Artifact(chunk, 'chunk')

        order = morphlib.buildorder.BuildOrder([artifact])

        self.assertEqual(len(order.groups), 1)
        self.assertEqual(order.groups[0], [artifact])

    def test_list_with_two_unrelated_artifacts_results_in_one_group(self):
        chunk1 = FakeSource()
        artifact1 = morphlib.artifact.Artifact(chunk1, 'chunk1')

        chunk2 = FakeSource()
        artifact2 = morphlib.artifact.Artifact(chunk2, 'chunk2')

        order = morphlib.buildorder.BuildOrder([artifact1, artifact2])

        self.assertEqual(len(order.groups), 1)
        self.assertEqual(order.groups[0], [artifact1, artifact2])

    def test_list_with_two_dependent_artifacts_results_in_two_groups(self):
        chunk1 = FakeSource()
        artifact1 = morphlib.artifact.Artifact(chunk1, 'chunk1')

        chunk2 = FakeSource()
        artifact2 = morphlib.artifact.Artifact(chunk2, 'chunk2')
        artifact2.add_dependency(artifact1)

        order = morphlib.buildorder.BuildOrder([artifact1, artifact2])

        self.assertEqual(len(order.groups), 2)
        self.assertEqual(order.groups[0], [artifact1])
        self.assertEqual(order.groups[1], [artifact2])

    def test_chain_of_three_dependent_artifacts_results_in_three_groups(self):
        chunk1 = FakeSource()
        artifact1 = morphlib.artifact.Artifact(chunk1, 'chunk1')

        chunk2 = FakeSource()
        artifact2 = morphlib.artifact.Artifact(chunk2, 'chunk2')
        artifact2.add_dependency(artifact1)

        chunk3 = FakeSource()
        artifact3 = morphlib.artifact.Artifact(chunk3, 'chunk3')
        artifact3.add_dependency(artifact2)

        order = morphlib.buildorder.BuildOrder(
            [artifact1, artifact2, artifact3])

        self.assertEqual(len(order.groups), 3)
        self.assertEqual(order.groups[0], [artifact1])
        self.assertEqual(order.groups[1], [artifact2])
        self.assertEqual(order.groups[2], [artifact3])

    def test_two_artifacts_depending_on_another_results_in_two_groups(self):
        chunk1 = FakeSource()
        artifact1 = morphlib.artifact.Artifact(chunk1, 'chunk1')

        chunk2 = FakeSource()
        artifact2 = morphlib.artifact.Artifact(chunk2, 'chunk2')
        artifact2.add_dependency(artifact1)

        chunk3 = FakeSource()
        artifact3 = morphlib.artifact.Artifact(chunk3, 'chunk3')
        artifact3.add_dependency(artifact1)

        order = morphlib.buildorder.BuildOrder(
            [artifact1, artifact2, artifact3])

        self.assertEqual(len(order.groups), 2)
        self.assertEqual(order.groups[0], [artifact1])
        self.assertEqual(order.groups[1], [artifact2, artifact3])

    def test_one_artifact_depending_on_two_others_results_in_two_groups(self):
        chunk1 = FakeSource()
        artifact1 = morphlib.artifact.Artifact(chunk1, 'chunk1')

        chunk2 = FakeSource()
        artifact2 = morphlib.artifact.Artifact(chunk2, 'chunk2')

        chunk3 = FakeSource()
        artifact3 = morphlib.artifact.Artifact(chunk3, 'chunk3')
        artifact3.add_dependency(artifact1)
        artifact3.add_dependency(artifact2)

        order = morphlib.buildorder.BuildOrder(
            [artifact1, artifact2, artifact3])

        self.assertEqual(len(order.groups), 2)
        self.assertEqual(order.groups[0], [artifact1, artifact2])
        self.assertEqual(order.groups[1], [artifact3])

    def test_detection_of_cyclic_dependency_chain(self):
        chunk1 = FakeSource()
        artifact1 = morphlib.artifact.Artifact(chunk1, 'chunk1')

        chunk2 = FakeSource()
        artifact2 = morphlib.artifact.Artifact(chunk2, 'chunk2')

        chunk3 = FakeSource()
        artifact3 = morphlib.artifact.Artifact(chunk3, 'chunk3')

        artifact1.add_dependency(artifact3)
        artifact2.add_dependency(artifact1)
        artifact3.add_dependency(artifact2)

        self.assertRaises(morphlib.buildorder.CyclicDependencyChainError,
                          morphlib.buildorder.BuildOrder,
                          [artifact1, artifact2, artifact3])