{"UMASK", "umask"},
};
-using ConfigLineHandler = std::function<void(
- const std::string& line, const std::string& key, const std::string& value)>;
-
bool
parse_bool(const std::string& value,
const optional<std::string> env_var_key,
return true;
}
+// `line` is the full configuration line excluding newline. `key` will be empty
+// for comments and blank lines. `value` does not include newline.
+using ConfigLineHandler = std::function<void(
+ const std::string& line, const std::string& key, const std::string& value)>;
+
+// Call `config_line_handler` for each line in `path`.
bool
parse_config_file(const std::string& path,
const ConfigLineHandler& config_line_handler)
if (!parse_line(line, &key, &value, &error_message)) {
throw Error(error_message);
}
- if (!key.empty()) {
- // key is empty if comment or blank line.
- config_line_handler(line, key, value);
- }
+ config_line_handler(line, key, value);
} catch (const Error& e) {
throw Error("{}:{}: {}", path, line_number, e.what());
}
[&](const std::string& /*line*/,
const std::string& key,
const std::string& value) {
- set_item(key, value, nullopt, false, path);
+ if (!key.empty()) {
+ set_item(key, value, nullopt, false, path);
+ }
});
}
std::string content = Util::read_file("ccache.conf");
CHECK(content == "path = vanilla\nsloppiness = foo\n");
}
+
+ SUBCASE("comments are kept")
+ {
+ Util::write_file("ccache.conf", "# c1\npath = blueberry\n#c2\n");
+ Config::set_value_in_file("ccache.conf", "path", "vanilla");
+ Config::set_value_in_file("ccache.conf", "compiler", "chocolate");
+ std::string content = Util::read_file("ccache.conf");
+ CHECK(content == "# c1\npath = vanilla\n#c2\ncompiler = chocolate\n");
+ }
}
TEST_CASE("Config::get_string_value")