FOSSology  4.4.0
Open Source License Compliance by Open Source Software
libfocunit.c
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "libfocunit.h"
8 
28 int focunit_main(int argc, char **argv, char *test_name, CU_SuiteInfo *suites)
29 {
30  int iopt;
31  char *SuiteName = 0;
32  char *TestName = 0;
33  CU_pTestRegistry pRegistry;
34  CU_pSuite pSuite;
35  CU_pTest pTest;
36  CU_ErrorCode ErrCode;
37  CU_pFailureRecord FailureList;
38  CU_RunSummary *pRunSummary;
39  int FailRec;
40 
42  if (!test_name)
43  {
44  fprintf(stderr, "FATAL: empty test name.\n");
45  exit(1);
46  }
47 
48  if (CU_initialize_registry())
49  {
50  fprintf(stderr, "FATAL: Initialization of Test Registry failed.\n");
51  exit(1);
52  }
53 
54  assert(CU_get_registry());
55  assert(!CU_is_test_running());
56 
57  if (CU_register_suites(suites) != CUE_SUCCESS)
58  {
59  fprintf(stderr, "FATAL: Register suites failed - %s\n", CU_get_error_msg());
60  exit(1);
61  }
62  pRegistry = CU_get_registry();
63 
64  /* option -s suitename to run
65  * -t testname to run
66  */
67  while ((iopt = getopt(argc, argv, "s:t:")) != -1)
68  {
69  switch (iopt)
70  {
71  case 's':
72  SuiteName = optarg;
73  break;
74  case 't':
75  TestName = optarg;
76  break;
77  default:
78  fprintf(stderr, "Invalid argument for %s\n", argv[0]);
79  exit(-1);
80  }
81  }
82 
83  /* If TestName is specified, SuiteName is required */
84  if (TestName && !SuiteName)
85  {
86  fprintf(stderr, "A Suite name (-s) is required if you specify a test name.\n");
87  exit(-1);
88  }
89 
90  if (SuiteName)
91  {
92  pSuite = CU_get_suite_by_name(SuiteName, pRegistry);
93  if (!pSuite)
94  {
95  fprintf(stderr, "Suite %s not found.\n", SuiteName);
96  exit(-1);
97  }
98 
99  if (TestName)
100  {
101  pTest = CU_get_test_by_name(TestName, pSuite);
102  if (!pTest)
103  {
104  fprintf(stderr, "Test %s not found in suite %s.\n", TestName, SuiteName);
105  exit(-1);
106  }
107  ErrCode = CU_run_test(pSuite, pTest);
108  }
109  else
110  {
111  ErrCode = CU_run_suite(pSuite);
112  }
113 
114  if (ErrCode)
115  {
116  fprintf(stderr, "Error: %s\n", CU_get_error_msg());
117  exit(-2);
118  }
119  }
120  else // generate xml test report via Automated mode only when run all suits in pRegistrAy, or not
121 
122  {
123  CU_set_output_filename(test_name);
124  CU_list_tests_to_file();
125  CU_automated_run_tests();
126  }
127 
128  pRunSummary = CU_get_run_summary();
129  printf("%s summary:\n", test_name);
130  printf(" Number of suites run: %d\n", pRunSummary->nSuitesRun);
131  printf(" Number of suites failed: %d\n", pRunSummary->nSuitesFailed);
132  printf(" Number of tests run: %d\n", pRunSummary->nTestsRun);
133  printf(" Number of tests failed: %d\n", pRunSummary->nTestsFailed);
134  printf(" Number of asserts: %d\n", pRunSummary->nAsserts);
135  printf(" Number of asserts failed: %d\n", pRunSummary->nAssertsFailed);
136  printf(" Number of failures: %d\n", pRunSummary->nFailureRecords);
137 
138  /* Print any failures */
139  int result = 0;
140  if (pRunSummary->nFailureRecords)
141  {
142  printf("\nFailures:\n");
143  FailRec = 1;
144  for (FailureList = CU_get_failure_list(); FailureList; FailureList = FailureList->pNext)
145  {
146  printf("%d. File: %s Line: %u",
147  FailRec,
148  FailureList->strFileName,
149  FailureList->uiLineNumber);
150 
151  if (FailureList->pTest) {
152  printf(" Test: %s", (FailureList->pTest)->pName);
153  }
154  if (FailureList->pSuite) {
155  printf(" Suite: %s", (FailureList->pSuite)->pName);
156  }
157 
158  printf("\n %s\n",
159  FailureList->strCondition);
160 
161  FailRec++;
162  }
163  printf("\n");
164  result = 1;
165  }
166 
167  CU_cleanup_registry();
168 
169  return result;
170 }
CU_SuiteInfo suites[]
all test suites for delagent
Definition: testRun.h:36