aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tools/pg-make-test/update-test-status/__main__.py
blob: 0a1f9852a45aec0f35a78e4690ee29f7c1bba1f3 (plain) (blame)
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
import os
from collections import namedtuple
from datetime import date
import csv
from pathlib import Path
import re
import click


TestCase = namedtuple('TestCase', 'statements successful ratio')


def load_testcases(filename):
    testcases = {}

    with open(filename, newline='') as infile:
        reader = csv.DictReader(infile, delimiter=',')
        for row in reader:
            testcases[row['testcase']] = TestCase(row['statements'], row['successful'], row['ratio'])

    return testcases


reIncrement = re.compile(r'(\d+)(?: ?\(+ ?(\d+)\))?')


def format_success_count(old, new):
    m = reIncrement.search(old)
    if m is None:
        return str(new)

    old_count = int(m[1])
    new_count = int(new)

    if old_count < new_count:
        return f'{new_count} (+{new_count - old_count})'

    # we need it to keep old increments, if count stays the same
    if old_count == new_count:
        return str(old)

    return str(new)


reTableRow = re.compile(r'^\|\| \d+ \|')


@click.command()
@click.argument("reportfile", type=str, nargs=1)
@click.argument("statusfile", type=str, nargs=1)
def cli(reportfile, statusfile):
    today = date.today().strftime('%d.%m.%Y')

    testcases = load_testcases(reportfile)

    outfile = Path(statusfile).with_suffix('.tmp')
    with open(statusfile, 'r') as fin, open(outfile, 'w') as fout:
        for line in fin:
            if reTableRow.match(line):
                testno, testname, stmts_count, success_count, ratio, when_updated, notes = line.split(' | ')
                try:
                    testcase = testcases[testname]
                    new_success_count = format_success_count(success_count, testcase.successful)
                    fout.write(
                        ' | '.join(
                            [
                                testno,
                                testname,
                                testcase.statements,
                                new_success_count,
                                testcase.ratio,
                                today if new_success_count != success_count else when_updated,
                                notes,
                            ]
                        )
                    )
                except KeyError:
                    fout.write(line)
            else:
                fout.write(line)

    oldfile = Path(statusfile).with_suffix('.old')
    os.rename(statusfile, oldfile)
    os.rename(outfile, statusfile)


if __name__ == '__main__':
    cli()