File: test_bash.py

package info (click to toggle)
kcov 43%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,600 kB
  • sloc: cpp: 12,617; ansic: 2,379; python: 2,001; sh: 333; makefile: 133; javascript: 65; xml: 7; asm: 4
file content (514 lines) | stat: -rw-r--r-- 18,874 bytes parent folder | download | duplicates (3)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import os
import platform
import sys
import unittest

import libkcov
from libkcov import cobertura


class BashBase(libkcov.TestCase):
    def doTest(self, args):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/shell-main "
            + args
        )

        return cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")


class bash_coverage(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/shell-main"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "shell-main", 3) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 4) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 13) == 0
        assert cobertura.hitsPerLine(dom, "shell-main", 26) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 30) == 5
        assert cobertura.hitsPerLine(dom, "shell-main", 34) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 38) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 5) == 1

        assert cobertura.hitsPerLine(dom, "short-test.sh", 5) == 11
        assert cobertura.hitsPerLine(dom, "shell-main", 128) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 132) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 135) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 136) == 1

        assert b"I'm echoing to stderr via some" in o


# Very limited, will expand once it's working better
class bash_coverage_debug_trap(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " --bash-method=DEBUG "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/shell-main 5"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "shell-main", 3) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 4) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 22) == 1


class bash_short_file(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/short-test.sh"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/short-test.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "short-test.sh", 5) == 11


class bash_heredoc_backslashes(BashBase):
    def runTest(self):
        dom = self.doTest("5")

        assert cobertura.hitsPerLine(dom, "shell-main", 52) == 4
        assert cobertura.hitsPerLine(dom, "shell-main", 53) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 55) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 59) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 61) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 62) is None


class bash_heredoc_special_cases_issue_44(BashBase):
    def runTest(self):
        dom = self.doTest("")
        assert cobertura.hitsPerLine(dom, "shell-main", 77) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 83) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 88) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 93) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 109) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 118) == 1


class bash_non_empty_braces(BashBase):
    def runTest(self):
        dom = self.doTest("")
        assert cobertura.hitsPerLine(dom, "shell-main", 102) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 103) == 3


class bash_coverage_tricky(BashBase):
    def runTest(self):
        dom = self.doTest("5")
        assert cobertura.hitsPerLine(dom, "shell-main", 36) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 44) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 7) is None


class bash_honor_signal(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.sources
            + "/tests/setpgid-kill/test-script.sh "
            + self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/trap.sh",
            False,
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/trap.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "trap.sh", 5) == 1


class bash_accumulate_data(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/unitundertest.sh 1"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/unitundertest.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 6) == 1
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 16) == 0
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/unitundertest.sh 2"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/unitundertest.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 6) == 1
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 16) == 1


class bash_accumulate_changed_data(libkcov.TestCase):
    # Not sure why, but for now...
    @unittest.skipUnless(platform.machine() in ["x86_64", "i686", "i386"], "Only for x86")
    def runTest(self):
        os.system("mkdir -p /tmp/test-kcov")
        os.system("cp " + self.sources + "/tests/bash/shell-main /tmp/test-kcov")
        os.system("cp " + self.sources + "/tests/bash/other.sh /tmp/test-kcov")
        rv, o = self.do(self.kcov + " " + self.outbase + "/kcov /tmp/test-kcov/shell-main 5 9")

        dom = cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "shell-main", 40) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 67) == 2
        assert cobertura.hitsPerLine(dom, "other.sh", 6) == 2
        os.system("echo \"echo 'arne-anka'\" >> /tmp/test-kcov/shell-main")
        rv, o = self.do(self.kcov + " " + self.outbase + "/kcov /tmp/test-kcov/shell-main 5")

        dom = cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "shell-main", 40) == 0
        assert cobertura.hitsPerLine(dom, "shell-main", 67) == 0
        assert cobertura.hitsPerLine(dom, "other.sh", 6) == 3


class bash_merge_data_issue_38(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/unitundertest.sh 1"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/unitundertest.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 6) == 1
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 16) == 0
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/unitundertest.sh 2"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/unitundertest.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 6) == 1
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 16) == 1
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/unitundertest.sh all"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/unitundertest.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 36) == 1
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 30) == 1
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 33) == 1
        assert cobertura.hitsPerLine(dom, "unitundertest.sh", 36) == 1


class bash_issue_116_arithmetic_and_heredoc_issue_117_comment_within_string(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/shell-main"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "shell-main", 142) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 149) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 151) == 1
        assert cobertura.hitsPerLine(dom, "shell-main", 152) == 1


class bash_multiline_quotes(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/multiline-alias.sh"
        )

        assert b"echo called test_alias" not in o
        dom = cobertura.parseFile(self.outbase + "/kcov/multiline-alias.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "multiline-alias.sh", 6) == 1


class bash_multiline_backslashes(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/multiline-backslash.sh"
        )

        assert b"function_name" not in o


class bash_no_executed_lines(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/no-executed-statements.sh"
        )

        assert b"echo called test_alias" not in o
        dom = cobertura.parseFile(self.outbase + "/kcov/no-executed-statements.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "no-executed-statements.sh", 4) == 0


class bash_stderr_redirection(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/redirect-stderr.sh"
        )

        assert b"kcov" not in o
        rv, o = self.do(
            self.kcov
            + " --debug-force-bash-stderr "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/redirect-stderr.sh"
        )
        assert b"kcov" not in o


class bash_dollar_var_replacement(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/dollar-var-replacements.sh"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/dollar-var-replacements.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "dollar-var-replacements.sh", 2) == 1
        assert cobertura.hitsPerLine(dom, "dollar-var-replacements.sh", 4) == 1
        assert cobertura.hitsPerLine(dom, "dollar-var-replacements.sh", 5) == 1


# Issue #152
class bash_done_eof(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/shell-main 5"
        )
        dom = cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "shell-main", 163) is None
        assert cobertura.hitsPerLine(dom, "shell-main", 169) is None


# Issue #154
class bash_eof_backtick(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/shell-main"
        )
        dom = cobertura.parseFile(self.outbase + "/kcov/shell-main/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "shell-main", 180) == 1


class bash_subshell(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/subshell.sh"
        )
        dom = cobertura.parseFile(self.outbase + "/kcov/subshell.sh/cobertura.xml")
        self.assertIsNone(cobertura.hitsPerLine(dom, "subshell.sh", 1))
        self.assertEqual(2, cobertura.hitsPerLine(dom, "subshell.sh", 4))
        self.assertEqual(0, cobertura.hitsPerLine(dom, "subshell.sh", 8))


class bash_handle_all_output(libkcov.TestCase):
    def runTest(self):
        script = "handle-all-output.sh"
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/" + script,
            timeout=5.0,
        )
        self.assertEqual(0, rv, "kcov exited unsuccessfully")
        dom = cobertura.parseFile(self.outbase + "/kcov/" + script + "/cobertura.xml")
        self.assertIsNone(cobertura.hitsPerLine(dom, script, 1))
        self.assertEqual(1000, cobertura.hitsPerLine(dom, script, 4))


class bash_exit_status(libkcov.TestCase):
    def runTest(self):
        noKcovRv, o = self.doCmd(self.sources + "/tests/bash/shell-main 5")
        rv, o = self.do(
            self.kcov + " " + self.outbase + "/kcov " + self.sources + "/tests/bash/shell-main 5"
        )

        assert rv == noKcovRv


# Issue 180
class bash_ignore_uncovered(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " --exclude-region=CUSTOM_RANGE_START:CUSTOM_RANGE_END "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/other.sh"
        )
        dom = cobertura.parseFile(self.outbase + "/kcov/other.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "other.sh", 22) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 23) == 1

        assert cobertura.hitsPerLine(dom, "other.sh", 26) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 27) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 28) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 29) == 1

        assert cobertura.hitsPerLine(dom, "other.sh", 32) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 34) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 35) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 36) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 37) == 1

        assert cobertura.hitsPerLine(dom, "other.sh", 40) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 42) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 43) == 1

        assert cobertura.hitsPerLine(dom, "other.sh", 47) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 48) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 49) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 51) == 1

        # Rust stuff
        assert cobertura.hitsPerLine(dom, "other.sh", 54) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 55) == 1


# Issue #224
class bash_can_find_non_executed_scripts(libkcov.TestCase):
    @unittest.skipUnless(platform.machine() in ["x86_64", "i686", "i386"], "Only for x86")
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/first-dir/a.sh 5"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/a.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "a.sh", 5) == 1
        # Not executed
        assert cobertura.hitsPerLine(dom, "c.sh", 3) == 0


class bash_can_find_non_executed_scripts_manually(libkcov.TestCase):
    @unittest.skipUnless(platform.machine() in ["x86_64", "i686", "i386"], "Only for x86")
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " --bash-parse-files-in-dir="
            + self.sources
            + "/tests/bash "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/first-dir/a.sh 5"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/a.sh/cobertura.xml")
        # Not executed
        assert cobertura.hitsPerLine(dom, "c.sh", 3) == 0
        assert cobertura.hitsPerLine(dom, "other.sh", 3) == 0


class bash_can_ignore_non_executed_scripts(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " --bash-dont-parse-binary-dir "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/first-dir/a.sh 5"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/a.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "a.sh", 5) == 1
        # Not included in report
        assert cobertura.hitsPerLine(dom, "c.sh", 3) is None
        assert cobertura.hitsPerLine(dom, "other.sh", 3) is None


class bash_can_ignore_function_with_spaces(libkcov.TestCase):
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " --bash-dont-parse-binary-dir "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/function-with-spaces.sh"
        )

        dom = cobertura.parseFile(self.outbase + "/kcov/function-with-spaces.sh/cobertura.xml")
        assert cobertura.hitsPerLine(dom, "function-with-spaces.sh", 5) is None
        assert cobertura.hitsPerLine(dom, "function-with-spaces.sh", 6) == 1
        assert cobertura.hitsPerLine(dom, "function-with-spaces.sh", 9) is None
        assert cobertura.hitsPerLine(dom, "function-with-spaces.sh", 10) is None
        assert cobertura.hitsPerLine(dom, "function-with-spaces.sh", 11) == 1


class bash_drain_stdout_without_return(libkcov.TestCase):
    @unittest.skipUnless(platform.machine() in ["x86_64", "i686", "i386"], "Only for x86")
    @unittest.skipIf(sys.platform.startswith("darwin"), "Not for OSX")
    def runTest(self):
        rv, o = self.do(
            self.kcov
            + " "
            + self.outbase
            + "/kcov "
            + self.sources
            + "/tests/bash/long-output-without-return.sh",
            timeout=5.0,
        )
        self.assertEqual(0, rv, "kcov exited unsuccessfully")
        dom = cobertura.parseFile(
            self.outbase + "/kcov/long-output-without-return.sh/cobertura.xml"
        )
        self.assertIsNone(cobertura.hitsPerLine(dom, "long-output-without-return.sh", 1))
        self.assertEqual(32768, cobertura.hitsPerLine(dom, "long-output-without-return.sh", 4))