This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
res_diff.pl
executable file
·156 lines (117 loc) · 2.8 KB
/
res_diff.pl
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
#!/usr/bin/perl
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2017-2021 Cyril Hrubis <[email protected]>
#
# Compares two json formatted result files
use strict;
use warnings;
use JSON;
use Data::Dumper;
sub load_json
{
my ($fname) = @_;
local $/;
open(my $fh, '<', $fname) or die("Can't open $fname $!");
return <$fh>;
}
sub json_to_hash
{
my ($json) = @_;
my $results = $json->{'tests'}->{'results'};
my %tid_hash;
foreach my $test (@$results) {
$tid_hash{$test->{'tid'}} = $test;
}
return \%tid_hash;
}
my @res_str = ('passed', 'failed', 'skipped', 'broken', 'warnings');
sub print_diff
{
my ($res_a, $res_b) = @_;
print("\n-------------------------------\n");
print("$res_a->{'tid'}\n");
foreach my $key (@res_str) {
print("$key: $res_a->{$key} -> $res_b->{$key}\n") if ($res_a->{$key} != $res_b->{$key});
}
my $mlen = 0;
foreach my $line (@{$res_a->{'log'}}) {
$mlen = length($line) if length($line) > $mlen;
}
print("\n");
for (my $i = 0; $i < @{$res_a->{'log'}}; $i++) {
my $line_a = $res_a->{'log'}->[$i];
my $line_b = $res_b->{'log'}->[$i];
print("$line_a");
if (not $line_b) {
print("\n");
next;
}
for (my $j = length($line_a); $j < $mlen; $j++) {
print(" ");
}
print(" | ");
print("$line_b\n");
}
for (my $i = @{$res_a->{'log'}}; $i < @{$res_b->{'log'}}; $i++) {
my $line_b = $res_b->{'log'}->[$i];
for (my $j = 0; $j < $mlen; $j++) {
print(" ");
}
print(" | ");
print("$line_b\n");
}
}
sub check_diff
{
my ($res_a, $res_b) = @_;
if ($res_a->{'passed'} != $res_b->{'passed'} or
$res_a->{'failed'} != $res_b->{'failed'} or
$res_a->{'skipped'} != $res_b->{'skipped'} or
$res_a->{'broken'} != $res_b->{'broken'} or
$res_a->{'warnings'} != $res_b->{'warnings'}) {
print_diff($res_a, $res_b);
}
}
sub print_res
{
my ($res) = @_;
print("$res->{'tid'}\n");
foreach my $key (@res_str) {
print("$key: $res->{$key}\t");
}
print("\n");
}
sub compare_res_hashes
{
my ($res_hash_a, $res_hash_b) = @_;
my @removed;
my @added;
my @compare;
foreach my $key (keys %$res_hash_a) {
if (exists($res_hash_b->{$key})) {
push(@compare, $key);
} else {
push(@removed, $key);
}
}
foreach my $key (keys %$res_hash_b) {
push(@added, $key) if not exists($res_hash_a->{$key});
}
print("==== Newly added tests ====\n");
foreach my $key (@added) {
print_res($res_hash_b->{$key});
}
print("\n");
print("==== Removed tests ====\n");
foreach my $key (@removed) {
print("$key\n");
}
print("\n");
printf("==== Result difference ====\n");
foreach my $key (@compare) {
check_diff($res_hash_a->{$key}, $res_hash_b->{$key});
}
}
my $res_hash_a = json_to_hash(decode_json(load_json($ARGV[0])));
my $res_hash_b = json_to_hash(decode_json(load_json($ARGV[1])));
compare_res_hashes($res_hash_a, $res_hash_b);