Imported Upstream version 3.4
[platform/upstream/ccache.git] / unittest / main.c
1 // Copyright (C) 2010-2018 Joel Rosdahl
2 //
3 // This program is free software; you can redistribute it and/or modify it
4 // under the terms of the GNU General Public License as published by the Free
5 // Software Foundation; either version 3 of the License, or (at your option)
6 // any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 // more details.
12 //
13 // You should have received a copy of the GNU General Public License along with
14 // this program; if not, write to the Free Software Foundation, Inc., 51
15 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 #include "framework.h"
18 #ifdef HAVE_GETOPT_LONG
19 #include <getopt.h>
20 #else
21 #include "../src/getopt_long.h"
22 #endif
23
24 // *INDENT-OFF* disable uncrustify
25 #define SUITE(name) unsigned suite_ ## name(unsigned);
26 #include "suites.h"
27 #undef SUITE
28 // *INDENT-ON* enable uncrustify
29
30 const char USAGE_TEXT[] =
31   "Usage:\n"
32   "    test [options]\n"
33   "\n"
34   "Options:\n"
35   "    -h, --help      print this help text\n"
36   "    -v, --verbose   enable verbose logging of tests\n";
37
38 int
39 main(int argc, char **argv)
40 {
41         suite_fn suites[] = {
42 #define SUITE(name) &suite_ ## name,
43 #include "suites.h"
44 #undef SUITE
45                 NULL
46         };
47         static const struct option options[] = {
48                 {"help", no_argument, NULL, 'h'},
49                 {"verbose", no_argument, NULL, 'v'},
50                 {NULL, 0, NULL, 0}
51         };
52         int verbose = 0;
53         int c;
54         char *testdir, *dir_before;
55         int result;
56
57 #ifdef _WIN32
58         putenv("CCACHE_DETECT_SHEBANG=1");
59 #endif
60
61         while ((c = getopt_long(argc, argv, "hv", options, NULL)) != -1) {
62                 switch (c) {
63                 case 'h':
64                         fprintf(stdout, USAGE_TEXT);
65                         return 0;
66
67                 case 'v':
68                         verbose = 1;
69                         break;
70
71                 default:
72                         fprintf(stderr, USAGE_TEXT);
73                         return 1;
74                 }
75         }
76
77         if (getenv("RUN_FROM_BUILD_FARM")) {
78                 verbose = 1;
79         }
80
81         testdir = format("testdir.%d", (int)getpid());
82         cct_create_fresh_dir(testdir);
83         dir_before = gnu_getcwd();
84         cct_chdir(testdir);
85         result = cct_run(suites, verbose);
86         if (result == 0) {
87                 cct_chdir(dir_before);
88                 cct_wipe(testdir);
89         }
90         free(testdir);
91         free(dir_before);
92         return result;
93 }