forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlib_exec.c
84 lines (72 loc) · 2.38 KB
/
tlib_exec.c
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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <stdio.h>
#include "util_memory.h"
#include "util_strings.h"
#include "tlib_main.h"
nr_status_t tlib_pass_if_exec_f(const char* what,
const char* cmd,
int notdiff,
const char* file,
int line) {
FILE* oput = popen(cmd, "r");
char* buf = 0;
char tmp[4096];
int blen = 0;
int slen;
while (0 != (slen = (int)fread(tmp, 1, sizeof(tmp) - 1, oput))) {
tmp[slen] = 0;
buf = (char*)nr_realloc(buf, blen + slen + 2);
nr_strcpy(buf + blen, tmp);
blen += slen;
}
slen = pclose(oput);
if (0 != slen) {
printf("FAIL [%s:%d]: exec: %s\n", file, line, what);
printf(">>> Command: %s\n", cmd);
if (0 == notdiff) {
printf(
">>> Output from diff is below. Lines beginning with a + are lines "
"that\n");
printf(
">>> appear in the generated file but not in the reference file, "
"and\n");
printf(
">>> lines that begin with a - are lines that appear in the "
"reference\n");
printf(">>> file but not in the generated output.\n");
}
if (buf) {
printf("%s\n", buf);
}
tlib_did_fail();
nr_free(buf);
return NR_FAILURE;
}
tlib_did_pass();
nr_free(buf);
return NR_SUCCESS;
}
nr_status_t tlib_pass_if_not_diff_f(const char* result_file,
const char* expect_file,
const char* transformation,
int do_sort,
int not_diff,
const char* file,
int line) {
nr_status_t r;
char cmdbuf[2048];
snprintf(cmdbuf, sizeof(cmdbuf), "cat %s | %s | %s | diff -u %s -",
result_file, transformation, do_sort ? " LC_ALL=C sort " : " cat",
expect_file);
r = tlib_pass_if_exec_f("compare logfile", cmdbuf, not_diff, file, line);
if (r != NR_SUCCESS) {
snprintf(cmdbuf, sizeof(cmdbuf), "cat %s | %s | %s > %s", result_file,
transformation, do_sort ? " LC_ALL=C sort " : " cat", expect_file);
printf("To regenerate the expected output, do:\n%s\n", cmdbuf);
}
return r;
}