Test Failed
Push — feature/zapcc_with_python ( 570545...c207d8 )
by srz
256:04 queued 187:55
created

iuwandbox_test.test_make_run()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 1
dl 0
loc 10
rs 9.9
c 0
b 0
f 0
1
#!/usr/bin/env python
2
#
3
# test_iuwandbox.py
4
#
5
6
from __future__ import print_function
7
8
import sys
9
import os
10
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../fused')
11
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../wandbox')
12
13
try:
14
    import unittest2 as unittest
15
except:
16
    import unittest
17
import iuwandbox
18
import fused_iutest_files
19
import iuwandbox_pp
20
21
try:
22
    from StringIO import StringIO
23
except ImportError:
24
    from io import StringIO
25
26
root = os.path.normpath(os.path.dirname(os.path.abspath(__file__)) + '/../../../')
27
fused_src = root + '/fused-src'
28
test_src = root + '/test/syntax_tests.cpp'
29
test_opt_default = ['--encoding', 'utf-8-sig']
30
test_opt_nomain = test_opt_default
31
test_opt = ['-f"-DIUTEST_USE_MAIN"']
32
test_opt.extend(test_opt_default)
33
test_opt_verbose = ['--verbose']
34
test_opt_dryrun = ['--dryrun']
35
test_opt_no_min = ['--iutest-use-wandbox-min']
36
37
38
def eprint(*args, **kwargs):
39
    print(*args, file=sys.stderr, **kwargs)
40
41
42
class iuwandbox_test_base(unittest.TestCase):
43
    dir = None
44
45
    def setUp(self):
46
        self.capture = StringIO()
47
        sys.stdout = self.capture
48
        self.dir = os.getcwd()
49
        os.chdir(os.path.dirname(os.path.abspath(__file__)))
50
        return super(iuwandbox_test_base, self).setUp()
51
52
    def tearDown(self):
53
        sys.stdout = sys.__stdout__
54
        os.chdir(self.dir)
55
        self.capture.close()
56
        return super(iuwandbox_test_base, self).tearDown()
57
58
    def dump(self):
59
        value = self.capture.getvalue()
60
        eprint(value)
61
        return value
62
63
64
class nofused_iuwandbox_test(iuwandbox_test_base):
65
    def setUp(self):
66
        if 'SCRUTINIZER' in os.environ:
67
            self.skipTest('this test is not run on SCRUTINIZER.')
68
        for f in ['iutest.hpp', 'iutest.min.hpp', 'iutest.wandbox.min.hpp']:
69
            if os.path.exists(os.path.join(fused_src, f)):
70
                try:
71
                    os.remove(os.path.join(fused_src, f))
72
                except Exception as e:
73
                    self.skipTest('fused-src/' + f + ' remove failed... : ' + str(e))
74
            if os.path.exists(os.path.join(fused_src, f)):
75
                self.skipTest('fused-src/' + f + ' is exists')
76
        return super(nofused_iuwandbox_test, self).setUp()
77
78
    def test_nofused(self):
79
        sys.argv[1:] = [test_src]
80
        sys.argv.extend(test_opt)
81
        with self.assertRaises(SystemExit) as cm:
82
            iuwandbox.main()
83
        self.dump()
84
        output = self.capture.getvalue()
85
        self.assertEqual(cm.exception.code, 1, output)
86
        self.assertRegex(output, '.*please try \"make fused\".*')
87
88
89
class iuwandbox_test(iuwandbox_test_base):
90
    def setUp(self):
91
        need_make_fused = False
92
        for f in ['iutest.hpp', 'iutest.min.hpp', 'iutest.wandbox.min.hpp']:
93
            if not os.path.exists(os.path.join(fused_src, f)):
94
                need_make_fused = True
95
        if need_make_fused:
96
            try:
97
                fused_iutest_files.FusedAll(fused_iutest_files.IUTEST_INCLUDE_DIR, fused_src)
98
                iuwandbox_pp.default_pp()
99
#              os.system('python ' + root + '/tools/fused/fused_iutest_files.py ' + fused_src)
100
            except:
101
                pass
102
        if not os.path.exists(os.path.join(fused_src, 'iutest.min.hpp')):
103
            self.skipTest('fused-src is not exists')
104
        return super(iuwandbox_test, self).setUp()
105
106
    def test_nomain(self):
107
        sys.argv[1:] = [test_src]
108
        sys.argv.extend(test_opt_nomain)
109
        with self.assertRaises(SystemExit) as cm:
110
            iuwandbox.main()
111
        output = self.dump()
112
        self.assertEqual(cm.exception.code, 1, output)
113
        self.assertRegex(output, '.*hint:.*')
114
        self.assertRegex(output, '.*In "iutest" you can omit the definition of the main function, please define IUTEST_USE_MAIN. (--iutest-use-main or -f"-DIUTEST_USE_MAIN")*')
115
116
    def test_use_main(self):
117
        sys.argv[1:] = [test_src]
118
        sys.argv.extend(test_opt_nomain)
119
        sys.argv.append('--iutest-use-main')
120
        with self.assertRaises(SystemExit) as cm:
121
            iuwandbox.main()
122
        output = self.dump()
123
        self.assertEqual(cm.exception.code, 0, output)
124
        self.assertRegex(output, '.*OK.*')
125
126
    def test_define_wandbox(self):
127
        sys.argv[1:] = [test_src]
128
        sys.argv.extend(test_opt)
129
        sys.argv.extend(test_opt_dryrun)
130
        sys.argv.extend(test_opt_verbose)
131
        sys.argv.append('-f"-DTEST"')
132
        with self.assertRaises(SystemExit) as cm:
133
            iuwandbox.main()
134
        output = self.dump()
135
        self.assertEqual(cm.exception.code, 0, output)
136
        self.assertRegex(output, '.*-D__WANDBOX__.*')
137
        self.assertRegex(output, '.*-DTEST.*')
138
139
    def test_boosttest_workarround(self):
140
        sys.argv[1:] = [test_src]
141
        sys.argv.extend(test_opt_nomain)
142
        sys.argv.extend(['--boost', '1.65.0'])
143
        with self.assertRaises(SystemExit) as cm:
144
            iuwandbox.main()
145
        output = self.dump()
146
        self.assertEqual(cm.exception.code, 1, output)
147
        self.assertRegex(output, '.*hint:.*')
148
        self.assertRegex(output, '.*If you do not use boost test, please specify the file with the main function first..*')
149
150
    def test_run(self):
151
        sys.argv[1:] = [test_src]
152
        sys.argv.extend(test_opt)
153
        print(sys.argv)
154
        with self.assertRaises(SystemExit) as cm:
155
            iuwandbox.main()
156
        output = self.dump()
157
        self.assertEqual(cm.exception.code, 0, output)
158
        self.assertRegex(output, r'\[ \s+OK \]')
159
        self.assertFalse('-Wmisleading-indentation' in output)
160
161
    def test_same_filename(self):
162
        sys.argv[1:] = ['src/main.cpp', 'src/A/sample.cpp', 'src/B/sample.cpp']
163
        sys.argv.extend(test_opt_nomain)
164
        print(sys.argv)
165
        with self.assertRaises(SystemExit) as cm:
166
            iuwandbox.main()
167
        output = self.dump()
168
        self.assertEqual(cm.exception.code, 0, output)
169
        self.assertRegex(output, '.*OK.*')
170
171
    def test_no_min_run(self):
172
        sys.argv[1:] = [test_src]
173
        sys.argv.extend(test_opt)
174
        sys.argv.extend(test_opt_no_min)
175
        print(sys.argv)
176
        with self.assertRaises(SystemExit) as cm:
177
            iuwandbox.main()
178
        output = self.dump()
179
        self.assertEqual(cm.exception.code, 0, output)
180
        self.assertRegex(output, r'\[ \s+OK \]')
181
        self.assertFalse('-Wmisleading-indentation' in output)
182
183
    def test_make_run(self):
184
        sys.argv[1:] = [test_src]
185
        sys.argv.extend(test_opt)
186
        sys.argv.extend(['--make'])
187
        print(sys.argv)
188
        with self.assertRaises(SystemExit) as cm:
189
            iuwandbox.main()
190
        output = self.dump()
191
        self.assertEqual(cm.exception.code, 0, output)
192
        self.assertRegex(output, r'\[ \s+OK \]')
193
194
195
if __name__ == "__main__":
196
    unittest.main()
197