use sed instead of grep to get rid of the <?xml...> line
[platform/upstream/libsolv.git] / tools / testsolv.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include "pool.h"
6 #include "repo.h"
7 #include "solver.h"
8 #include "selection.h"
9 #include "solverdebug.h"
10 #include "testcase.h"
11
12 static struct resultflags2str {
13   Id flag;
14   const char *str;
15 } resultflags2str[] = {
16   { TESTCASE_RESULT_TRANSACTION,        "transaction" },
17   { TESTCASE_RESULT_PROBLEMS,           "problems" },
18   { TESTCASE_RESULT_ORPHANED,           "orphaned" },
19   { TESTCASE_RESULT_RECOMMENDED,        "recommended" },
20   { TESTCASE_RESULT_UNNEEDED,           "unneeded" },
21   { 0, 0 }
22 };
23
24 static void
25 usage(ex)
26 {
27   fprintf(ex ? stderr : stdout, "Usage: testsolv <testcase>\n");
28   exit(ex);
29 }
30
31 int
32 main(int argc, char **argv)
33 {
34   Pool *pool;
35   Queue job;
36   Queue solq;
37   Solver *solv;
38   char *result = 0;
39   int resultflags = 0;
40   int debuglevel = 0;
41   int writeresult = 0;
42   int multijob = 0;
43   int c;
44   int ex = 0;
45   const char *list = 0;
46   FILE *fp;
47   const char *p;
48
49   queue_init(&solq);
50   while ((c = getopt(argc, argv, "vrhl:s:")) >= 0)
51     {
52       switch (c)
53       {
54         case 'v':
55           debuglevel++;
56           break;
57         case 'r':
58           writeresult++;
59           break;
60         case 'h':
61           usage(0);
62           break;
63         case 'l':
64           list = optarg;
65           break;
66         case 's':
67           if ((p = strchr(optarg, ':')))
68             queue_push2(&solq, atoi(optarg), atoi(p + 1));
69           else
70             queue_push2(&solq, 1, atoi(optarg));
71           break;
72         default:
73           usage(1);
74           break;
75       }
76     }
77   if (optind == argc)
78     usage(1);
79   for (; optind < argc; optind++)
80     {
81       pool = pool_create();
82       pool_setdebuglevel(pool, debuglevel);
83
84       fp = fopen(argv[optind], "r");
85       if (!fp)
86         {
87           perror(argv[optind]);
88           exit(0);
89         }
90       while (!feof(fp))
91         {
92           queue_init(&job);
93           result = 0;
94           resultflags = 0;
95           solv = testcase_read(pool, fp, argv[optind], &job, &result, &resultflags);
96           if (!solv)
97             {
98               pool_free(pool);
99               exit(1);
100             }
101
102           if (!multijob && !feof(fp))
103             multijob = 1;
104
105           if (multijob)
106             printf("test %d:\n", multijob++);
107           if (list)
108             {
109               queue_empty(&job);
110               selection_make(pool, &job, list, SELECTION_NAME|SELECTION_PROVIDES|SELECTION_FILELIST|SELECTION_CANON|SELECTION_DOTARCH|SELECTION_REL|SELECTION_GLOB|SELECTION_FLAT);
111               if (!job.elements)
112                 printf("No match\n");
113               else
114                 {
115                   Queue q;
116                   int i;
117                   queue_init(&q);
118                   selection_solvables(pool, &job, &q);
119                   for (i = 0; i < q.count; i++)
120                     printf("  - %s\n", testcase_solvid2str(pool, q.elements[i]));
121                   queue_free(&q);
122                 }
123             }
124           else if (result || writeresult)
125             {
126               char *myresult, *resultdiff;
127               solver_solve(solv, &job);
128               if (!resultflags)
129                 resultflags = TESTCASE_RESULT_TRANSACTION | TESTCASE_RESULT_PROBLEMS;
130               myresult = testcase_solverresult(solv, resultflags);
131               if (writeresult)
132                 {
133                   if (*myresult)
134                     {
135                       if (writeresult > 1)
136                         {
137                           const char *p;
138                           int i;
139                           
140                           printf("result ");
141                           p = "%s";
142                           for (i = 0; resultflags2str[i].str; i++)
143                             if ((resultflags & resultflags2str[i].flag) != 0)
144                               {
145                                 printf(p, resultflags2str[i].str);
146                                 p = ",%s";
147                               }
148                           printf(" <inline>\n");
149                           p = myresult;
150                           while (*p)
151                             {
152                               const char *p2 = strchr(p, '\n');
153                               p2 = p2 ? p2 + 1 : p + strlen(p);
154                               printf("#>%.*s", (int)(p2 - p), p);
155                               p = p2;
156                             }
157                         }
158                       else
159                         printf("%s", myresult);
160                     }
161                 }
162               else
163                 {
164                   resultdiff = testcase_resultdiff(result, myresult);
165                   if (resultdiff)
166                     {
167                       printf("Results differ:\n%s", resultdiff);
168                       ex = 1;
169                       solv_free(resultdiff);
170                     }
171                 }
172               solv_free(result);
173               solv_free(myresult);
174             }
175           else
176             {
177               int pcnt = solver_solve(solv, &job);
178               if (pcnt && solq.count)
179                 {
180                   int i, taken = 0;
181                   for (i = 0; i < solq.count; i += 2)
182                     {
183                       if (solq.elements[i] > 0 && solq.elements[i] <= pcnt)
184                         if (solq.elements[i + 1] > 0 && solq.elements[i + 1] <=  solver_solution_count(solv, solq.elements[i]))
185                           {
186                             printf("problem %d: taking solution %d\n", solq.elements[i], solq.elements[i + 1]);
187                             solver_take_solution(solv, solq.elements[i], solq.elements[i + 1], &job);
188                             taken = 1;
189                           }
190                     }
191                   if (taken)
192                     pcnt = solver_solve(solv, &job);
193                 }
194               if (pcnt)
195                 {
196                   int problem, solution, scnt;
197                   printf("Found %d problems:\n", pcnt);
198                   for (problem = 1; problem <= pcnt; problem++)
199                     {
200                       printf("Problem %d:\n", problem);
201 #if 1
202                       solver_printprobleminfo(solv, problem);
203 #else
204                       {
205                         Queue pq;
206                         int j;
207                         queue_init(&pq);
208                         solver_findallproblemrules(solv, problem, &pq);
209                         for (j = 0; j < pq.count; j++)
210                           solver_printproblemruleinfo(solv, pq.elements[j]);
211                         queue_free(&pq);
212                       }
213 #endif
214                       printf("\n");
215                       scnt = solver_solution_count(solv, problem);
216                       for (solution = 1; solution <= scnt; solution++)
217                         {
218                           printf("Solution %d:\n", solution);
219                           solver_printsolution(solv, problem, solution);
220                           printf("\n");
221                         }
222                     }
223                 }
224               else
225                 {
226                   Transaction *trans = solver_create_transaction(solv);
227                   printf("Transaction summary:\n\n");
228                   transaction_print(trans);
229                   transaction_free(trans);
230                 }
231             }
232           queue_free(&job);
233           solver_free(solv);
234         }
235       pool_free(pool);
236       fclose(fp);
237     }
238   queue_free(&solq);
239   exit(ex);
240 }