convert razor_package_get_details() and razor_package_iterator_next() to varargs
The functions for getting package details about a package were limited to a few
things, when in the future we will want to support much more about a package.
The iterator was also limited to name,version,arch when most of the time we
didn't need all this data.
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #define XML_BUFFER_SIZE 4096
33 parse_xml_file(const char *filename,
34 XML_StartElementHandler start,
35 XML_EndElementHandler end,
42 parser = XML_ParserCreate(NULL);
43 XML_SetElementHandler(parser, start, end);
44 XML_SetUserData(parser, data);
46 fd = open(filename, O_RDONLY);
48 fprintf(stderr, "failed to open %s: %m\n", filename);
53 buffer = XML_GetBuffer(parser, XML_BUFFER_SIZE);
54 len = read(fd, buffer, XML_BUFFER_SIZE);
57 err = XML_ParseBuffer(parser, len, len == 0);
58 if (err == XML_STATUS_ERROR) {
59 fprintf(stderr, "parse error at line %lu:\n%s\n",
60 XML_GetCurrentLineNumber(parser),
61 XML_ErrorString(XML_GetErrorCode(parser)));
67 fprintf(stderr, "read: %m\n");
75 struct razor_set *system_set, *repo_set, *result_set;
77 struct razor_importer *importer;
78 struct razor_set **importer_set;
80 struct razor_transaction *trans;
82 char *install_pkgs[3], *remove_pkgs[3];
83 int n_install_pkgs, n_remove_pkgs;
92 get_atts(const char **atts, ...)
95 const char *name, **ptr;
99 while (name = va_arg(ap, const char *), name != NULL) {
100 ptr = va_arg(ap, const char **);
102 for (i = 0; atts[i]; i += 2) {
103 if (strcmp(atts[i], name) == 0)
110 static enum razor_property_flags
111 parse_relation (const char *rel_str)
115 if (rel_str[0] == 'L')
116 return rel_str[1] == 'E' ? RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL : RAZOR_PROPERTY_LESS;
117 else if (rel_str[0] == 'G')
118 return rel_str[1] == 'E' ? RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL : RAZOR_PROPERTY_GREATER;
119 else if (rel_str[0] == 'E' || rel_str[1] == 'Q')
120 return RAZOR_PROPERTY_EQUAL;
126 start_test(struct test_context *ctx, const char **atts)
128 const char *name = NULL;
130 get_atts(atts, "name", &name, NULL);
132 fprintf(stderr, "Test with no name\n");
135 printf("%s\n", name);
139 end_test(struct test_context *ctx)
141 if (ctx->system_set) {
142 razor_set_destroy(ctx->system_set);
143 ctx->system_set = NULL;
146 razor_set_destroy(ctx->repo_set);
147 ctx->repo_set = NULL;
149 if (ctx->result_set) {
150 razor_set_destroy(ctx->result_set);
151 ctx->result_set = NULL;
154 razor_transaction_destroy(ctx->trans);
160 start_set(struct test_context *ctx, const char **atts)
162 const char *name = NULL;
164 ctx->importer = razor_importer_create();
165 get_atts(atts, "name", &name, NULL);
167 ctx->importer_set = &ctx->result_set;
168 else if (!strcmp(name, "system"))
169 ctx->importer_set = &ctx->system_set;
170 else if (!strcmp(name, "repo"))
171 ctx->importer_set = &ctx->repo_set;
173 fprintf(stderr, " bad set name '%s'\n", name);
179 end_set(struct test_context *ctx)
181 *ctx->importer_set = razor_importer_finish(ctx->importer);
182 ctx->importer = NULL;
186 start_package(struct test_context *ctx, const char **atts)
188 const char *name = NULL, *version = NULL, *arch = NULL;
190 get_atts(atts, "name", &name,
196 fprintf(stderr, " package with no name\n");
200 razor_importer_begin_package(ctx->importer, name, version, arch);
201 razor_importer_add_property(ctx->importer, name,
202 RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_PROVIDES,
207 end_package(struct test_context *ctx)
209 razor_importer_finish_package(ctx->importer);
213 add_property(struct test_context *ctx, enum razor_property_flags type, const char *name, enum razor_property_flags rel, const char *version)
215 razor_importer_add_property(ctx->importer, name,
216 rel | type, version);
220 razor_property_flags_relation_to_string(enum razor_property_flags rel)
222 if (rel == RAZOR_PROPERTY_LESS)
224 if (rel == (RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_LESS))
226 if (rel == RAZOR_PROPERTY_EQUAL)
228 if (rel == (RAZOR_PROPERTY_EQUAL | RAZOR_PROPERTY_GREATER))
230 if (rel == RAZOR_PROPERTY_GREATER)
237 check_unsatisfiable_property(struct test_context *ctx,
238 enum razor_property_flags type,
240 enum razor_property_flags rel,
246 if (razor_transaction_unsatisfied_property(ctx->trans,
247 name, rel | type, version))
250 fprintf(stderr, " didn't get unsatisfiable '%s %s %s'\n",
251 name, razor_property_flags_relation_to_string(rel), version);
256 start_property(struct test_context *ctx, enum razor_property_flags type, const char **atts)
258 const char *name = NULL, *rel_str = NULL, *version = NULL;
259 enum razor_property_flags rel;
261 get_atts(atts, "name", &name, "relation", &rel_str, "version", &version, NULL);
263 fprintf(stderr, " no name specified for property\n");
267 rel = parse_relation(rel_str);
269 fprintf(stderr, " bad or missing version relation for property %s\n", name);
273 rel = RAZOR_PROPERTY_EQUAL;
276 check_unsatisfiable_property(ctx, type, name, rel, version);
278 add_property(ctx, type, name, rel, version);
282 start_transaction(struct test_context *ctx, const char **atts)
284 ctx->n_install_pkgs = 0;
285 ctx->n_remove_pkgs = 0;
289 end_transaction(struct test_context *ctx)
291 struct razor_package *pkg;
294 ctx->trans = razor_transaction_create(ctx->system_set, ctx->repo_set);
295 for (i = 0; i < ctx->n_install_pkgs; i++) {
296 pkg = razor_set_get_package(ctx->repo_set,
297 ctx->install_pkgs[i]);
298 razor_transaction_install_package(ctx->trans, pkg);
300 for (i = 0; i < ctx->n_remove_pkgs; i++) {
301 pkg = razor_set_get_package(ctx->system_set,
302 ctx->remove_pkgs[i]);
304 pkg = razor_set_get_package(ctx->repo_set,
305 ctx->remove_pkgs[i]);
307 razor_transaction_remove_package(ctx->trans, pkg);
310 razor_transaction_resolve(ctx->trans);
311 errors = razor_transaction_describe(ctx->trans);
314 while (ctx->n_install_pkgs--)
315 free(ctx->install_pkgs[ctx->n_install_pkgs]);
316 while (ctx->n_remove_pkgs--)
317 free(ctx->remove_pkgs[ctx->n_remove_pkgs]);
320 struct razor_set *new;
321 new = razor_transaction_finish(ctx->trans);
323 ctx->system_set = new;
328 start_install_or_update(struct test_context *ctx, const char **atts)
330 const char *name = NULL;
332 get_atts(atts, "name", &name, NULL);
334 fprintf(stderr, " install/update with no name\n");
338 ctx->install_pkgs[ctx->n_install_pkgs++] = strdup(name);
342 start_remove(struct test_context *ctx, const char **atts)
344 const char *name = NULL;
346 get_atts(atts, "name", &name, NULL);
348 fprintf(stderr, " remove with no name\n");
352 ctx->remove_pkgs[ctx->n_remove_pkgs++] = strdup(name);
356 start_result(struct test_context *ctx, const char **atts)
362 diff_callback(enum razor_diff_action action,
363 struct razor_package *package,
369 struct test_context *ctx = data;
372 if (action == RAZOR_DIFF_ACTION_REMOVE) {
373 fprintf(stderr, " result set should not contain %s %s\n",
376 fprintf(stderr, " result set should contain %s %s\n",
382 end_result(struct test_context *ctx)
386 if (ctx->result_set) {
387 if (!ctx->system_set)
388 ctx->system_set = razor_set_create();
389 razor_set_diff(ctx->system_set, ctx->result_set,
395 start_unsatisfiable(struct test_context *ctx, const char **atts)
397 if (ctx->result_set) {
398 fprintf(stderr, "Expected to fail, but didn't\n");
406 end_unsatisfiable(struct test_context *ctx)
412 start_test_element(void *data, const char *element, const char **atts)
414 struct test_context *ctx = data;
416 if (strcmp(element, "tests") == 0) {
418 } else if (strcmp(element, "test") == 0) {
419 start_test(ctx, atts);
420 } else if (strcmp(element, "set") == 0) {
421 start_set(ctx, atts);
422 } else if (strcmp(element, "transaction") == 0) {
423 start_transaction(ctx, atts);
424 } else if (strcmp(element, "install") == 0) {
425 start_install_or_update(ctx, atts);
426 } else if (strcmp(element, "install") == 0) {
427 start_install_or_update(ctx, atts);
428 } else if (strcmp(element, "remove") == 0) {
429 start_remove(ctx, atts);
430 } else if (strcmp(element, "result") == 0) {
431 start_result(ctx, atts);
432 } else if (strcmp(element, "unsatisfiable") == 0) {
433 start_unsatisfiable(ctx, atts);
434 } else if (strcmp(element, "package") == 0) {
435 start_package(ctx, atts);
436 } else if (strcmp(element, "requires") == 0) {
437 start_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
438 } else if (strcmp(element, "provides") == 0) {
439 start_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
440 } else if (strcmp(element, "conflicts") == 0) {
441 start_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
442 } else if (strcmp(element, "obsoletes") == 0) {
443 start_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
445 fprintf(stderr, "Unrecognized element '%s'\n", element);
451 end_test_element (void *data, const char *element)
453 struct test_context *ctx = data;
455 if (strcmp(element, "test") == 0) {
457 } else if (strcmp(element, "set") == 0) {
459 } else if (strcmp(element, "package") == 0) {
461 } else if (strcmp(element, "transaction") == 0) {
462 end_transaction(ctx);
463 } else if (strcmp(element, "result") == 0) {
465 } else if (strcmp(element, "unsatisfiable") == 0) {
466 end_unsatisfiable(ctx);
470 int main(int argc, char *argv[])
472 struct test_context ctx;
473 const char *test_file;
475 memset(&ctx, 0, sizeof ctx);
478 fprintf(stderr, "usage: %s [-d] [TESTS-FILE]\n", argv[0]);
482 if (argc >= 2 && !strcmp (argv[1], "-d")) {
490 test_file = "test.xml";
492 parse_xml_file(test_file, start_test_element, end_test_element, &ctx);
495 fprintf(stderr, "\n%d errors\n", ctx.errors);