Skip to content

Commit

Permalink
CFE-413: set_variable_values_ini for values with equal sign
Browse files Browse the repository at this point in the history
  • Loading branch information
ncharles committed Dec 2, 2020
1 parent e2b3e56 commit f531e4a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
42 changes: 27 additions & 15 deletions lib/files.cf
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ bundle edit_line set_variable_values_ini(tab, sectionName)
# If the line is commented out with `#`, it gets uncommented first.
# Adds a new line if none exists.
#
# It does support = in value
#
# @param tab An associative array containing `tab[sectionName][LHS]="RHS"`.
# The value is not changed when the `RHS` is "dontchange"
# @param sectionName The section in the file within which values should be
Expand All @@ -436,33 +438,43 @@ bundle edit_line set_variable_values_ini(tab, sectionName)
# Be careful if the index string contains funny chars
"cindex[$(index)]" string => canonify("$(index)");

# Escape the value so that it can be regexed
"ctab[$(index)]" string => escape("$($(tab)[$(sectionName)][$(index)])");

classes:
"edit_$(cindex[$(index)])" not => strcmp("$($(tab)[$(sectionName)][$(index)])","dontchange"),
comment => "Create conditions to make changes";

field_edits:

# If the line is there, but commented out, first uncomment it
"#+\s*$(index)\s*=.*"
select_region => INI_section(escape("$(sectionName)")),
edit_field => col("\s*=\s*","1","$(index)","set"),
if => "edit_$(cindex[$(index)])";

# match a line starting like the key something
"\s*$(index)\s*=.*"
edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"),
select_region => INI_section(escape("$(sectionName)")),
classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"),
if => "edit_$(cindex[$(index)])";
"pass2" expression => "pass1";
"pass1" expression => "any";

insert_lines:
"[$(sectionName)]"
location => start,
comment => "Insert lines";

pass2::
"$(index)=$($(tab)[$(sectionName)][$(index)])"
select_region => INI_section(escape("$(sectionName)")),
if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])";
if => "edit_$(cindex[$(index)])";

replace_patterns:
# If the line is commented out, uncomment and replace with
# the correct value
"^\s*#\s*($(index)\s*=\s*.*)$"
comment => "If we find a commented entry we uncomment it",
select_region => INI_section(escape("$(set_variable_values_ini.sectionName)")),
replace_with => value("$(index)=$($(tab)[$(sectionName)][$(index)])"),
if => "edit_$(cindex[$(index)]).!set_variable_values_ini_not_$(cindex[$(index)])_reached";

# If the line is there with the wrong value, replace with
# the correct value
"^\s*($(index)\s*=\s*(?!$(ctab[$(index)])$).*)$"
comment => "Correct the value $(index)",
replace_with => value("$(index)=$($(tab)[$(sectionName)][$(index)])"),
select_region => INI_section(escape("$(set_variable_values_ini.sectionName)")),
classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"),
if => "edit_$(cindex[$(index)])";

}

Expand Down
7 changes: 6 additions & 1 deletion tests/acceptance/lib/files/set_variable_values_ini.cf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bundle agent init
bundle agent test
{
meta:
"description" -> { "CFE-3221", "CFE-3244" }
"description" -> { "CFE-3221", "CFE-3244", "CFE-413" }
string => "Test expected behavior of set_variable_values_ini";

vars:
Expand All @@ -50,6 +50,9 @@ bundle agent test
"config[CFE-3244][keyone]" string => "1";
"config[CFE-3244][keytwo]" string => "valuetwo";

"config[CFE-413][keyone]" string => "one";
"config[CFE-413][keytwo]" string => "value=one";

files:

"$(G.testfile).actual"
Expand All @@ -61,6 +64,8 @@ bundle agent test
"$(G.testfile).actual" -> { "CFE-3244" }
edit_line => set_variable_values_ini( "test.config", "CFE-3244" );

"$(G.testfile).actual" -> { "CFE-413" }
edit_line => set_variable_values_ini( "test.config", "CFE-413" );
}

#######################################################
Expand Down
2 changes: 2 additions & 0 deletions tests/acceptance/lib/files/set_variable_values_ini.cf.finish
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ variablename4=desired value4
keytwo = valuetwo
[something]
keyone=something1
[CFE-413]
one=value=one
2 changes: 2 additions & 0 deletions tests/acceptance/lib/files/set_variable_values_ini.cf.start
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ variablename3 =desired value3
# keytwo = bar
[something]
keyone=something1
[CFE-413]
one=value=dummy

0 comments on commit f531e4a

Please sign in to comment.