Skip to content

Commit

Permalink
fix: only create config file on write (#1559)
Browse files Browse the repository at this point in the history
  • Loading branch information
sriram-mv authored Nov 20, 2019
1 parent 9d2063a commit 10b4357
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 7 additions & 5 deletions samcli/lib/config/samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ def __init__(self, config_dir, filename=None):
could automatically support auto-resolving multiple config files within same directory.
"""
self.filepath = Path(config_dir, filename or DEFAULT_CONFIG_FILE_NAME)
if not self.filepath.exists():
open(self.filepath, "a+").close()

def get_all(self, cmd_names, section, env=DEFAULT_ENV):
"""
Expand Down Expand Up @@ -153,15 +151,19 @@ def _read(self):
if self.document:
return self.document

txt = self.filepath.read_text()
self.document = tomlkit.loads(txt)
try:
txt = self.filepath.read_text()
self.document = tomlkit.loads(txt)
except OSError:
self.document = tomlkit.document()

return self.document

def _write(self):
if not self.document:
return

if not self.exists():
open(self.filepath, "a+").close()
self.filepath.write_text(tomlkit.dumps(self.document))

@staticmethod
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/lib/config/test_samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class TestSamConfig(TestCase):
def setUp(self):
self.config_dir = os.getcwd()
self.samconfig = SamConfig(self.config_dir)
open(self.samconfig.path(), "w").close()

def tearDown(self):
if self.samconfig.exists():
Expand All @@ -19,6 +18,8 @@ def tearDown(self):
def _setup_config(self):
self.samconfig.put(cmd_names=["local", "start", "api"], section="parameters", key="port", value=5401)
self.samconfig.flush()
self.assertTrue(self.samconfig.exists())
self.assertTrue(self.samconfig.sanity_check())

def test_init(self):
self.assertEqual(self.samconfig.filepath, Path(self.config_dir, DEFAULT_CONFIG_FILE_NAME))
Expand All @@ -32,3 +33,6 @@ def test_check_config_get(self):
def test_check_config_exists(self):
self._setup_config()
self.assertTrue(self.samconfig.exists())

def test_check_sanity(self):
self.assertTrue(self.samconfig.sanity_check())

0 comments on commit 10b4357

Please sign in to comment.