summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pgcli/config.py17
-rwxr-xr-xpgcli/main.py5
-rw-r--r--tests/test_config.py30
-rw-r--r--tests/test_main.py18
4 files changed, 45 insertions, 25 deletions
diff --git a/pgcli/config.py b/pgcli/config.py
index 026abace..e2868676 100644
--- a/pgcli/config.py
+++ b/pgcli/config.py
@@ -19,19 +19,24 @@ def load_config(usr_cfg, def_cfg=None):
return cfg
-def write_default_config(source, destination, overwrite=False):
- destination = expanduser(destination)
- if not overwrite and exists(destination):
- return
- destination_dir = dirname(destination)
+def ensure_dir_exists(path):
+ parent_dir = dirname(path)
try:
- os.makedirs(destination_dir)
+ 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
+
+ ensure_dir_exists(destination)
+
shutil.copyfile(source, destination)
def upgrade_config(config, def_config):
diff --git a/pgcli/main.py b/pgcli/main.py
index 0e0c382a..29315d72 100755
--- a/pgcli/main.py
+++ b/pgcli/main.py
@@ -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,
diff --git a/tests/test_config.py b/tests/test_config.py
new file mode 100644
index 00000000..1c023e02
--- /dev/null
+++ b/tests/test_config.py
@@ -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))
diff --git a/tests/test_main.py b/tests/test_main.py
index a30d1164..add1f7aa 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -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))