mirror of https://github.com/dbcli/pgcli
factor out `ensure_dir_exists` and use for log
This commit is contained in:
parent
43a13ba065
commit
85181ab252
|
@ -19,18 +19,23 @@ def load_config(usr_cfg, def_cfg=None):
|
|||
|
||||
return cfg
|
||||
|
||||
|
||||
def ensure_dir_exists(path):
|
||||
parent_dir = dirname(path)
|
||||
try:
|
||||
os.makedirs(parent_dir)
|
||||
except OSError as exc:
|
||||
# ignore existing destination (py2 has no exist_ok arg to makedirs)
|
||||
if exc.errno != errno.EEXIST:
|
||||
raise
|
||||
|
||||
|
||||
def write_default_config(source, destination, overwrite=False):
|
||||
destination = expanduser(destination)
|
||||
if not overwrite and exists(destination):
|
||||
return
|
||||
|
||||
destination_dir = dirname(destination)
|
||||
try:
|
||||
os.makedirs(destination_dir)
|
||||
except OSError as exc:
|
||||
# ignore existing destination (py2 has no exist_ok arg to makedirs)
|
||||
if exc.errno != errno.EEXIST:
|
||||
raise
|
||||
ensure_dir_exists(destination)
|
||||
|
||||
shutil.copyfile(source, destination)
|
||||
|
||||
|
|
|
@ -44,7 +44,9 @@ from .pgstyle import style_factory
|
|||
from .pgexecute import PGExecute
|
||||
from .pgbuffer import PGBuffer
|
||||
from .completion_refresher import CompletionRefresher
|
||||
from .config import write_default_config, load_config, config_location
|
||||
from .config import (
|
||||
write_default_config, load_config, config_location, ensure_dir_exists,
|
||||
)
|
||||
from .key_bindings import pgcli_bindings
|
||||
from .encodingutils import utf8tounicode
|
||||
from .__init__ import __version__
|
||||
|
@ -185,6 +187,7 @@ class PGCli(object):
|
|||
log_file = self.config['main']['log_file']
|
||||
if log_file == 'default':
|
||||
log_file = config_location() + 'log'
|
||||
ensure_dir_exists(log_file)
|
||||
log_level = self.config['main']['log_level']
|
||||
|
||||
level_map = {'CRITICAL': logging.CRITICAL,
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
import os
|
||||
import stat
|
||||
|
||||
import pytest
|
||||
|
||||
from pgcli.config import ensure_dir_exists
|
||||
|
||||
|
||||
def test_ensure_file_parent(tmpdir):
|
||||
subdir = tmpdir.join("subdir")
|
||||
rcfile = subdir.join("rcfile")
|
||||
ensure_dir_exists(str(rcfile))
|
||||
|
||||
|
||||
def test_ensure_existing_dir(tmpdir):
|
||||
rcfile = str(tmpdir.mkdir("subdir").join("rcfile"))
|
||||
|
||||
# should just not raise
|
||||
ensure_dir_exists(rcfile)
|
||||
|
||||
|
||||
def test_ensure_other_create_error(tmpdir):
|
||||
subdir = tmpdir.join("subdir")
|
||||
rcfile = subdir.join("rcfile")
|
||||
|
||||
# trigger an oserror that isn't "directory already exists"
|
||||
os.chmod(str(tmpdir), stat.S_IREAD)
|
||||
|
||||
with pytest.raises(OSError):
|
||||
ensure_dir_exists(str(rcfile))
|
|
@ -78,26 +78,8 @@ def test_i_works(tmpdir, executor):
|
|||
run(executor, statement, pgspecial=cli.pgspecial)
|
||||
|
||||
|
||||
def test_existing_rc_dir(tmpdir):
|
||||
rcfile = str(tmpdir.mkdir("subdir").join("rcfile"))
|
||||
|
||||
PGCli(pgclirc_file=rcfile)
|
||||
assert os.path.exists(rcfile)
|
||||
|
||||
|
||||
def test_missing_rc_dir(tmpdir):
|
||||
rcfile = str(tmpdir.join("subdir").join("rcfile"))
|
||||
|
||||
PGCli(pgclirc_file=rcfile)
|
||||
assert os.path.exists(rcfile)
|
||||
|
||||
|
||||
def test_other_rcdir_create_error(tmpdir):
|
||||
subdir = tmpdir.join("subdir")
|
||||
rcfile = subdir.join("rcfile")
|
||||
|
||||
# trigger an oserror that isn't "directory already exists"
|
||||
os.chmod(str(tmpdir), stat.S_IREAD)
|
||||
|
||||
with pytest.raises(OSError):
|
||||
PGCli(pgclirc_file=str(rcfile))
|
||||
|
|
Loading…
Reference in New Issue