File tree Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -177,20 +177,23 @@ def get_pg_version(bin_dir=None):
177177 Return PostgreSQL version provided by postmaster.
178178 """
179179
180- # get raw version (e.g. postgres (PostgreSQL) 9.5.7)
180+ # Get raw version (e.g., postgres (PostgreSQL) 9.5.7)
181181 postgres_path = os .path .join (bin_dir , 'postgres' ) if bin_dir else get_bin_path ('postgres' )
182182 _params = [postgres_path , '--version' ]
183183 raw_ver = tconf .os_ops .exec_command (_params , encoding = 'utf-8' )
184184
185- # Remove "(Homebrew)" if present
186- raw_ver = raw_ver .replace ('(Homebrew)' , '' ).strip ()
185+ return parse_pg_version (raw_ver )
187186
188- # cook version of PostgreSQL
189- version = raw_ver .strip ().split (' ' )[- 1 ] \
187+
188+ def parse_pg_version (version_out ):
189+ # Generalize removal of system-specific suffixes (anything in parentheses)
190+ raw_ver = re .sub (r'\([^)]*\)' , '' , version_out ).strip ()
191+
192+ # Cook version of PostgreSQL
193+ version = raw_ver .split (' ' )[- 1 ] \
190194 .partition ('devel' )[0 ] \
191195 .partition ('beta' )[0 ] \
192196 .partition ('rc' )[0 ]
193-
194197 return version
195198
196199
Original file line number Diff line number Diff line change 4848
4949# NOTE: those are ugly imports
5050from testgres import bound_ports
51- from testgres .utils import PgVer
51+ from testgres .utils import PgVer , parse_pg_version
5252from testgres .node import ProcessProxy
5353
5454
@@ -1023,6 +1023,16 @@ def test_upgrade_node(self):
10231023 node_new .start ()
10241024 self .assertTrue (b'Upgrade Complete' in res )
10251025
1026+ def test_parse_pg_version (self ):
1027+ # Linux Mint
1028+ assert parse_pg_version ("postgres (PostgreSQL) 15.5 (Ubuntu 15.5-1.pgdg22.04+1)" ) == "15.5"
1029+ # Linux Ubuntu
1030+ assert parse_pg_version ("postgres (PostgreSQL) 12.17" ) == "12.17"
1031+ # Windows
1032+ assert parse_pg_version ("postgres (PostgreSQL) 11.4" ) == "11.4"
1033+ # Macos
1034+ assert parse_pg_version ("postgres (PostgreSQL) 14.9 (Homebrew)" ) == "14.9"
1035+
10261036
10271037if __name__ == '__main__' :
10281038 if os .environ .get ('ALT_CONFIG' ):
You can’t perform that action at this time.
0 commit comments