File: catch_test_common.cpp

package info (click to toggle)
libapache2-mod-tile 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,872 kB
  • sloc: cpp: 18,151; ansic: 7,574; sh: 980; makefile: 163; xml: 27
file content (181 lines) | stat: -rw-r--r-- 4,297 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2007 - 2023 by mod_tile contributors (see AUTHORS file)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see http://www.gnu.org/licenses/.
 */

#include <glib.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unistd.h>

#include "catch_test_common.hpp"
#include "pstreams/pstream.hpp"

extern int foreground;

std::string err_log_lines, out_log_lines;

captured_stdio captured_stderr;
captured_stdio captured_stdout;

int run_command(std::string file, std::vector<std::string> argv, std::string input)
{
	auto mode = redi::pstreams::pstdout | redi::pstreams::pstderr | redi::pstreams::pstdin;

	argv.insert(argv.begin(), file);

	redi::pstream proc(file, argv, mode);

	std::string line;
	err_log_lines = "";
	out_log_lines = "";

	if (!input.empty()) {
		proc << input << redi::peof;
	}

	while (std::getline(proc.err(), line)) {
		err_log_lines += line;
	}

	proc.clear();

	while (std::getline(proc.out(), line)) {
		out_log_lines += line;
	}

	return proc.close();
}

std::string read_stderr(int buffer_size)
{
	char buffer[buffer_size];
	read(captured_stderr.pipes[0], buffer, buffer_size);
	return buffer;
}

void capture_stderr()
{
	// Flush stderr first if you've previously printed something
	fflush(stderr);

	// Save stderr so it can be restored later
	int temp_stderr;
	temp_stderr = dup(fileno(stderr));

	// Redirect stderr to a new pipe
	int pipes[2];
	pipe(pipes);
	dup2(pipes[1], fileno(stderr));

	captured_stderr.temp_fd = temp_stderr;
	captured_stderr.pipes[0] = pipes[0];
	captured_stderr.pipes[1] = pipes[1];
}

std::string get_captured_stderr(bool print)
{
	// Terminate captured output with a zero
	write(captured_stderr.pipes[1], "", 1);

	// Restore stderr
	fflush(stderr);
	dup2(captured_stderr.temp_fd, fileno(stderr));

	// Save & return the captured stderr
	std::string log_lines = read_stderr();

	if (print) {
		std::cout << "err_log_lines: " << log_lines << "\n";
	}

	return log_lines;
}

std::string read_stdout(int buffer_size)
{
	char buffer[buffer_size];
	read(captured_stdout.pipes[0], buffer, buffer_size);
	return buffer;
}

void capture_stdout()
{
	// Flush stdout first if you've previously printed something
	fflush(stdout);

	// Save stdout so it can be restored later
	int temp_stdout;
	temp_stdout = dup(fileno(stdout));

	// Redirect stdout to a new pipe
	int pipes[2];
	pipe(pipes);
	dup2(pipes[1], fileno(stdout));

	captured_stdout.temp_fd = temp_stdout;
	captured_stdout.pipes[0] = pipes[0];
	captured_stdout.pipes[1] = pipes[1];
}

std::string get_captured_stdout(bool print)
{
	// Terminate captured output with a zero
	write(captured_stdout.pipes[1], "", 1);

	// Restore stdout
	fflush(stdout);
	dup2(captured_stdout.temp_fd, fileno(stdout));

	// Save & return the captured stdout
	std::string log_lines = read_stdout();

	if (print) {
		std::cout << "out_log_lines: " << log_lines << "\n";
	}

	return log_lines;
}

void start_capture(bool debug)
{
	foreground = debug ? 1 : 0;

	if (debug) {
		setenv("G_MESSAGES_DEBUG", "all", 1);
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 79
		// https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3710
		std::cout << "Resetting G_MESSAGES_DEBUG env var in runtime no longer has an effect.\n";
		const gchar *domains[] = {"all", NULL};
		g_log_writer_default_set_debug_domains(domains);
#endif
	}

	capture_stderr();
	capture_stdout();
}

std::tuple<std::string, std::string> end_capture(bool print)
{
	setenv("G_MESSAGES_DEBUG", "", 1);
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 79
	g_log_writer_default_set_debug_domains(NULL);
#endif
	foreground = 0;

	return std::tuple<std::string, std::string>(get_captured_stderr(print), get_captured_stdout(print));
}