1 /* TESH (Test Shell) -- mini shell specialized in running test units */
3 /* Copyright (c) 2007-2013. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 /* specific to Borland Compiler */
17 XBT_LOG_NEW_DEFAULT_CATEGORY(tesh, "TEst SHell utility");
20 int timeout_value = 5; /* child timeout value */
21 int sort_len = 19; /* length of the prefix to sort */
23 int coverage = 0; /* whether the code coverage is enable */
26 const char *testsuite_name;
30 static void handle_line(const char *filepos, char *line)
33 xbt_str_rtrim(line + 2, "\n");
36 XBT_DEBUG("rctx={%s,in={%d,>>%10s<<},exp={%d,>>%10s<<},got={%d,>>%10s<<}}",
38 rctx->input->used, rctx->input->data,
39 rctx->output_wanted->used,rctx->output_wanted->data,
40 rctx->output_got->used, rctx->output_got->data);
42 XBT_DEBUG("[%s] %s", filepos, line);
49 /* further trim useless chars which are significant for in/output */
50 xbt_str_rtrim(line + 2, " \t");
52 /* Deal with CD commands here, not in rctx */
53 if (!strncmp("cd ", line + 2, 3)) {
59 /* search beginning */
60 while (*(dir++) == ' ');
62 XBT_VERB("Saw cd '%s'", dir);
64 XBT_ERROR("Chdir to %s failed: %s", dir, strerror(errno));
65 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
66 rctx_armageddon(rctx, 4);
69 } /* else, pushline */
74 rctx_pushline(filepos, line[0], line + 2 /* pass '$ ' stuff */ );
78 XBT_INFO("[%s] %s", filepos, line + 2);
81 XBT_CRITICAL("[%s] %s", filepos, line + 2);
85 XBT_ERROR("[%s] Syntax error: %s", filepos, line);
86 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
87 rctx_armageddon(rctx, 1);
92 static void handle_suite(const char *filename, FILE * IN)
102 xbt_strbuff_t buff = NULL;
104 buff = xbt_strbuff_new();
107 while (xbt_getline(&line, &len, IN) != -1) {
110 /* Count the line length while checking wheather it's blank */
114 while (line[linelen] != '\0') {
115 if (line[linelen] != ' ' && line[linelen] != '\t'
116 && line[linelen] != '\n')
122 if (!rctx->cmd && !rctx->is_empty) {
123 XBT_ERROR("[%d] Error: no command found in this chunk of lines.",
125 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
126 rctx_armageddon(rctx, 1);
134 /* Deal with \ at the end of the line, and call handle_line on result */
136 if (linelen > 1 && line[linelen - 2] == '\\') {
137 if (linelen > 2 && line[linelen - 3] == '\\') {
138 /* Damn. Escaped \ */
139 line[linelen - 2] = '\n';
140 line[linelen - 1] = '\0';
143 line[linelen - 2] = '\0';
145 buffbegin = line_num;
149 if (buff->used || to_be_continued) {
150 xbt_strbuff_append(buff, line);
152 if (!to_be_continued) {
153 snprintf(file_pos, 256, "%s:%d", filename, buffbegin);
154 handle_line(file_pos, buff->data);
155 xbt_strbuff_empty(buff);
159 snprintf(file_pos, 256, "%s:%d", filename, line_num);
160 handle_line(file_pos, line);
163 /* Check that last command of the file ran well */
167 /* Wait all background commands */
173 xbt_strbuff_free(buff);
177 static void parse_environ()
183 env = xbt_dict_new_homogeneous(xbt_free_f);
184 for (i = 0; environ[i]; i++) {
187 key = bprintf("%.*s", (int) (eq - p), p);
188 xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL);
193 int main(int argc, char *argv[])
197 char *suitename = NULL;
198 struct sigaction newact;
200 XBT_LOG_CONNECT(tesh);
201 xbt_init(&argc, argv);
205 /* Ignore pipe issues.
206 They will show up when we try to send data to dead buddies,
207 but we will stop doing so when we're done with provided input */
208 memset(&newact, 0, sizeof(newact));
209 newact.sa_handler = SIG_IGN;
210 sigaction(SIGPIPE, &newact, NULL);
213 for (i = 1; i < argc; i++) {
214 if (!strcmp(argv[i], "--cd")) {
216 XBT_ERROR("--cd argument requires an argument");
219 if (chdir(argv[i + 1])) {
220 XBT_ERROR("Cannot change directory to %s: %s", argv[i + 1],
224 XBT_INFO("Change directory to %s", argv[i + 1]);
225 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
228 } else if (!strcmp(argv[i], "--setenv" )) {
230 XBT_ERROR("--setenv argument requires an argument");
233 char *eq = strchr(argv[i+1], '=');
234 xbt_assert(eq,"The argument of --setenv must contain a '=' (got %s instead)",argv[i+1]);
235 char *key = bprintf("%.*s", (int) (eq - argv[i+1]), argv[i+1]);
236 xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL);
237 XBT_INFO("setting environment variable '%s' to '%s'", key, eq+1);
239 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
242 } else if (!strcmp(argv[i], "--cfg" )) {
244 XBT_ERROR("--cfg argument requires an argument");
247 if (!option){ //if option is NULL
248 option = bprintf("--cfg=%s",argv[i+1]);
250 char *newoption = bprintf("%s --cfg=%s", option, argv[i+1]);
254 XBT_INFO("Add option \'--cfg=%s\' to command line",argv[i+1]);
255 memmove(argv + i, argv + i + 2, (argc - i - 1) * sizeof(char *));
259 else if (!strcmp(argv[i], "--enable-coverage" )){
261 XBT_INFO("Enable coverage");
262 memmove(argv + i, argv + i + 1, (argc - i - 1) * sizeof(char *));
268 /* Find the description file */
270 XBT_INFO("Test suite from stdin");
271 testsuite_name = "(stdin)";
272 handle_suite(testsuite_name, stdin);
274 XBT_INFO("Test suite from stdin OK");
277 for (i = 1; i < argc; i++) {
278 suitename = xbt_strdup(argv[i]);
279 if (!strncmp("./", suitename, 2))
280 memmove(suitename, suitename + 2, strlen(suitename + 2));
282 if (strlen(suitename) > 5 &&
283 !strcmp(".tesh", suitename + strlen(suitename) - 5))
284 suitename[strlen(suitename) - 5] = '\0';
286 XBT_INFO("Test suite `%s'", suitename);
287 testsuite_name = suitename;
288 IN = fopen(argv[i], "r");
290 perror(bprintf("Impossible to open the suite file `%s'", argv[i]));
291 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
292 rctx_armageddon(rctx, 1);
294 handle_suite(suitename, IN);
297 XBT_INFO("Test suite `%s' OK", suitename);