Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/pytest_postgresql/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,23 @@ def init_directory(self):
return
# remove old one if exists first.
self.clean_directory()
init_directory = [self.executable, 'initdb', '--pgdata', self.datadir]
options = ['--username=%s' % self.user]
init_directory = [
self.executable, 'initdb', f'-o "--auth=trust --username={self.user}"',
f'-D {self.datadir}'
]

if self.password:
password = self.password if isinstance(self.password, bytes) else self.password.encode()
with tempfile.NamedTemporaryFile() as password_file:
options += ['--auth=password',
'--pwfile=%s' % password_file.name]
if hasattr(self.password, 'encode'):
password = self.password.encode('utf-8')
else:
password = self.password
init_directory += [
f'-o "--pwfile={password_file.name}"',
]
password_file.write(password)
# Without flush(), PostgreSQL will se password file as empty
password_file.flush()
init_directory += ['-o', ' '.join(options)]
subprocess.check_output(init_directory)
subprocess.check_output(' '.join(init_directory), shell=True)
else:
options += ['--auth=trust']
init_directory += ['-o', ' '.join(options)]
subprocess.check_output(init_directory)
subprocess.check_output(' '.join(init_directory), shell=True)

self._directory_initialised = True

Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@

postgresql_rand_proc = factories.postgresql_proc(port=None)
postgresql_rand = factories.postgresql('postgresql_rand_proc')
postgresql_password_proc = factories.postgresql_proc(password='test')
postgresql_password = factories.postgresql('postgresql_password_proc')
# pylint:enable=invalid-name
5 changes: 5 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def test_rand_postgres_port(postgresql_rand):
assert postgresql_rand.status == psycopg2.extensions.STATUS_READY


def test_postgres_password(postgresql_password):
"""Check if postgres fixture can be started on random port."""
assert postgresql_password.status == psycopg2.extensions.STATUS_READY


@pytest.mark.parametrize('_', range(2))
def test_postgres_terminate_connection(
postgresql, _):
Expand Down