Skip to content

Commit 1b9a293

Browse files
author
vshepard
committed
Fix set_auto_conf with single quotes
1 parent 655a386 commit 1b9a293

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

testgres/node.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,17 +1627,22 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):
16271627
name, var = line.partition('=')[::2]
16281628
name = name.strip()
16291629
var = var.strip()
1630-
var = var.strip('"')
1631-
var = var.strip("'")
16321630

1633-
# remove options specified in rm_options list
1631+
# Handle quoted values and remove escaping
1632+
if var.startswith("'") and var.endswith("'"):
1633+
var = var[1:-1].replace("''", "'")
1634+
1635+
# Remove options specified in rm_options list
16341636
if name in rm_options:
16351637
continue
16361638

16371639
current_options[name] = var
16381640

16391641
for option in options:
1640-
current_options[option] = options[option]
1642+
value = options[option]
1643+
if isinstance(value, str):
1644+
value = value.replace("'", "\\'")
1645+
current_options[option] = value
16411646

16421647
auto_conf = ''
16431648
for option in current_options:

tests/test_simple.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,34 @@ def test_simple_with_bin_dir(self):
10611061
except FileNotFoundError:
10621062
pass # Expected error
10631063

1064+
def test_set_auto_conf(self):
1065+
with get_new_node() as node:
1066+
node.init().start()
1067+
1068+
options = {
1069+
"archive_command": "cp '%p' \"/mnt/server/archivedir/%f\"",
1070+
'restore_command': 'cp "/mnt/server/archivedir/%f" \'%p\'',
1071+
}
1072+
1073+
node.set_auto_conf(options)
1074+
node.stop()
1075+
node.slow_start()
1076+
1077+
auto_conf_path = f"{node.data_dir}/postgresql.auto.conf"
1078+
with open(auto_conf_path, "r") as f:
1079+
content = f.read()
1080+
self.assertIn(
1081+
"archive_command = 'cp \\'%p\\' \"/mnt/server/archivedir/%f\"",
1082+
content,
1083+
"archive_command stored wrong"
1084+
)
1085+
self.assertIn(
1086+
"restore_command = 'cp \"/mnt/server/archivedir/%f\" \\'%p\\''",
1087+
content,
1088+
"restore_command stored wrong"
1089+
)
1090+
1091+
10641092

10651093
if __name__ == '__main__':
10661094
if os.environ.get('ALT_CONFIG'):

0 commit comments

Comments
 (0)