add a test for semantic errors in config files
[git.git] / test-config.c
blob9dd1b22630948b63d971a8ef5ff363db2d80fd9a
1 #include "cache.h"
2 #include "string-list.h"
4 /*
5 * This program exposes the C API of the configuration mechanism
6 * as a set of simple commands in order to facilitate testing.
8 * Reads stdin and prints result of command to stdout:
10 * get_value -> prints the value with highest priority for the entered key
12 * get_value_multi -> prints all values for the entered key in increasing order
13 * of priority
15 * get_int -> print integer value for the entered key or die
17 * get_bool -> print bool value for the entered key or die
19 * configset_get_value -> returns value with the highest priority for the entered key
20 * from a config_set constructed from files entered as arguments.
22 * configset_get_value_multi -> returns value_list for the entered key sorted in
23 * ascending order of priority from a config_set
24 * constructed from files entered as arguments.
26 * Examples:
28 * To print the value with highest priority for key "foo.bAr Baz.rock":
29 * test-config get_value "foo.bAr Baz.rock"
34 int main(int argc, char **argv)
36 int i, val;
37 const char *v;
38 const struct string_list *strptr;
39 struct config_set cs;
40 git_configset_init(&cs);
42 if (argc < 2) {
43 fprintf(stderr, "Please, provide a command name on the command-line\n");
44 goto exit1;
45 } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
46 if (!git_config_get_value(argv[2], &v)) {
47 if (!v)
48 printf("(NULL)\n");
49 else
50 printf("%s\n", v);
51 goto exit0;
52 } else {
53 printf("Value not found for \"%s\"\n", argv[2]);
54 goto exit1;
56 } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
57 strptr = git_config_get_value_multi(argv[2]);
58 if (strptr) {
59 for (i = 0; i < strptr->nr; i++) {
60 v = strptr->items[i].string;
61 if (!v)
62 printf("(NULL)\n");
63 else
64 printf("%s\n", v);
66 goto exit0;
67 } else {
68 printf("Value not found for \"%s\"\n", argv[2]);
69 goto exit1;
71 } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
72 if (!git_config_get_int(argv[2], &val)) {
73 printf("%d\n", val);
74 goto exit0;
75 } else {
76 printf("Value not found for \"%s\"\n", argv[2]);
77 goto exit1;
79 } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
80 if (!git_config_get_bool(argv[2], &val)) {
81 printf("%d\n", val);
82 goto exit0;
83 } else {
84 printf("Value not found for \"%s\"\n", argv[2]);
85 goto exit1;
87 } else if (!strcmp(argv[1], "configset_get_value")) {
88 for (i = 3; i < argc; i++) {
89 int err;
90 if ((err = git_configset_add_file(&cs, argv[i]))) {
91 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
92 goto exit2;
95 if (!git_configset_get_value(&cs, argv[2], &v)) {
96 if (!v)
97 printf("(NULL)\n");
98 else
99 printf("%s\n", v);
100 goto exit0;
101 } else {
102 printf("Value not found for \"%s\"\n", argv[2]);
103 goto exit1;
105 } else if (!strcmp(argv[1], "configset_get_value_multi")) {
106 for (i = 3; i < argc; i++) {
107 int err;
108 if ((err = git_configset_add_file(&cs, argv[i]))) {
109 fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
110 goto exit2;
113 strptr = git_configset_get_value_multi(&cs, argv[2]);
114 if (strptr) {
115 for (i = 0; i < strptr->nr; i++) {
116 v = strptr->items[i].string;
117 if (!v)
118 printf("(NULL)\n");
119 else
120 printf("%s\n", v);
122 goto exit0;
123 } else {
124 printf("Value not found for \"%s\"\n", argv[2]);
125 goto exit1;
129 die("%s: Please check the syntax and the function name", argv[0]);
131 exit0:
132 git_configset_clear(&cs);
133 return 0;
135 exit1:
136 git_configset_clear(&cs);
137 return 1;
139 exit2:
140 git_configset_clear(&cs);
141 return 2;