1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import, print_function, unicode_literals
6
7import errno
8import os
9import shutil
10
11from buildconfig import topsrcdir
12
13from mach.logging import LoggingManager
14
15from mozbuild.util import ReadOnlyDict
16
17import mozpack.path as mozpath
18
19
20# By including this module, tests get structured logging.
21log_manager = LoggingManager()
22log_manager.add_terminal_logging()
23
24
25def prepare_tmp_topsrcdir(path):
26    for p in (
27        "build/autoconf/config.guess",
28        "build/autoconf/config.sub",
29        "build/moz.configure/checks.configure",
30        "build/moz.configure/init.configure",
31        "build/moz.configure/util.configure",
32    ):
33        file_path = os.path.join(path, p)
34        try:
35            os.makedirs(os.path.dirname(file_path))
36        except OSError as e:
37            if e.errno != errno.EEXIST:
38                raise
39        shutil.copy(os.path.join(topsrcdir, p), file_path)
40
41
42# mozconfig is not a reusable type (it's actually a module) so, we
43# have to mock it.
44class MockConfig(object):
45    def __init__(
46        self,
47        topsrcdir="/path/to/topsrcdir",
48        extra_substs={},
49        error_is_fatal=True,
50    ):
51        self.topsrcdir = mozpath.abspath(topsrcdir)
52        self.topobjdir = mozpath.abspath("/path/to/topobjdir")
53
54        self.substs = ReadOnlyDict(
55            {
56                "MOZ_FOO": "foo",
57                "MOZ_BAR": "bar",
58                "MOZ_TRUE": "1",
59                "MOZ_FALSE": "",
60                "DLL_PREFIX": "lib",
61                "DLL_SUFFIX": ".so",
62            },
63            **extra_substs
64        )
65
66        self.defines = self.substs
67
68        self.lib_prefix = "lib"
69        self.lib_suffix = ".a"
70        self.import_prefix = "lib"
71        self.import_suffix = ".so"
72        self.dll_prefix = "lib"
73        self.dll_suffix = ".so"
74        self.error_is_fatal = error_is_fatal
75