Skip to content

Commit

Permalink
Parse value in Entry constructor only if not already parsed from file
Browse files Browse the repository at this point in the history
  • Loading branch information
pgiraud committed Nov 12, 2024
1 parent 5240169 commit ba45f40
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pgtoolkit/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ def __init__(
raw_line: Optional[str] = None,
) -> None:
self._name = name
if isinstance(value, str):
# We parse value only if not already parsed from a file
if raw_line is None and isinstance(value, str):
value = parse_value(value)
self._value = value
self.commented = commented
Expand Down
2 changes: 2 additions & 0 deletions tests/data/postgres.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ log_rotation_age = 1d # Automatic rotation of logfiles will
# Add settings for extensions here
pg_stat_statements.max = 10000
pg_stat_statements.track = all

some_guc = '2.30'
18 changes: 17 additions & 1 deletion tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def test_parser_includes():
"ssl": True,
"unix_socket_permissions": "0777",
"wal_level": "hot_standby",
"some_guc": "2.30",
}
assert "include" not in conf
assert "include_if_exists" not in conf
Expand All @@ -253,10 +254,12 @@ def test_parser_includes():
"# - Connection Settings -",
"listen_addresses = '*' # comma-separated list of addresses;",
]
assert lines[-3:] == [
assert lines[-5:] == [
"# Add settings for extensions here",
"pg_stat_statements.max = 10000",
"pg_stat_statements.track = all",
"",
"some_guc = '2.30'",
]


Expand Down Expand Up @@ -310,6 +313,19 @@ def test_entry_edit():
assert entry.value == 9876


def test_entry_constructor_parse_value():
from pgtoolkit.conf import Entry

entry = Entry(name="var", value="'1.2'")
assert entry.value == "1.2"
entry = Entry(name="var", value="1234")
assert entry.value == 1234
# If value come from the parsing of a file (ie. raw_line is provided) value should
# not be parsed and be kept as is
entry = Entry(name="var", value="'1.2'", raw_line="var = '1.2'")
assert entry.value == "'1.2'"


def test_serialize_entry():
from pgtoolkit.conf import Entry

Expand Down

0 comments on commit ba45f40

Please sign in to comment.