Skip to content

Commit b7f4158

Browse files
committed
CFE-413: set_variable_values_ini for values with equal sign
1 parent e2b3e56 commit b7f4158

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

lib/files.cf

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ bundle edit_line set_variable_values_ini(tab, sectionName)
423423
# If the line is commented out with `#`, it gets uncommented first.
424424
# Adds a new line if none exists.
425425
#
426+
# It does support = in value
427+
#
426428
# @param tab An associative array containing `tab[sectionName][LHS]="RHS"`.
427429
# The value is not changed when the `RHS` is "dontchange"
428430
# @param sectionName The section in the file within which values should be
@@ -436,33 +438,43 @@ bundle edit_line set_variable_values_ini(tab, sectionName)
436438
# Be careful if the index string contains funny chars
437439
"cindex[$(index)]" string => canonify("$(index)");
438440

441+
# Escape the value so that it can be regexed
442+
"ctab[$(index)]" string => escape("$($(tab)[$(sectionName)][$(index)])");
443+
439444
classes:
440445
"edit_$(cindex[$(index)])" not => strcmp("$($(tab)[$(sectionName)][$(index)])","dontchange"),
441446
comment => "Create conditions to make changes";
442447

443-
field_edits:
444-
445-
# If the line is there, but commented out, first uncomment it
446-
"#+\s*$(index)\s*=.*"
447-
select_region => INI_section(escape("$(sectionName)")),
448-
edit_field => col("\s*=\s*","1","$(index)","set"),
449-
if => "edit_$(cindex[$(index)])";
450-
451-
# match a line starting like the key something
452-
"\s*$(index)\s*=.*"
453-
edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"),
454-
select_region => INI_section(escape("$(sectionName)")),
455-
classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"),
456-
if => "edit_$(cindex[$(index)])";
448+
"pass2" expression => "pass1";
449+
"pass1" expression => "any";
457450

458451
insert_lines:
459452
"[$(sectionName)]"
460453
location => start,
461454
comment => "Insert lines";
462455

456+
pass2::
463457
"$(index)=$($(tab)[$(sectionName)][$(index)])"
464458
select_region => INI_section(escape("$(sectionName)")),
465-
if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])";
459+
if => "edit_$(cindex[$(index)])";
460+
461+
replace_patterns:
462+
# If the line is commented out, uncomment and replace with
463+
# the correct value
464+
"^\s*#\s*($(index)\s*=\s*.*)$"
465+
comment => "If we find a commented entry we uncomment it",
466+
select_region => INI_section(escape("$(set_variable_values_ini.sectionName)")),
467+
replace_with => value("$(index)=$($(tab)[$(sectionName)][$(index)])"),
468+
if => "edit_$(cindex[$(index)]).!set_variable_values_ini_not_$(cindex[$(index)])_reached";
469+
470+
# If the line is there with the wrong value, replace with
471+
# the correct value
472+
"^\s*($(index)\s*=\s*(?!$(ctab[$(index)])$).*)$"
473+
comment => "Correct the value $(index)",
474+
replace_with => value("$(index)=$($(tab)[$(sectionName)][$(index)])"),
475+
select_region => INI_section(escape("$(set_variable_values_ini.sectionName)")),
476+
classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"),
477+
if => "edit_$(cindex[$(index)])";
466478

467479
}
468480

tests/acceptance/lib/files/set_variable_values_ini.cf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bundle agent init
3131
bundle agent test
3232
{
3333
meta:
34-
"description" -> { "CFE-3221", "CFE-3244" }
34+
"description" -> { "CFE-3221", "CFE-3244", "CFE-413" }
3535
string => "Test expected behavior of set_variable_values_ini";
3636

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

53+
"config[CFE-413][keyone]" string => "one";
54+
"config[CFE-413][keytwo]" string => "value=one";
55+
5356
files:
5457

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

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

6671
#######################################################

tests/acceptance/lib/files/set_variable_values_ini.cf.finish

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ variablename4=desired value4
1515
keytwo = valuetwo
1616
[something]
1717
keyone=something1
18+
[CFE-413]
19+
keytwo=value=one
20+
keyone=one

tests/acceptance/lib/files/set_variable_values_ini.cf.start

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ variablename3 =desired value3
1414
# keytwo = bar
1515
[something]
1616
keyone=something1
17+
[CFE-413]
18+
keytwo=value=two

0 commit comments

Comments
 (0)