Default project_name to dirname of fig.yml

Signed-off-by: Ryan Brainard <brainard@heroku.com>
This commit is contained in:
Ryan Brainard 2014-07-12 23:35:37 -07:00
parent 8e265905d3
commit e9c2f2c5fb
3 changed files with 24 additions and 15 deletions

View File

@ -23,7 +23,7 @@ class Command(DocoptCommand):
base_dir = '.' base_dir = '.'
def __init__(self): def __init__(self):
self.yaml_path = os.environ.get('FIG_FILE', None) self._yaml_path = os.environ.get('FIG_FILE', None)
self.explicit_project_name = None self.explicit_project_name = None
def dispatch(self, *args, **kwargs): def dispatch(self, *args, **kwargs):
@ -56,10 +56,7 @@ class Command(DocoptCommand):
@cached_property @cached_property
def project(self): def project(self):
try: try:
yaml_path = self.yaml_path config = yaml.safe_load(open(self.yaml_path))
if yaml_path is None:
yaml_path = self.check_yaml_filename()
config = yaml.safe_load(open(yaml_path))
except IOError as e: except IOError as e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
raise errors.FigFileNotFound(os.path.basename(e.filename)) raise errors.FigFileNotFound(os.path.basename(e.filename))
@ -72,7 +69,7 @@ class Command(DocoptCommand):
@cached_property @cached_property
def project_name(self): def project_name(self):
project = os.path.basename(os.getcwd()) project = os.path.basename(os.path.dirname(self.yaml_path))
if self.explicit_project_name is not None: if self.explicit_project_name is not None:
project = self.explicit_project_name project = self.explicit_project_name
project = re.sub(r'[^a-zA-Z0-9]', '', project) project = re.sub(r'[^a-zA-Z0-9]', '', project)
@ -84,8 +81,11 @@ class Command(DocoptCommand):
def formatter(self): def formatter(self):
return Formatter() return Formatter()
def check_yaml_filename(self): @cached_property
if os.path.exists(os.path.join(self.base_dir, 'fig.yaml')): def yaml_path(self):
if self._yaml_path is not None:
return self._yaml_path
elif os.path.exists(os.path.join(self.base_dir, 'fig.yaml')):
log.warning("Fig just read the file 'fig.yaml' on startup, rather than 'fig.yml'") log.warning("Fig just read the file 'fig.yaml' on startup, rather than 'fig.yml'")
log.warning("Please be aware that fig.yml the expected extension in most cases, and using .yaml can cause compatibility issues in future") log.warning("Please be aware that fig.yml the expected extension in most cases, and using .yaml can cause compatibility issues in future")
@ -93,3 +93,7 @@ class Command(DocoptCommand):
return os.path.join(self.base_dir, 'fig.yaml') return os.path.join(self.base_dir, 'fig.yaml')
else: else:
return os.path.join(self.base_dir, 'fig.yml') return os.path.join(self.base_dir, 'fig.yml')
@yaml_path.setter
def yaml_path(self, value):
self._yaml_path = value

View File

@ -22,7 +22,7 @@ class CLITestCase(DockerClientTestCase):
def test_ps(self, mock_stdout): def test_ps(self, mock_stdout):
self.command.project.get_service('simple').create_container() self.command.project.get_service('simple').create_container()
self.command.dispatch(['ps'], None) self.command.dispatch(['ps'], None)
self.assertIn('fig_simple_1', mock_stdout.getvalue()) self.assertIn('simplefigfile_simple_1', mock_stdout.getvalue())
@patch('sys.stdout', new_callable=StringIO) @patch('sys.stdout', new_callable=StringIO)
def test_ps_default_figfile(self, mock_stdout): def test_ps_default_figfile(self, mock_stdout):
@ -31,9 +31,9 @@ class CLITestCase(DockerClientTestCase):
self.command.dispatch(['ps'], None) self.command.dispatch(['ps'], None)
output = mock_stdout.getvalue() output = mock_stdout.getvalue()
self.assertIn('fig_simple_1', output) self.assertIn('multiplefigfiles_simple_1', output)
self.assertIn('fig_another_1', output) self.assertIn('multiplefigfiles_another_1', output)
self.assertNotIn('fig_yetanother_1', output) self.assertNotIn('multiplefigfiles_yetanother_1', output)
@patch('sys.stdout', new_callable=StringIO) @patch('sys.stdout', new_callable=StringIO)
def test_ps_alternate_figfile(self, mock_stdout): def test_ps_alternate_figfile(self, mock_stdout):
@ -42,9 +42,9 @@ class CLITestCase(DockerClientTestCase):
self.command.dispatch(['-f', 'fig2.yml', 'ps'], None) self.command.dispatch(['-f', 'fig2.yml', 'ps'], None)
output = mock_stdout.getvalue() output = mock_stdout.getvalue()
self.assertNotIn('fig_simple_1', output) self.assertNotIn('multiplefigfiles_simple_1', output)
self.assertNotIn('fig_another_1', output) self.assertNotIn('multiplefigfiles_another_1', output)
self.assertIn('fig_yetanother_1', output) self.assertIn('multiplefigfiles_yetanother_1', output)
def test_up(self): def test_up(self):
self.command.dispatch(['up', '-d'], None) self.command.dispatch(['up', '-d'], None)

View File

@ -5,6 +5,11 @@ from fig.cli.main import TopLevelCommand
from fig.packages.six import StringIO from fig.packages.six import StringIO
class CLITestCase(unittest.TestCase): class CLITestCase(unittest.TestCase):
def test_project_name_defaults_to_dirname(self):
command = TopLevelCommand()
command.base_dir = 'tests/fixtures/simple-figfile'
self.assertEquals('simplefigfile', command.project_name)
def test_yaml_filename_check(self): def test_yaml_filename_check(self):
command = TopLevelCommand() command = TopLevelCommand()
command.base_dir = 'tests/fixtures/longer-filename-figfile' command.base_dir = 'tests/fixtures/longer-filename-figfile'