Repository: Kinto/kinto

Issue:
Error while trying to generate a configuration file without subfolder with CLI.
```
$ kinto --ini kinto.ini

Traceback (most recent call last):
  File "~/.virtualenvs/kinto/bin/kinto", line 9, in <module>
    load_entry_point('kinto', 'console_scripts', 'kinto')()
  File "~/mozilla/kinto/kinto/__main__.py", line 72, in main
    init(config_file, backend)
  File "~/mozilla/kinto/kinto/config/__init__.py", line 50, in init
    render_template("kinto.tpl", config_file, **values)
  File "~/mozilla/kinto/kinto/config/__init__.py", line 14, in render_template
    os.makedirs(folder)
  File "~/.virtualenvs/kinto/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 2] No such file or directory: ''
```



Discussion:

Resolving patch:
diff --git a/kinto/config/__init__.py b/kinto/config/__init__.py
--- a/kinto/config/__init__.py
+++ b/kinto/config/__init__.py
@@ -12,7 +12,7 @@ def render_template(template, destination, **kwargs):
     template = os.path.join(HERE, template)
     folder = os.path.dirname(destination)
 
-    if not os.path.exists(folder):
+    if folder and not os.path.exists(folder):
         os.makedirs(folder)
 
     logger.info("Created config {}".format(os.path.abspath(destination)))


Test patch:
diff --git a/kinto/tests/test_config.py b/kinto/tests/test_config.py
--- a/kinto/tests/test_config.py
+++ b/kinto/tests/test_config.py
@@ -6,7 +6,7 @@
 
 import six
 
-from kinto.config import render_template, init
+from kinto import config
 
 
 class ConfigTest(unittest.TestCase):
@@ -14,14 +14,14 @@ def test_transpose_parameters_into_template(self):
         self.maxDiff = None
         template = "kinto.tpl"
         dest = tempfile.mktemp()
-        render_template(template, dest,
-                        secret='secret',
-                        storage_backend='storage_backend',
-                        cache_backend='cache_backend',
-                        permission_backend='permission_backend',
-                        storage_url='storage_url',
-                        cache_url='cache_url',
-                        permission_url='permission_url')
+        config.render_template(template, dest,
+                               secret='secret',
+                               storage_backend='storage_backend',
+                               cache_backend='cache_backend',
+                               permission_backend='permission_backend',
+                               storage_url='storage_url',
+                               cache_url='cache_url',
+                               permission_url='permission_url')
 
         with codecs.open(dest, 'r', encoding='utf-8') as d:
             destination_temp = d.read()
@@ -36,26 +36,26 @@ def test_transpose_parameters_into_template(self):
     def test_create_destination_directory(self):
         dest = os.path.join(tempfile.mkdtemp(), 'config', 'kinto.ini')
 
-        render_template("kinto.tpl", dest,
-                        secret='secret',
-                        storage_backend='storage_backend',
-                        cache_backend='cache_backend',
-                        permission_backend='permission_backend',
-                        storage_url='storage_url',
-                        cache_url='cache_url',
-                        permission_url='permission_url')
+        config.render_template("kinto.tpl", dest,
+                               secret='secret',
+                               storage_backend='storage_backend',
+                               cache_backend='cache_backend',
+                               permission_backend='permission_backend',
+                               storage_url='storage_url',
+                               cache_url='cache_url',
+                               permission_url='permission_url')
 
         self.assertTrue(os.path.exists(dest))
 
     @mock.patch('kinto.config.render_template')
     def test_hmac_secret_is_text(self, mocked_render_template):
-        init('kinto.ini', 'postgresql')
+        config.init('kinto.ini', 'postgresql')
         args, kwargs = list(mocked_render_template.call_args)
         self.assertEquals(type(kwargs['secret']), six.text_type)
 
     @mock.patch('kinto.config.render_template')
     def test_init_postgresql_values(self, mocked_render_template):
-        init('kinto.ini', 'postgresql')
+        config.init('kinto.ini', 'postgresql')
 
         args, kwargs = list(mocked_render_template.call_args)
         self.assertEquals(args, ('kinto.tpl', 'kinto.ini'))
@@ -73,7 +73,7 @@ def test_init_postgresql_values(self, mocked_render_template):
 
     @mock.patch('kinto.config.render_template')
     def test_init_redis_values(self, mocked_render_template):
-        init('kinto.ini', 'redis')
+        config.init('kinto.ini', 'redis')
 
         args, kwargs = list(mocked_render_template.call_args)
         self.assertEquals(args, ('kinto.tpl', 'kinto.ini'))
@@ -91,7 +91,7 @@ def test_init_redis_values(self, mocked_render_template):
 
     @mock.patch('kinto.config.render_template')
     def test_init_memory_values(self, mocked_render_template):
-        init('kinto.ini', 'memory')
+        config.init('kinto.ini', 'memory')
 
         args, kwargs = list(mocked_render_template.call_args)
         self.assertEquals(args, ('kinto.tpl', 'kinto.ini'))
@@ -105,3 +105,33 @@ def test_init_memory_values(self, mocked_render_template):
             'cache_url':  '',
             'permission_url': ''
         })
+
+    def test_render_template_creates_directory_if_necessary(self):
+        temp_path = tempfile.mkdtemp()
+        destination = os.path.join(temp_path, 'config/kinto.ini')
+        config.render_template('kinto.tpl', destination, **{
+            'secret': "abcd-ceci-est-un-secret",
+            'storage_backend': 'cliquet.storage.memory',
+            'cache_backend': 'cliquet.cache.memory',
+            'permission_backend': 'cliquet.permission.memory',
+            'storage_url': '',
+            'cache_url':  '',
+            'permission_url': ''
+        })
+        self.assertTrue(os.path.exists(destination))
+
+    def test_render_template_works_with_file_in_cwd(self):
+        temp_path = tempfile.mkdtemp()
+        os.chdir(temp_path)
+        config.render_template('kinto.tpl', 'kinto.ini', **{
+            'secret': "abcd-ceci-est-un-secret",
+            'storage_backend': 'cliquet.storage.memory',
+            'cache_backend': 'cliquet.cache.memory',
+            'permission_backend': 'cliquet.permission.memory',
+            'storage_url': '',
+            'cache_url':  '',
+            'permission_url': ''
+        })
+        self.assertTrue(os.path.exists(
+            os.path.join(temp_path, 'kinto.ini')
+        ))