Skip to content

Commit

Permalink
Wrap sh calls to check for dry_run
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljoseph committed Oct 23, 2013
1 parent 0e0505b commit 7d27e37
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
14 changes: 5 additions & 9 deletions changes/shell.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import logging

import iterpipes
from changes import config

log = logging.getLogger(__name__)


def execute(command, dry_run=True):
log.debug('executing %s', command)
if not dry_run:
try:
return [result for result in iterpipes.linecmd(command)(None)]
except iterpipes.CalledProcessError, e:
log.debug('return code: %s, output: %s', e.returncode, e.output)
return False
def handle_dry_run(function, *args):
if not config.arguments.get('--dry-run', True):
return function(*args)
else:
log.debug('dry run of %s %s, skipping' % (function, args))
return True
25 changes: 25 additions & 0 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sh
from unittest2 import TestCase

from changes import config, shell


class ShellTestCase(TestCase):

def test_handle_dry_run(self):
self.assertEquals(
'',
shell.handle_dry_run(
sh.diff,
('README.md', 'README.md')
)
)

def test_handle_dry_run_true(self):
config.arguments['--dry-run'] = True
self.assertTrue(
shell.handle_dry_run(
sh.diff,
('README.md', 'README.md')
)
)

0 comments on commit 7d27e37

Please sign in to comment.