summaryrefslogtreecommitdiff
path: root/test/test_cerbero_build_recipe.py
blob: 0455f8db2b86403e6741b9abf3b32a3f9c159c35 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import unittest
import os

from cerbero.build import recipe
from cerbero.config import Platform, License, Architecture
from cerbero.errors import FatalError
from test.test_common import DummyConfig
from test.test_build_common import Recipe1

# ruff: noqa: E731


class Class1(object):
    test = None

    def __init__(self):
        self.test = 'CODEPASS'

    class1_method = lambda x: None
    compile = lambda x: 'CODEPASS'


class Class2(object):
    class2_method = lambda x: None
    fetch = lambda x: 'CODEPASS'


class Recipe(recipe.Recipe):
    btype = Class1
    stype = Class2

    name = 'recipe'
    version = '0.0.0'
    licenses = [License.LGPL]
    deps = ['dep1', 'dep2']
    platform_deps = {Platform.LINUX: ['dep3'], Platform.WINDOWS: ['dep4']}
    post_install = lambda x: 'CODEPASS'

    files_libs = ['librecipe-test']
    files_bins = ['recipe-test']
    licenses_bins = [License.GPL]
    platform_files_test = {Platform.LINUX: ['test1']}
    platform_licenses_test = {Platform.LINUX: [License.BSD]}


class Class3(object, metaclass=recipe.MetaUniversalRecipe):
    def _do_step(self, name):
        return name


class TestReceiptMetaClass(unittest.TestCase):
    def setUp(self):
        self.config = DummyConfig()
        self.config.local_sources = ''
        self.config.sources = ''
        self.t = Recipe(self.config)

    def testReceiptBases(self):
        r = recipe.Recipe(self.config)
        bases = r.__class__.mro()
        self.assertTrue(Class1 not in bases)
        self.assertTrue(Class2 not in bases)

    def testReceiptSubclassBases(self):
        bases = self.t.__class__.mro()
        self.assertTrue(Class1 in bases)
        self.assertTrue(Class2 in bases)

    def testFunctions(self):
        self.assertTrue(hasattr(self.t, 'class1_method'))
        self.assertTrue(hasattr(self.t, 'class2_method'))
        self.assertEqual(self.t.fetch(), 'CODEPASS')
        self.assertEqual(self.t.compile(), 'CODEPASS')
        self.assertEqual(self.t.post_install(), 'CODEPASS')

    def testSubclassesInit(self):
        self.assertEqual(self.t.test, 'CODEPASS')


class TestReceipt(unittest.TestCase):
    def setUp(self):
        self.config = DummyConfig()
        self.config.local_sources = 'path1'
        self.config.sources = 'path2'
        self.recipe = Recipe(self.config)

    def testSources(self):
        repo_dir = os.path.join(self.config.local_sources, self.recipe.package_name)
        repo_dir = os.path.abspath(repo_dir)
        build_dir = os.path.join(self.config.sources, self.recipe.package_name)
        build_dir = os.path.abspath(build_dir)

        self.assertEqual(self.recipe.repo_dir, repo_dir)
        self.assertEqual(self.recipe.build_dir, build_dir)

    def testListDeps(self):
        self.recipe.config.target_platform = Platform.LINUX
        self.assertEqual(['dep1', 'dep2', 'dep3'], self.recipe.list_deps())
        self.recipe.config.target_platform = Platform.WINDOWS
        self.assertEqual(['dep1', 'dep2', 'dep4'], self.recipe.list_deps())

    def testRemoveSteps(self):
        self.recipe._remove_steps(['donotexits'])
        self.assertTrue(recipe.BuildSteps.FETCH in self.recipe._steps)
        self.recipe._remove_steps([recipe.BuildSteps.FETCH])
        self.assertTrue(recipe.BuildSteps.FETCH not in self.recipe._steps)
        r = Recipe(self.config)
        self.assertTrue(recipe.BuildSteps.FETCH in r._steps)


class TestLicenses(unittest.TestCase):
    def setUp(self):
        self.config = DummyConfig()
        self.config.local_sources = ''
        self.config.sources = ''
        self.recipe = Recipe(self.config)

    def testLicenses(self):
        self.assertEqual(self.recipe.licenses, [License.LGPL])

        licenses_libs = self.recipe.list_licenses_by_categories(['libs'])
        self.assertEqual(licenses_libs['libs'], [License.LGPL])
        self.assertEqual(list(licenses_libs.values()), [[License.LGPL]])
        licenses_bins = self.recipe.list_licenses_by_categories(['bins'])
        self.assertEqual(licenses_bins['bins'], [License.GPL])
        self.assertEqual(list(licenses_bins.values()), [[License.GPL]])

        self.recipe.platform = Platform.LINUX
        self.recipe.config.target_platform = Platform.LINUX
        licenses_test = self.recipe.list_licenses_by_categories(['test'])
        self.assertEqual(licenses_test['test'], [License.BSD])
        self.assertEqual(list(licenses_test.values()), [[License.BSD]])


class TestMetaUniveralRecipe(unittest.TestCase):
    def testBuildSteps(self):
        obj = Class3()
        for _, step in recipe.BuildSteps():
            self.assertTrue(hasattr(obj, step))
            stepfunc = getattr(obj, step)
            self.assertEqual(stepfunc(), step)


class TestUniversalRecipe(unittest.TestCase):
    def setUp(self):
        self.config = DummyConfig()
        self.config.target_platform = Platform.LINUX
        self.config_x86 = DummyConfig()
        self.config_x86.target_platform = Platform.LINUX
        self.config_x86.target_architecture = Architecture.X86
        self.config_x86_64 = DummyConfig()
        self.config_x86_64.target_platform = Platform.LINUX
        self.config_x86_64.target_architecture = Architecture.X86_64
        self.recipe = recipe.UniversalRecipe(self.config)
        self.recipe_x86 = Recipe1(self.config_x86)
        self.recipe_x86_64 = Recipe1(self.config_x86_64)

    def testEmpty(self):
        self.assertTrue(self.recipe.is_empty())

    def testProxyEmpty(self):
        self.assertRaises(AttributeError, getattr, self.recipe, 'name')

    def testProxyRecipe(self):
        self.recipe.add_recipe(self.recipe_x86)
        self.assertEqual(self.recipe.name, self.recipe_x86.name)
        self.assertEqual(self.recipe.licence, self.recipe_x86.licence)
        self.assertEqual(self.recipe.uuid, self.recipe_x86.uuid)

    def testAddRecipe(self):
        self.recipe.add_recipe(self.recipe_x86)
        self.assertEqual(self.recipe._recipes[Architecture.X86], self.recipe_x86)
        self.assertEqual(self.recipe._proxy_recipe, self.recipe_x86)

    def testDifferentRecipe(self):
        self.recipe.add_recipe(self.recipe_x86)
        recipe_test = Recipe1(self.config_x86)
        recipe_test.name = 'noname'
        self.assertRaises(FatalError, self.recipe.add_recipe, recipe_test)

    def testSteps(self):
        self.assertEqual(self.recipe.steps, [])
        self.recipe.add_recipe(self.recipe_x86)
        self.recipe.add_recipe(self.recipe_x86_64)
        self.assertEqual(self.recipe.steps, recipe.BuildSteps() + [recipe.BuildSteps.MERGE])