Skip to content

Commit

Permalink
Create target directories before we begin
Browse files Browse the repository at this point in the history
Useful for CI where we do not have human interaction.
  • Loading branch information
retr0h committed Nov 23, 2016
1 parent 8070acf commit fd60886
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
20 changes: 20 additions & 0 deletions gilt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# DEALINGS IN THE SOFTWARE.

import collections
import errno
import os

import giturlparse
Expand Down Expand Up @@ -128,6 +129,7 @@ def _get_dst_dir(dst_dir):
:return: str
"""
wd = os.getcwd()
_makedirs(dst_dir)

return os.path.join(wd, dst_dir)

Expand All @@ -141,3 +143,21 @@ def _get_clone_dir():
path = '~/.gilt/clone'

return os.path.expanduser(path)


def _makedirs(path):
"""
Create a base directory of the provided path and return None.
:param path: A string containing a path to be deconstructed and basedir
created.
:return: None
"""
dirname, _ = os.path.split(path)
try:
os.makedirs(dirname)
except OSError as exc:
if exc.errno == errno.EEXIST:
pass
else:
raise
48 changes: 45 additions & 3 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# THE SOFTWARE.

import os
import stat

import pytest

Expand Down Expand Up @@ -149,7 +150,7 @@ def test_get_config_generator(gilt_config_file):


def test_get_files_generator(temp_dir):
files_list = [{'src': 'foo', 'dst': 'bar'}]
files_list = [{'src': 'foo', 'dst': 'bar/'}]
result = [i for i in config._get_files_generator('/tmp/dir', files_list)]

assert isinstance(result, list)
Expand Down Expand Up @@ -179,12 +180,53 @@ def test_get_config_handles_parse_error(gilt_config_file):

def test_get_dst_dir(temp_dir):
os.chdir(temp_dir.strpath)
result = config._get_dst_dir('role/foo')
result = config._get_dst_dir('roles/foo')

assert os.path.join(temp_dir.strpath, 'role', 'foo') == result
assert os.path.join(temp_dir.strpath, 'roles', 'foo') == result


def test_get_clone_dir():
parts = pytest.helpers.os_split(config._get_clone_dir())

assert ('.gilt', 'clone') == parts[-2:]


def test_makedir(temp_dir):
config._makedirs('foo/')

d = os.path.join(temp_dir.strpath, 'foo')
assert os.path.isdir(d)
assert '0755' == oct(os.lstat(d).st_mode & 0777)


def test_makedir_nested_directory(temp_dir):
config._makedirs('foo/bar/')

d = os.path.join(temp_dir.strpath, 'foo', 'bar')
assert os.path.isdir(d)


def test_makedir_basedir(temp_dir):
config._makedirs('foo/filename.py')

d = os.path.join(temp_dir.strpath, 'foo')
assert os.path.isdir(d)


def test_makedir_nested_basedir(temp_dir):
config._makedirs('foo/bar/filename.py')

d = os.path.join(temp_dir.strpath, 'foo', 'bar')
assert os.path.isdir(d)


def test_makedir_passes_if_exists(temp_dir):
d = os.path.join(temp_dir.strpath, 'foo')
os.mkdir(d)

config._makedirs('foo/')


def test_makedir_raises(temp_dir):
with pytest.raises(OSError):
config._makedirs('')

0 comments on commit fd60886

Please sign in to comment.