summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/test/backend/common.py
blob: 85ccb10378ebdb5fc499448a3d7949cb6239aced (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import unicode_literals

import os
import unittest

from collections import defaultdict
from shutil import rmtree
from tempfile import mkdtemp

from mach.logging import LoggingManager

from mozbuild.backend.configenvironment import ConfigEnvironment
from mozbuild.frontend.emitter import TreeMetadataEmitter
from mozbuild.frontend.reader import BuildReader

import mozpack.path as mozpath


log_manager = LoggingManager()
log_manager.add_terminal_logging()


test_data_path = mozpath.abspath(mozpath.dirname(__file__))
test_data_path = mozpath.join(test_data_path, 'data')


CONFIGS = defaultdict(lambda: {
    'defines': {},
    'non_global_defines': [],
    'substs': {'OS_TARGET': 'WINNT'},
}, {
    'android_eclipse': {
        'defines': {
            'MOZ_ANDROID_MIN_SDK_VERSION': '15',
        },
        'non_global_defines': [],
        'substs': {
            'ANDROID_TARGET_SDK': '16',
            'MOZ_WIDGET_TOOLKIT': 'android',
        },
    },
    'binary-components': {
        'defines': {},
        'non_global_defines': [],
        'substs': {
            'LIB_PREFIX': 'lib',
            'LIB_SUFFIX': 'a',
            'COMPILE_ENVIRONMENT': '1',
        },
    },
    'sources': {
        'defines': {},
        'non_global_defines': [],
        'substs': {
            'LIB_PREFIX': 'lib',
            'LIB_SUFFIX': 'a',
        },
    },
    'stub0': {
        'defines': {
            'MOZ_TRUE_1': '1',
            'MOZ_TRUE_2': '1',
        },
        'non_global_defines': [
            'MOZ_NONGLOBAL_1',
            'MOZ_NONGLOBAL_2',
        ],
        'substs': {
            'MOZ_FOO': 'foo',
            'MOZ_BAR': 'bar',
        },
    },
    'substitute_config_files': {
        'defines': {},
        'non_global_defines': [],
        'substs': {
            'MOZ_FOO': 'foo',
            'MOZ_BAR': 'bar',
        },
    },
    'test_config': {
        'defines': {
            'foo': 'baz qux',
            'baz': 1,
        },
        'non_global_defines': [],
        'substs': {
            'foo': 'bar baz',
        },
    },
    'visual-studio': {
        'defines': {},
        'non_global_defines': [],
        'substs': {
            'MOZ_APP_NAME': 'my_app',
        },
    },
})


class BackendTester(unittest.TestCase):
    def setUp(self):
        self._old_env = dict(os.environ)
        os.environ.pop('MOZ_OBJDIR', None)

    def tearDown(self):
        os.environ.clear()
        os.environ.update(self._old_env)

    def _get_environment(self, name):
        """Obtain a new instance of a ConfigEnvironment for a known profile.

        A new temporary object directory is created for the environment. The
        environment is cleaned up automatically when the test finishes.
        """
        config = CONFIGS[name]

        objdir = mkdtemp()
        self.addCleanup(rmtree, objdir)

        srcdir = mozpath.join(test_data_path, name)
        config['substs']['top_srcdir'] = srcdir
        return ConfigEnvironment(srcdir, objdir, **config)

    def _emit(self, name, env=None):
        env = env or self._get_environment(name)
        reader = BuildReader(env)
        emitter = TreeMetadataEmitter(env)

        return env, emitter.emit(reader.read_topsrcdir())

    def _consume(self, name, cls, env=None):
        env, objs = self._emit(name, env=env)
        backend = cls(env)
        backend.consume(objs)

        return env

    def _tree_paths(self, topdir, filename):
        for dirpath, dirnames, filenames in os.walk(topdir):
            for f in filenames:
                if f == filename:
                    yield mozpath.relpath(mozpath.join(dirpath, f), topdir)

    def _mozbuild_paths(self, env):
        return self._tree_paths(env.topsrcdir, 'moz.build')

    def _makefile_in_paths(self, env):
        return self._tree_paths(env.topsrcdir, 'Makefile.in')


__all__ = ['BackendTester']