diff --git a/src/commons/config.c b/src/commons/config.c index a65daffd..366076ac 100644 --- a/src/commons/config.c +++ b/src/commons/config.c @@ -53,6 +53,8 @@ t_config *config_create(char *path) { void add_cofiguration(char *line) { if (!string_is_empty(line) && !string_starts_with(line, "#")) { char** keyAndValue = string_n_split(line, 2, "="); + string_trim(&keyAndValue[0]); + string_trim(&keyAndValue[1]); dictionary_put(config->properties, keyAndValue[0], keyAndValue[1]); free(keyAndValue[0]); free(keyAndValue); diff --git a/tests/unit-tests/resources/config-double-newline.cfg b/tests/unit-tests/resources/config-double-newline.cfg deleted file mode 100644 index c1438a0a..00000000 --- a/tests/unit-tests/resources/config-double-newline.cfg +++ /dev/null @@ -1,17 +0,0 @@ -# This is the IP -IP=127.0.0.1 - -PORT=8080 - -PROCESS_NAME=TEST - -# This is the load -LOAD=0.5 - - -# This is an array -NUMBERS=[1, 2, 3, 4, 5] - -EMPTY_ARRAY=[] - - diff --git a/tests/unit-tests/resources/config.cfg b/tests/unit-tests/resources/config.cfg index 6558cb33..61dc04ef 100644 --- a/tests/unit-tests/resources/config.cfg +++ b/tests/unit-tests/resources/config.cfg @@ -4,6 +4,9 @@ IP=127.0.0.1 # This has an equals sign WITH_EQUALS=this=value +# This has trailing whitespace characters +TRAILING_WHITESPACES = 42 + PORT=8080 PROCESS_NAME=TEST @@ -18,3 +21,5 @@ NUMBERS=[1, 2, 3, 4, 5] NO_SPACES=[One,String,Next,to,another] EMPTY_ARRAY=[] + + diff --git a/tests/unit-tests/test_config.c b/tests/unit-tests/test_config.c index a0847b0e..95808894 100644 --- a/tests/unit-tests/test_config.c +++ b/tests/unit-tests/test_config.c @@ -64,7 +64,7 @@ context (test_config) { } end it("should return the keys count") { - should_int(config_keys_amount(config)) be equal to(8); + should_int(config_keys_amount(config)) be equal to(9); } end describe("Get") { @@ -115,6 +115,11 @@ context (test_config) { string_array_destroy(strings); } end + it ("should get a value ignoring trailing spaces") { + should_string(config_get_string_value(config, "TRAILING_WHITESPACES")) be equal to("42"); + should_int(config_get_int_value(config, "TRAILING_WHITESPACES")) be equal to(42); + } end + } end } end @@ -124,7 +129,7 @@ context (test_config) { describe("Double newline") { it ("should not fail when the config file ends with two newlines") { - t_config *config = config_create("resources/config-double-newline.cfg"); + t_config *config = config_create("resources/config.cfg"); config_destroy(config); } end } end