Skip to content

Commit

Permalink
Try to determine the path of PostgreSQL server commands from pg_config
Browse files Browse the repository at this point in the history
pg_config is usually in the path of many distros, as soon as postgresql-client tools are installed, but server commands are not.
That's why we always had to adjust the PATH accordingly.
Determining the path of these commands automatically makes testing easier.

Extracted from #1
  • Loading branch information
larskanis committed Feb 28, 2023
1 parent 1e8dc14 commit feb64b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,22 @@ After checking out the source, install all dependencies:

$ bundle install

Cleanup extension files, packaging files, test databases:
Cleanup extension files, packaging files, test databases.
Run this to change between PostgreSQL versions:

$ rake clean

Compile extension:

$ rake compile

Run tests/specs with PostgreSQL tools like `initdb` in the path:
Run tests/specs on the PostgreSQL version that `pg_config --bindir` points to:

$ PATH=$PATH:/usr/lib/postgresql/14/bin rake test
$ rake test

Or run a specific test with the line number:
Or run a specific test per file and line number on a specific PostgreSQL version:

$ PATH=$PATH:/usr/lib/postgresql/14/bin rspec -Ilib -fd spec/pg/connection_spec.rb:455
$ PATH=/usr/lib/postgresql/14/bin:$PATH rspec -Ilib -fd spec/pg/connection_spec.rb:455

Generate the API documentation:

Expand Down
20 changes: 15 additions & 5 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def initialize( name, port: 54321, postgresql_conf: '' )
@test_dir = TEST_DIRECTORY + "tmp_test_#{@name}"
@test_pgdata = @test_dir + 'data'
@test_pgdata.mkpath
@pg_bin_dir = nil

@logfile = @test_dir + 'setup.log'
trace "Command output logged to #{@logfile}"
Expand All @@ -260,7 +261,7 @@ def initialize( name, port: 54321, postgresql_conf: '' )
unless (@test_pgdata+"postgresql.conf").exist?
FileUtils.rm_rf( @test_pgdata, :verbose => $DEBUG )
trace "Running initdb"
log_and_run @logfile, 'initdb', '-E', 'UTF8', '--no-locale', '-D', @test_pgdata.to_s
log_and_run @logfile, pg_bin_path('initdb'), '-E', 'UTF8', '--no-locale', '-D', @test_pgdata.to_s
end

unless (@test_pgdata+"ruby-pg-server-cert").exist?
Expand Down Expand Up @@ -293,7 +294,7 @@ def initialize( name, port: 54321, postgresql_conf: '' )
trace "Starting postgres"
sopt = "-p #{@port}"
sopt += " -k #{@test_dir.to_s.dump}" unless RUBY_PLATFORM=~/mingw|mswin/i
log_and_run @logfile, 'pg_ctl', '-w', '-o', sopt,
log_and_run @logfile, pg_bin_path('pg_ctl'), '-w', '-o', sopt,
'-D', @test_pgdata.to_s, 'start'
sleep 2

Expand Down Expand Up @@ -328,8 +329,8 @@ def generate_ssl_certs(output_dir)

def create_test_db
trace "Creating the test DB"
log_and_run @logfile, 'psql', '-p', @port.to_s, '-e', '-c', 'DROP DATABASE IF EXISTS test', 'postgres'
log_and_run @logfile, 'createdb', '-p', @port.to_s, '-e', 'test'
log_and_run @logfile, pg_bin_path('psql'), '-p', @port.to_s, '-e', '-c', 'DROP DATABASE IF EXISTS test', 'postgres'
log_and_run @logfile, pg_bin_path('createdb'), '-p', @port.to_s, '-e', 'test'
end

def connect
Expand All @@ -344,7 +345,16 @@ def connect
def teardown
trace "Tearing down test database for #{@name}"

log_and_run @logfile, 'pg_ctl', '-D', @test_pgdata.to_s, 'stop'
log_and_run @logfile, pg_bin_path('pg_ctl'), '-D', @test_pgdata.to_s, 'stop'
end

def pg_bin_path(cmd)
@pg_bin_dir ||= begin
`pg_config --bindir`.strip
rescue
nil
end
[@pg_bin_dir&.empty? ? nil : @pg_bin_dir, cmd].compact.join("/")
end
end

Expand Down

0 comments on commit feb64b0

Please sign in to comment.