Debian Codegraph @

testsuite/tools/run-command

raw
1 #! /usr/bin/env python3
2
3 from pathlib import Path
4 import os, sys, subprocess, difflib
5
6 def run_command(command, name, inputdir, resultsdir):
7 success = True
8
9 ret = subprocess.run (command, shell=True, capture_output=True, text=True)
10 output = ret.stdout.splitlines()
11
12 path = Path(inputdir) / (name + '.expected')
13 expected = path.open().read().splitlines()
14
15 for s in expected:
16 print(s)
17 sys.exit(0)
18
19 diff = list(difflib.unified_diff(output, expected))
20 if len(diff) > 0:
21 path = Path(resultsdir) / (name + '.diff')
22 path.parent.mkdir(parents=True, exist_ok=True)
23 path.open(mode='w').writelines(s + '\n' for s in diff)
24 success = False
25
26 errors = ret.stderr.splitlines(keepends=True)
27 path = Path(resultsdir) / (name + '.errors')
28 try:
29 expected_errors = path.open().read().splitlines()
30 except OSError:
31 expected_errors = ''
32
33 diff = list(difflib.unified_diff(errors, expected_errors))
34 if len(diff) > 0:
35 path = Path(resultsdir) / (name + '.errors.diff')
36 path.parent.mkdir(parents=True, exist_ok=True)
37 path.open(mode='w').writelines(s + '\n' for s in diff)
38 success = False
39
40 if not success:
41 path = Path(resultsdir) / (name + '.out')
42 path.parent.mkdir(parents=True, exist_ok=True)
43 path.open(mode='w').writelines(s + '\n' for s in output)
44
45 path = Path(resultsdir) / (name + '.ref')
46 path.parent.mkdir(parents=True, exist_ok=True)
47 path.open(mode='w').writelines(s + '\n' for s in expected)
48
49 sys.exit(1)
50
51
52 if __name__ == '__main__':
53
54 if len(sys.argv) != 5:
55 print('usage: run-command COMMAND NAME INPUTDIR OUTPUTDIR')
56 sys.exit(1)
57
58 run_command(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])