summaryrefslogtreecommitdiffstats
path: root/tools/mb/mb_unittest.py
blob: 19cd87fabed2eff250c835471988202d1b13982f (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
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for mb.py."""

import json
import sys
import unittest

import mb


class FakeMBW(mb.MetaBuildWrapper):
  def __init__(self):
    super(FakeMBW, self).__init__()
    self.files = {}
    self.calls = []
    self.out = ''
    self.err = ''
    self.chromium_src_dir = '/fake_src'
    self.default_config = '/fake_src/tools/mb/mb_config.pyl'

  def ExpandUser(self, path):
    return '$HOME/%s' % path

  def Exists(self, path):
    return self.files.get(path) is not None

  def ReadFile(self, path):
    return self.files[path]

  def WriteFile(self, path, contents):
    self.files[path] = contents

  def Call(self, cmd):
    self.calls.append(cmd)
    return 0, '', ''

  def Print(self, *args, **kwargs):
    sep = kwargs.get('sep', ' ')
    end = kwargs.get('end', '\n')
    f = kwargs.get('file', sys.stdout)
    if f == sys.stderr:
      self.err += sep.join(args) + end
    else:
      self.out += sep.join(args) + end

class IntegrationTest(unittest.TestCase):
  def test_validate(self):
    # Note that this validates that the actual mb_config.pyl is valid.
    ret = mb.main(['validate', '--quiet'])
    self.assertEqual(ret, 0)


TEST_CONFIG = """\
{
  'common_dev_configs': ['gn_debug'],
  'configs': {
    'gyp_rel_bot': ['gyp', 'rel', 'goma'],
    'gn_debug': ['gn', 'debug'],
    'gn_rel_bot': ['gn', 'rel', 'goma'],
    'private': ['gyp', 'fake_feature1'],
    'unsupported': ['gn', 'fake_feature2'],
  },
  'masters': {
    'fake_master': {
      'fake_builder': 'gyp_rel_bot',
      'fake_gn_builder': 'gn_rel_bot',
    },
  },
  'mixins': {
    'fake_feature1': {
      'gn_args': 'enable_doom_melon=true',
      'gyp_defines': 'doom_melon=1',
    },
    'fake_feature2': {
      'gn_args': 'enable_doom_melon=false',
      'gyp_defaults': 'doom_melon=0',
    },
    'gyp': {'type': 'gyp'},
    'gn': {'type': 'gn'},
    'goma': {
      'gn_args': 'use_goma=true goma_dir="$(goma_dir)"',
      'gyp_defines': 'goma=1 gomadir="$(goma_dir)"',
    },
    'rel': {
      'gn_args': 'is_debug=false',
      'gyp_config': 'Release',
    },
    'debug': {
      'gn_args': 'is_debug=true',
    },
  },
  'private_configs': ['private'],
  'unsupported_configs': ['unsupported'],
}
"""


class UnitTest(unittest.TestCase):
  def fake_mbw(self, files):
    mbw = FakeMBW()
    if files:
      for path, contents in files.items():
        mbw.files[path] = contents
    mbw.files.setdefault(mbw.default_config, TEST_CONFIG)
    return mbw

  def check(self, args, mbw=None, files=None, out=None, err=None, ret=None):
    if not mbw:
      mbw = self.fake_mbw(files)
    mbw.ParseArgs(args)
    actual_ret = mbw.args.func()
    if ret is not None:
      self.assertEqual(actual_ret, ret)
    if out is not None:
      self.assertEqual(mbw.out, out)
    if err is not None:
      self.assertEqual(mbw.err, err)
    return mbw

  def test_gn_analyze(self):
    files = {'/tmp/in.json': """{\
               "files": ["foo/foo_unittest.cc"],
               "targets": ["foo_unittests", "bar_unittests"]
             }"""}
    mbw = self.fake_mbw(files)
    mbw.Call = lambda cmd: (0, 'out/Default/foo_unittests\n', '')

    self.check(['analyze', '-c', 'gn_debug', '//out/Default',
                '/tmp/in.json', '/tmp/out.json'], mbw=mbw, ret=0)
    out = json.loads(mbw.files['/tmp/out.json'])
    self.assertEqual(out, {
      'status': 'Found dependency',
      'targets': ['foo_unittests'],
      'build_targets': ['foo_unittests']
    })

  def test_gyp_analyze(self):
    self.check(['analyze', '-c', 'gyp_rel_bot', '//out/Release',
                '/tmp/in.json', '/tmp/out.json'],
               ret=0)

  def test_gen(self):
    self.check(['gen', '-c', 'gn_debug', '//out/Default'], ret=0)
    self.check(['gen', '-c', 'gyp_rel_bot', '//out/Release'], ret=0)

  def test_goma_dir_expansion(self):
    self.check(['lookup', '-c', 'gyp_rel_bot', '-g', '/foo'], ret=0,
               out=("python build/gyp_chromium -G 'output_dir=<path>' "
                    "-G config=Release -D goma=1 -D gomadir=/foo\n"))
    self.check(['lookup', '-c', 'gn_rel_bot', '-g', '/foo'], ret=0,
               out=("gn gen '<path>' '--args=is_debug=false use_goma=true "
                    "goma_dir=\"/foo\"'\n" ))

  def test_help(self):
    self.assertRaises(SystemExit, self.check, ['-h'])
    self.assertRaises(SystemExit, self.check, ['help'])
    self.assertRaises(SystemExit, self.check, ['help', 'gen'])

  def test_lookup(self):
    self.check(['lookup', '-c', 'gn_debug'], ret=0)

  def test_validate(self):
    self.check(['validate'], ret=0)


if __name__ == '__main__':
  unittest.main()