def test_find_filename_custom(mocker): stat = mocker.patch('ldap2pg.config.stat') from ldap2pg.config import Configuration, UserError config = Configuration() # Read from env var LDAP2PG_CONFIG stat.reset_mock() stat.side_effect = [ OSError(), AssertionError("Not reached."), ] with pytest.raises(UserError): config.find_filename(environ=dict(LDAP2PG_CONFIG=b'my.yml')) # Read from args stat.reset_mock() stat.side_effect = [ mocker.Mock(st_mode=0o600), AssertionError("Not reached."), ] filename, mode = config.find_filename( environ=dict(LDAP2PG_CONFIG=b'env.yml'), args=MockArgs(config='argv.yml'), ) assert filename.endswith('argv.yml')
def test_find_filename_default(mocker): stat = mocker.patch('ldap2pg.config.stat') from ldap2pg.config import Configuration, NoConfigurationError config = Configuration() def mk_oserror(errno=None): e = OSError() e.errno = errno return e # Search default path stat.side_effect = [ mk_oserror(), mk_oserror(13), mocker.Mock(st_mode=0o600), ] filename, mode = config.find_filename(environ=dict()) assert config._file_candidates[2] == filename assert 0o600 == mode # No files at all stat.side_effect = OSError() with pytest.raises(NoConfigurationError): config.find_filename(environ=dict())
def test_find_filename_stdin(): from ldap2pg.config import Configuration config = Configuration() filename, mode = config.find_filename(environ=dict(LDAP2PG_CONFIG=b'-'), ) assert '-' == filename assert 0o400 == mode