deal with a single package being the target of both a FORCED_UPDATE and an INSTALL
11 #define XML_BUFFER_SIZE 4096
14 parse_xml_file(const char *filename,
15 XML_StartElementHandler start,
16 XML_EndElementHandler end,
23 parser = XML_ParserCreate(NULL);
24 XML_SetElementHandler(parser, start, end);
25 XML_SetUserData(parser, data);
27 fd = open(filename, O_RDONLY);
29 fprintf(stderr, "failed to open %s: %m\n", filename);
34 buffer = XML_GetBuffer(parser, XML_BUFFER_SIZE);
35 len = read(fd, buffer, XML_BUFFER_SIZE);
38 err = XML_ParseBuffer(parser, len, len == 0);
39 if (err == XML_STATUS_ERROR) {
40 fprintf(stderr, "parse error at line %lu:\n%s\n",
41 XML_GetCurrentLineNumber(parser),
42 XML_ErrorString(XML_GetErrorCode(parser)));
48 fprintf(stderr, "read: %m\n");
56 struct razor_set *system_set, *repo_set, *result_set;
58 struct razor_importer *importer;
59 struct razor_set **importer_set;
61 struct razor_transaction *trans;
62 struct razor_transaction_package *unsat;
64 char *install_pkgs[3], *remove_pkgs[3];
65 int n_install_pkgs, n_remove_pkgs;
73 get_atts(const char **atts, ...)
76 const char *name, **ptr;
80 while (name = va_arg(ap, const char *), name != NULL) {
81 ptr = va_arg(ap, const char **);
83 for (i = 0; atts[i]; i += 2) {
84 if (strcmp(atts[i], name) == 0)
91 static enum razor_version_relation
92 parse_relation (const char *rel_str)
96 if (rel_str[0] == 'L')
97 return rel_str[1] == 'E' ? RAZOR_VERSION_LESS_OR_EQUAL : RAZOR_VERSION_LESS;
98 else if (rel_str[0] == 'G')
99 return rel_str[1] == 'E' ? RAZOR_VERSION_GREATER_OR_EQUAL : RAZOR_VERSION_GREATER;
100 else if (rel_str[0] == 'E' || rel_str[1] == 'Q')
101 return RAZOR_VERSION_EQUAL;
107 start_test(struct test_context *ctx, const char **atts)
109 const char *name = NULL;
111 get_atts(atts, "name", &name, NULL);
113 fprintf(stderr, "Test with no name\n");
116 printf("%s\n", name);
120 end_test(struct test_context *ctx)
122 if (ctx->system_set) {
123 razor_set_destroy(ctx->system_set);
124 ctx->system_set = NULL;
127 razor_set_destroy(ctx->repo_set);
128 ctx->repo_set = NULL;
130 if (ctx->result_set) {
131 razor_set_destroy(ctx->result_set);
132 ctx->result_set = NULL;
135 razor_transaction_destroy(ctx->trans);
141 start_set(struct test_context *ctx, const char **atts)
143 const char *name = NULL;
145 ctx->importer = razor_importer_new();
146 get_atts(atts, "name", &name, NULL);
148 ctx->importer_set = &ctx->result_set;
149 else if (!strcmp(name, "system"))
150 ctx->importer_set = &ctx->system_set;
151 else if (!strcmp(name, "repo"))
152 ctx->importer_set = &ctx->repo_set;
154 fprintf(stderr, " bad set name '%s'\n", name);
160 end_set(struct test_context *ctx)
162 *ctx->importer_set = razor_importer_finish(ctx->importer);
163 ctx->importer = NULL;
167 start_package(struct test_context *ctx, const char **atts)
169 const char *name = NULL, *version = NULL, *arch = NULL;
171 get_atts(atts, "name", &name, "version", &version, "arch", &arch, NULL);
173 fprintf(stderr, " package with no name\n");
177 razor_importer_begin_package(ctx->importer, name, version);
178 razor_importer_add_property(ctx->importer, name,
179 RAZOR_VERSION_EQUAL, version,
180 RAZOR_PROPERTY_PROVIDES);
184 end_package(struct test_context *ctx)
186 razor_importer_finish_package(ctx->importer);
190 add_property(struct test_context *ctx, enum razor_property_type type, const char *name, enum razor_version_relation rel, const char *version)
192 razor_importer_add_property(ctx->importer, name,
197 check_unsatisfiable_property(struct test_context *ctx, enum razor_property_type type, const char *name, enum razor_version_relation rel, const char *version)
202 for (; ctx->unsat < ctx->trans->packages + ctx->trans->package_count; ctx->unsat++) {
203 if (ctx->unsat->state != RAZOR_PACKAGE_UNSATISFIABLE)
205 if (strcmp(name, ctx->unsat->dep_property) != 0 ||
206 rel != ctx->unsat->dep_relation ||
207 strcmp(version, ctx->unsat->dep_version) != 0)
210 /* OK, found it, so skip over it and continue */
215 fprintf(stderr, " didn't get unsatisfiable '%s %s %s'\n",
216 name, razor_version_relations[rel], version);
221 start_property(struct test_context *ctx, enum razor_property_type type, const char **atts)
223 const char *name = NULL, *rel_str = NULL, *version = NULL;
224 enum razor_version_relation rel;
226 get_atts(atts, "name", &name, "relation", &rel_str, "version", &version, NULL);
228 fprintf(stderr, " no name specified for property\n");
232 rel = parse_relation(rel_str);
234 fprintf(stderr, " bad or missing version relation for property %s\n", name);
238 rel = RAZOR_VERSION_EQUAL;
241 check_unsatisfiable_property(ctx, type, name, rel, version);
243 add_property(ctx, type, name, rel, version);
247 start_transaction(struct test_context *ctx, const char **atts)
249 ctx->n_install_pkgs = 0;
250 ctx->n_remove_pkgs = 0;
254 end_transaction(struct test_context *ctx)
256 ctx->trans = razor_transaction_create(ctx->system_set, ctx->repo_set,
258 (const char **)ctx->install_pkgs,
260 (const char **)ctx->remove_pkgs);
262 razor_transaction_describe(ctx->trans);
266 while (ctx->n_install_pkgs--)
267 free(ctx->install_pkgs[ctx->n_install_pkgs]);
268 while (ctx->n_remove_pkgs--)
269 free(ctx->remove_pkgs[ctx->n_remove_pkgs]);
271 if (!ctx->trans->errors) {
272 struct razor_set *new;
273 new = razor_transaction_run(ctx->trans);
274 razor_set_destroy(ctx->system_set);
275 ctx->system_set = new;
280 start_install_or_update(struct test_context *ctx, const char **atts)
282 const char *name = NULL;
284 get_atts(atts, "name", &name, NULL);
286 fprintf(stderr, " install/update with no name\n");
290 ctx->install_pkgs[ctx->n_install_pkgs++] = strdup(name);
294 start_remove(struct test_context *ctx, const char **atts)
296 const char *name = NULL;
298 get_atts(atts, "name", &name, NULL);
300 fprintf(stderr, " remove with no name\n");
304 ctx->remove_pkgs[ctx->n_remove_pkgs++] = strdup(name);
308 start_result(struct test_context *ctx, const char **atts)
314 diff_callback(const char *name,
315 const char *old_version, const char *new_version,
318 struct test_context *ctx = data;
322 fprintf(stderr, " result set should not contain %s %s\n",
325 fprintf(stderr, " result set should contain %s %s\n",
331 end_result(struct test_context *ctx)
335 if (ctx->result_set) {
336 if (!ctx->system_set)
337 ctx->system_set = razor_set_create();
338 razor_set_diff(ctx->system_set, ctx->result_set,
344 start_unsatisfiable(struct test_context *ctx, const char **atts)
346 if (ctx->result_set) {
347 fprintf(stderr, "Expected to fail, but didn't\n");
351 ctx->unsat = ctx->trans->packages;
355 end_unsatisfiable(struct test_context *ctx)
361 start_test_element(void *data, const char *element, const char **atts)
363 struct test_context *ctx = data;
365 if (strcmp(element, "tests") == 0) {
367 } else if (strcmp(element, "test") == 0) {
368 start_test(ctx, atts);
369 } else if (strcmp(element, "set") == 0) {
370 start_set(ctx, atts);
371 } else if (strcmp(element, "transaction") == 0) {
372 start_transaction(ctx, atts);
373 } else if (strcmp(element, "install") == 0) {
374 start_install_or_update(ctx, atts);
375 } else if (strcmp(element, "install") == 0) {
376 start_install_or_update(ctx, atts);
377 } else if (strcmp(element, "remove") == 0) {
378 start_remove(ctx, atts);
379 } else if (strcmp(element, "result") == 0) {
380 start_result(ctx, atts);
381 } else if (strcmp(element, "unsatisfiable") == 0) {
382 start_unsatisfiable(ctx, atts);
383 } else if (strcmp(element, "package") == 0) {
384 start_package(ctx, atts);
385 } else if (strcmp(element, "requires") == 0) {
386 start_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
387 } else if (strcmp(element, "provides") == 0) {
388 start_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
389 } else if (strcmp(element, "conflicts") == 0) {
390 start_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
391 } else if (strcmp(element, "obsoletes") == 0) {
392 start_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
394 fprintf(stderr, "Unrecognized element '%s'\n", element);
400 end_test_element (void *data, const char *element)
402 struct test_context *ctx = data;
404 if (strcmp(element, "test") == 0) {
406 } else if (strcmp(element, "set") == 0) {
408 } else if (strcmp(element, "package") == 0) {
410 } else if (strcmp(element, "transaction") == 0) {
411 end_transaction(ctx);
412 } else if (strcmp(element, "result") == 0) {
414 } else if (strcmp(element, "unsatisfiable") == 0) {
415 end_unsatisfiable(ctx);
419 int main(int argc, char *argv[])
421 struct test_context ctx;
422 const char *test_file;
424 memset(&ctx, 0, sizeof ctx);
427 fprintf(stderr, "usage: %s [-d] [TESTS-FILE]\n", argv[0]);
431 if (argc >= 2 && !strcmp (argv[1], "-d")) {
439 test_file = "test.xml";
441 parse_xml_file(test_file, start_test_element, end_test_element, &ctx);
444 fprintf(stderr, "\n%d errors\n", ctx.errors);