Skip to content

Commit

Permalink
Under JSON Create containing directory for config file.
Browse files Browse the repository at this point in the history
Using '~/.config/$(APPNAME)/..' is a pretty common on linux
but using it here will fail if the directory isn't manually created.

This fixes that by auto creating the directory.
  • Loading branch information
rgammans committed May 27, 2020
1 parent a890074 commit d6e3db3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions nativeconfig/configs/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class JSONConfig(BaseConfig):
JSON_PATH = None

def __init__(self):
Path(self.JSON_PATH).parent.mkdir(parents= True, exist_ok = True)
if not Path(self.JSON_PATH).is_file():
with open(self.JSON_PATH, 'w+', encoding='utf-8') as f:
f.write(json.dumps({}))
Expand Down
11 changes: 11 additions & 0 deletions test/configs/test_json_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections
import json
import os
import shutil
import tempfile
import unittest
import unittest.mock
Expand All @@ -25,6 +26,8 @@ def setUp(self):

def tearDown(self):
try:
shutil.rmtree(self.CONFIG_TYPE.JSON_PATH)
except NotADirectoryError:
os.unlink(self.CONFIG_TYPE.JSON_PATH)
except FileNotFoundError:
pass
Expand Down Expand Up @@ -63,6 +66,14 @@ class MyConfig(self.CONFIG_TYPE):
MyConfig.get_instance()
self.assertEqual(os.path.isfile(MyConfig.JSON_PATH), True)

def test_config_is_created_if_not_found__and_app_directory_need_creating(self):
class MyConfig(self.CONFIG_TYPE):
JSON_PATH = os.path.join(self.CONFIG_TYPE.JSON_PATH,'_cfgtmp.json')
first_name = StringOption('FirstName', default='Ilya')
self.assertEqual(os.path.isfile(MyConfig.JSON_PATH), False)
MyConfig.get_instance()
self.assertEqual(os.path.isfile(MyConfig.JSON_PATH), True)

def test_order_is_preserved_in_json(self):
class MyConfig(self.CONFIG_TYPE):
second_name = StringOption('SecondName', default='Kulakov')
Expand Down

0 comments on commit d6e3db3

Please sign in to comment.