Hide a bunch of depsolver internals in razor.c.
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;
63 char *install_pkgs[3], *remove_pkgs[3];
64 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,
198 enum razor_property_type type,
200 enum razor_version_relation rel,
206 if (razor_transaction_unsatisfied_property(ctx->trans,
210 fprintf(stderr, " didn't get unsatisfiable '%s %s %s'\n",
211 name, razor_version_relations[rel], version);
216 start_property(struct test_context *ctx, enum razor_property_type type, const char **atts)
218 const char *name = NULL, *rel_str = NULL, *version = NULL;
219 enum razor_version_relation rel;
221 get_atts(atts, "name", &name, "relation", &rel_str, "version", &version, NULL);
223 fprintf(stderr, " no name specified for property\n");
227 rel = parse_relation(rel_str);
229 fprintf(stderr, " bad or missing version relation for property %s\n", name);
233 rel = RAZOR_VERSION_EQUAL;
236 check_unsatisfiable_property(ctx, type, name, rel, version);
238 add_property(ctx, type, name, rel, version);
242 start_transaction(struct test_context *ctx, const char **atts)
244 ctx->n_install_pkgs = 0;
245 ctx->n_remove_pkgs = 0;
249 end_transaction(struct test_context *ctx)
253 ctx->trans = razor_transaction_create(ctx->system_set, ctx->repo_set,
255 (const char **)ctx->install_pkgs,
257 (const char **)ctx->remove_pkgs);
258 errors = razor_transaction_describe(ctx->trans);
261 while (ctx->n_install_pkgs--)
262 free(ctx->install_pkgs[ctx->n_install_pkgs]);
263 while (ctx->n_remove_pkgs--)
264 free(ctx->remove_pkgs[ctx->n_remove_pkgs]);
267 struct razor_set *new;
268 new = razor_transaction_run(ctx->trans);
269 razor_set_destroy(ctx->system_set);
270 ctx->system_set = new;
275 start_install_or_update(struct test_context *ctx, const char **atts)
277 const char *name = NULL;
279 get_atts(atts, "name", &name, NULL);
281 fprintf(stderr, " install/update with no name\n");
285 ctx->install_pkgs[ctx->n_install_pkgs++] = strdup(name);
289 start_remove(struct test_context *ctx, const char **atts)
291 const char *name = NULL;
293 get_atts(atts, "name", &name, NULL);
295 fprintf(stderr, " remove with no name\n");
299 ctx->remove_pkgs[ctx->n_remove_pkgs++] = strdup(name);
303 start_result(struct test_context *ctx, const char **atts)
309 diff_callback(const char *name,
310 const char *old_version, const char *new_version,
313 struct test_context *ctx = data;
317 fprintf(stderr, " result set should not contain %s %s\n",
320 fprintf(stderr, " result set should contain %s %s\n",
326 end_result(struct test_context *ctx)
330 if (ctx->result_set) {
331 if (!ctx->system_set)
332 ctx->system_set = razor_set_create();
333 razor_set_diff(ctx->system_set, ctx->result_set,
339 start_unsatisfiable(struct test_context *ctx, const char **atts)
341 if (ctx->result_set) {
342 fprintf(stderr, "Expected to fail, but didn't\n");
350 end_unsatisfiable(struct test_context *ctx)
356 start_test_element(void *data, const char *element, const char **atts)
358 struct test_context *ctx = data;
360 if (strcmp(element, "tests") == 0) {
362 } else if (strcmp(element, "test") == 0) {
363 start_test(ctx, atts);
364 } else if (strcmp(element, "set") == 0) {
365 start_set(ctx, atts);
366 } else if (strcmp(element, "transaction") == 0) {
367 start_transaction(ctx, atts);
368 } else if (strcmp(element, "install") == 0) {
369 start_install_or_update(ctx, atts);
370 } else if (strcmp(element, "install") == 0) {
371 start_install_or_update(ctx, atts);
372 } else if (strcmp(element, "remove") == 0) {
373 start_remove(ctx, atts);
374 } else if (strcmp(element, "result") == 0) {
375 start_result(ctx, atts);
376 } else if (strcmp(element, "unsatisfiable") == 0) {
377 start_unsatisfiable(ctx, atts);
378 } else if (strcmp(element, "package") == 0) {
379 start_package(ctx, atts);
380 } else if (strcmp(element, "requires") == 0) {
381 start_property(ctx, RAZOR_PROPERTY_REQUIRES, atts);
382 } else if (strcmp(element, "provides") == 0) {
383 start_property(ctx, RAZOR_PROPERTY_PROVIDES, atts);
384 } else if (strcmp(element, "conflicts") == 0) {
385 start_property(ctx, RAZOR_PROPERTY_CONFLICTS, atts);
386 } else if (strcmp(element, "obsoletes") == 0) {
387 start_property(ctx, RAZOR_PROPERTY_OBSOLETES, atts);
389 fprintf(stderr, "Unrecognized element '%s'\n", element);
395 end_test_element (void *data, const char *element)
397 struct test_context *ctx = data;
399 if (strcmp(element, "test") == 0) {
401 } else if (strcmp(element, "set") == 0) {
403 } else if (strcmp(element, "package") == 0) {
405 } else if (strcmp(element, "transaction") == 0) {
406 end_transaction(ctx);
407 } else if (strcmp(element, "result") == 0) {
409 } else if (strcmp(element, "unsatisfiable") == 0) {
410 end_unsatisfiable(ctx);
414 int main(int argc, char *argv[])
416 struct test_context ctx;
417 const char *test_file;
419 memset(&ctx, 0, sizeof ctx);
422 fprintf(stderr, "usage: %s [-d] [TESTS-FILE]\n", argv[0]);
426 if (argc >= 2 && !strcmp (argv[1], "-d")) {
434 test_file = "test.xml";
436 parse_xml_file(test_file, start_test_element, end_test_element, &ctx);
439 fprintf(stderr, "\n%d errors\n", ctx.errors);