• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2#                     The LLVM Compiler Infrastructure
3#
4# This file is distributed under the University of Illinois Open Source
5# License. See LICENSE.TXT for details.
6
7import libear
8from . import make_args, silent_check_call, silent_call, create_empty_file
9import unittest
10
11import os.path
12import json
13
14
15class CompilationDatabaseTest(unittest.TestCase):
16    @staticmethod
17    def run_intercept(tmpdir, args):
18        result = os.path.join(tmpdir, 'cdb.json')
19        make = make_args(tmpdir) + args
20        silent_check_call(
21            ['intercept-build', '--cdb', result] + make)
22        return result
23
24    @staticmethod
25    def count_entries(filename):
26        with open(filename, 'r') as handler:
27            content = json.load(handler)
28            return len(content)
29
30    def test_successful_build(self):
31        with libear.TemporaryDirectory() as tmpdir:
32            result = self.run_intercept(tmpdir, ['build_regular'])
33            self.assertTrue(os.path.isfile(result))
34            self.assertEqual(5, self.count_entries(result))
35
36    def test_successful_build_with_wrapper(self):
37        with libear.TemporaryDirectory() as tmpdir:
38            result = os.path.join(tmpdir, 'cdb.json')
39            make = make_args(tmpdir) + ['build_regular']
40            silent_check_call(['intercept-build', '--cdb', result,
41                               '--override-compiler'] + make)
42            self.assertTrue(os.path.isfile(result))
43            self.assertEqual(5, self.count_entries(result))
44
45    @unittest.skipIf(os.getenv('TRAVIS'), 'ubuntu make return -11')
46    def test_successful_build_parallel(self):
47        with libear.TemporaryDirectory() as tmpdir:
48            result = self.run_intercept(tmpdir, ['-j', '4', 'build_regular'])
49            self.assertTrue(os.path.isfile(result))
50            self.assertEqual(5, self.count_entries(result))
51
52    @unittest.skipIf(os.getenv('TRAVIS'), 'ubuntu env remove clang from path')
53    def test_successful_build_on_empty_env(self):
54        with libear.TemporaryDirectory() as tmpdir:
55            result = os.path.join(tmpdir, 'cdb.json')
56            make = make_args(tmpdir) + ['CC=clang', 'build_regular']
57            silent_check_call(['intercept-build', '--cdb', result,
58                               'env', '-'] + make)
59            self.assertTrue(os.path.isfile(result))
60            self.assertEqual(5, self.count_entries(result))
61
62    def test_successful_build_all_in_one(self):
63        with libear.TemporaryDirectory() as tmpdir:
64            result = self.run_intercept(tmpdir, ['build_all_in_one'])
65            self.assertTrue(os.path.isfile(result))
66            self.assertEqual(5, self.count_entries(result))
67
68    def test_not_successful_build(self):
69        with libear.TemporaryDirectory() as tmpdir:
70            result = os.path.join(tmpdir, 'cdb.json')
71            make = make_args(tmpdir) + ['build_broken']
72            silent_call(
73                ['intercept-build', '--cdb', result] + make)
74            self.assertTrue(os.path.isfile(result))
75            self.assertEqual(2, self.count_entries(result))
76
77
78class ExitCodeTest(unittest.TestCase):
79    @staticmethod
80    def run_intercept(tmpdir, target):
81        result = os.path.join(tmpdir, 'cdb.json')
82        make = make_args(tmpdir) + [target]
83        return silent_call(
84            ['intercept-build', '--cdb', result] + make)
85
86    def test_successful_build(self):
87        with libear.TemporaryDirectory() as tmpdir:
88            exitcode = self.run_intercept(tmpdir, 'build_clean')
89            self.assertFalse(exitcode)
90
91    def test_not_successful_build(self):
92        with libear.TemporaryDirectory() as tmpdir:
93            exitcode = self.run_intercept(tmpdir, 'build_broken')
94            self.assertTrue(exitcode)
95
96
97class ResumeFeatureTest(unittest.TestCase):
98    @staticmethod
99    def run_intercept(tmpdir, target, args):
100        result = os.path.join(tmpdir, 'cdb.json')
101        make = make_args(tmpdir) + [target]
102        silent_check_call(
103            ['intercept-build', '--cdb', result] + args + make)
104        return result
105
106    @staticmethod
107    def count_entries(filename):
108        with open(filename, 'r') as handler:
109            content = json.load(handler)
110            return len(content)
111
112    def test_overwrite_existing_cdb(self):
113        with libear.TemporaryDirectory() as tmpdir:
114            result = self.run_intercept(tmpdir, 'build_clean', [])
115            self.assertTrue(os.path.isfile(result))
116            result = self.run_intercept(tmpdir, 'build_regular', [])
117            self.assertTrue(os.path.isfile(result))
118            self.assertEqual(2, self.count_entries(result))
119
120    def test_append_to_existing_cdb(self):
121        with libear.TemporaryDirectory() as tmpdir:
122            result = self.run_intercept(tmpdir, 'build_clean', [])
123            self.assertTrue(os.path.isfile(result))
124            result = self.run_intercept(tmpdir, 'build_regular', ['--append'])
125            self.assertTrue(os.path.isfile(result))
126            self.assertEqual(5, self.count_entries(result))
127
128
129class ResultFormatingTest(unittest.TestCase):
130    @staticmethod
131    def run_intercept(tmpdir, command):
132        result = os.path.join(tmpdir, 'cdb.json')
133        silent_check_call(
134            ['intercept-build', '--cdb', result] + command,
135            cwd=tmpdir)
136        with open(result, 'r') as handler:
137            content = json.load(handler)
138            return content
139
140    def assert_creates_number_of_entries(self, command, count):
141        with libear.TemporaryDirectory() as tmpdir:
142            filename = os.path.join(tmpdir, 'test.c')
143            create_empty_file(filename)
144            command.append(filename)
145            cmd = ['sh', '-c', ' '.join(command)]
146            cdb = self.run_intercept(tmpdir, cmd)
147            self.assertEqual(count, len(cdb))
148
149    def test_filter_preprocessor_only_calls(self):
150        self.assert_creates_number_of_entries(['cc', '-c'], 1)
151        self.assert_creates_number_of_entries(['cc', '-c', '-E'], 0)
152        self.assert_creates_number_of_entries(['cc', '-c', '-M'], 0)
153        self.assert_creates_number_of_entries(['cc', '-c', '-MM'], 0)
154
155    def assert_command_creates_entry(self, command, expected):
156        with libear.TemporaryDirectory() as tmpdir:
157            filename = os.path.join(tmpdir, command[-1])
158            create_empty_file(filename)
159            cmd = ['sh', '-c', ' '.join(command)]
160            cdb = self.run_intercept(tmpdir, cmd)
161            self.assertEqual(' '.join(expected), cdb[0]['command'])
162
163    def test_filter_preprocessor_flags(self):
164        self.assert_command_creates_entry(
165            ['cc', '-c', '-MD', 'test.c'],
166            ['cc', '-c', 'test.c'])
167        self.assert_command_creates_entry(
168            ['cc', '-c', '-MMD', 'test.c'],
169            ['cc', '-c', 'test.c'])
170        self.assert_command_creates_entry(
171            ['cc', '-c', '-MD', '-MF', 'test.d', 'test.c'],
172            ['cc', '-c', 'test.c'])
173
174    def test_pass_language_flag(self):
175        self.assert_command_creates_entry(
176            ['cc', '-c', '-x', 'c', 'test.c'],
177            ['cc', '-c', '-x', 'c', 'test.c'])
178        self.assert_command_creates_entry(
179            ['cc', '-c', 'test.c'],
180            ['cc', '-c', 'test.c'])
181
182    def test_pass_arch_flags(self):
183        self.assert_command_creates_entry(
184            ['clang', '-c', 'test.c'],
185            ['cc', '-c', 'test.c'])
186        self.assert_command_creates_entry(
187            ['clang', '-c', '-arch', 'i386', 'test.c'],
188            ['cc', '-c', '-arch', 'i386', 'test.c'])
189        self.assert_command_creates_entry(
190            ['clang', '-c', '-arch', 'i386', '-arch', 'armv7l', 'test.c'],
191            ['cc', '-c', '-arch', 'i386', '-arch', 'armv7l', 'test.c'])
192