diff --git a/src/pytest_postgresql/executor.py b/src/pytest_postgresql/executor.py index 72dfaaee..9350ecc3 100644 --- a/src/pytest_postgresql/executor.py +++ b/src/pytest_postgresql/executor.py @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 5670a6a2..9d883b32 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 83f33b8f..100d3582 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -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, _):