File: CxxTestMain.cpp

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (211 lines) | stat: -rw-r--r-- 5,561 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <TestSupport.h>
#include "../tut/tut_reporter.h"
#include "../support/valgrind.h"
#include <oxt/initialize.hpp>
#include <oxt/system_calls.hpp>
#include <string>
#include <map>
#include <vector>
#include <signal.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>

#include <MultiLibeio.cpp>
#include <Utils.h>
#include <Utils/IOUtils.h>
#include <Utils/StrIntUtils.h>
#include <json/json.h>

using namespace std;

namespace tut {
	test_runner_singleton runner;
}

typedef tut::groupnames::const_iterator groupnames_iterator;

/** All available groups. */
static tut::groupnames allGroups;

/** Whether the user wants to run all test groups, or only the specified test groups. */
static enum { RUN_ALL_GROUPS, RUN_SPECIFIED_GROUPS } runMode = RUN_ALL_GROUPS;

/** The test groups and test numbers that the user wants to run.
 * Only meaningful if runMode == RUN_SPECIFIED_GROUPS.
 */
static map< string, vector<int> > groupsToRun;


static void
usage(int exitCode) {
	printf("Usage: ./Apache2ModuleTests [options]\n");
	printf("Runs the unit tests for the Apache 2 module.\n\n");
	printf("Options:\n");
	printf("  -g GROUP_NAME   Instead of running all unit tests, only run the test group\n");
	printf("                  named GROUP_NAME. You can specify -g multiple times, which\n");
	printf("                  will result in only the specified test groups being run.\n\n");
	printf("                  Available test groups:\n\n");
	for (groupnames_iterator it = allGroups.begin(); it != allGroups.end(); it++) {
		printf("                    %s\n", it->c_str());
	}
	printf("\n");
	printf("  -h              Print this usage information.\n");
	exit(exitCode);
}

static bool
groupExists(const string &name) {
	for (groupnames_iterator it = allGroups.begin(); it != allGroups.end(); it++) {
		if (name == *it) {
			return true;
		}
	}
	return false;
}

static void
parseGroupSpec(const char *spec, string &groupName, vector<int> &testNumbers) {
	testNumbers.clear();
	if (*spec == '\0') {
		groupName = "";
		return;
	}

	vector<string> components;
	split(spec, ':', components);
	groupName = components[0];
	if (components.size() > 1) {
		string testNumbersSpec = components[1];
		components.clear();
		split(testNumbersSpec, ',', components);
		vector<string>::const_iterator it;
		for (it = components.begin(); it != components.end(); it++) {
			testNumbers.push_back(atoi(*it));
		}
	}
}

static void
parseOptions(int argc, char *argv[]) {
	for (int i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-h") == 0) {
			usage(0);
		} else if (strcmp(argv[i], "-g") == 0) {
			if (argv[i + 1] == NULL) {
				fprintf(stderr, "*** ERROR: A -g option must be followed by a test group name.\n");
				exit(1);
			}

			string groupName;
			vector<int> testNumbers;
			parseGroupSpec(argv[i + 1], groupName, testNumbers);

			if (!groupExists(groupName)) {
				fprintf(stderr,
					"*** ERROR: Invalid test group '%s'. Available test groups are:\n\n",
					argv[i + 1]);
				for (groupnames_iterator it = allGroups.begin(); it != allGroups.end(); it++) {
					printf("%s\n", it->c_str());
				}
				exit(1);
			} else {
				runMode = RUN_SPECIFIED_GROUPS;
				groupsToRun[groupName] = testNumbers;
				i++;
			}
		} else {
			fprintf(stderr, "*** ERROR: Unknown option: %s\n", argv[i]);
			fprintf(stderr, "Please pass -h for a list of valid options.\n");
			exit(1);
		}
	}
}

static int
doNothing(eio_req *req) {
	return 0;
}

static void
loadConfigFile() {
	Json::Reader reader;
	if (!reader.parse(readAll("config.json"), testConfig)) {
		fprintf(stderr, "Cannot parse config.json: %s\n",
			reader.getFormattedErrorMessages().c_str());
		exit(1);
	}
}

static void
abortHandler(int signo, siginfo_t *info, void *ctx) {
	// Stop itself so that we can attach it to gdb.
	static const char message[] = "Crash handler called!\n";
	write(STDERR_FILENO, message, sizeof(message) - 1);
	raise(SIGSTOP);
	// Run default signal handler.
	raise(signo);
}

static void
installAbortHandler() {
	const char *stopOnAbort = getenv("STOP_ON_ABORT");
	if (stopOnAbort != NULL && *stopOnAbort != '\0' && *stopOnAbort != '0') {
		struct sigaction action;
		action.sa_sigaction = abortHandler;
		action.sa_flags = SA_RESETHAND | SA_SIGINFO;
		sigemptyset(&action.sa_mask);
		sigaction(SIGABRT, &action, NULL);
		sigaction(SIGSEGV, &action, NULL);
		sigaction(SIGBUS, &action, NULL);
		sigaction(SIGFPE, &action, NULL);
	}
}

int
main(int argc, char *argv[]) {
	signal(SIGPIPE, SIG_IGN);
	setenv("RAILS_ENV", "production", 1);
	setenv("TESTING_PASSENGER", "1", 1);
	setenv("PYTHONDONTWRITEBYTECODE", "1", 1);
	unsetenv("PASSENGER_TMPDIR");
	unsetenv("PASSENGER_TEMP_DIR");
	oxt::initialize();
	oxt::setup_syscall_interruption_support();
    
	tut::reporter reporter;
	tut::runner.get().set_callback(&reporter);
	allGroups = tut::runner.get().list_groups();
	parseOptions(argc, argv);
	
	char path[PATH_MAX + 1];
	getcwd(path, PATH_MAX);
	resourceLocator = new ResourceLocator(extractDirName(path));

	Passenger::MultiLibeio::init();
	eio_set_idle_timeout(9999); // Never timeout.
	eio_set_min_parallel(1);
	eio_set_max_parallel(1);
	if (RUNNING_ON_VALGRIND) {
		// Start an EIO thread to warm up Valgrind.
		eio_nop(0, doNothing, NULL);
	}

	loadConfigFile();
	installAbortHandler();
	
	bool all_ok = true;
	if (runMode == RUN_ALL_GROUPS) {
		tut::runner.get().run_tests();
	} else {
		tut::runner.get().run_tests(groupsToRun);
	}
	all_ok = reporter.all_ok();
	Passenger::MultiLibeio::shutdown();
	if (all_ok) {
		return 0;
	} else {
		return 1;
	}
}