Skip to content

Commit

Permalink
Composers respect the force configuration.
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
mblayman committed Sep 13, 2015
1 parent 83873c0 commit 061a639
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Version 2.1, In Development

* Use the SmartyPants library to generate better quotation
marks for Markdown.
* Composers can be forced to compose with the ``--force`` flag.

Version 2.0, Released July 25, 2015
-----------------------------------
Expand Down
5 changes: 4 additions & 1 deletion handroll/composers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ def compose(self, catalog, source_file, out_dir):
# Do not copy files that are already there unless different.
destination = os.path.join(out_dir, filename)
if os.path.exists(destination):
if filecmp.cmp(source_file, destination):
if (
not self._config.force and
filecmp.cmp(source_file, destination)
):
# Files are equal. Do nothing.
logger.debug(_('Skipping {filename} ... It is the same as '
'{destination}.').format(
Expand Down
3 changes: 3 additions & 0 deletions handroll/composers/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def compose(self, catalog, source_file, out_dir):
def _needs_update(self, source_file, out_file):
"""Check if the output file needs to be updated by looking at the
modified times of the source file and output file."""
if self._config.force:
return True

if os.path.exists(out_file):
return os.path.getmtime(source_file) > os.path.getmtime(out_file)
else:
Expand Down
3 changes: 3 additions & 0 deletions handroll/composers/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def _has_frontmatter(self, first_line):
def _needs_update(self, template, source_file, output_file):
"""Check if the output file needs to be updated by looking at the
modified times of the template, source file, and output file."""
if self._config.force:
return True

out_modified_time = None
if os.path.exists(output_file):
out_modified_time = os.path.getmtime(output_file)
Expand Down
1 change: 1 addition & 0 deletions handroll/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Configuration(object):

def __init__(self):
self.active_extensions = set()
self.force = False
# The output directory should be absolute. That constraint will make it
# easy to check if a filepath is in the output directory.
self.outdir = None
Expand Down
46 changes: 46 additions & 0 deletions handroll/tests/test_composers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ def test_output_extension(self):
composer = self._make_one()
self.assertEqual('.xml', composer.output_extension)

@mock.patch('handroll.composers.atom.json')
def test_forces_update(self, json):
json.loads.return_value = {
'title': 'Fakity Fake',
'id': "let's pretend this is unique",
'entries': [{
'title': 'Sample A',
'updated': '2014-02-23T00:00:00',
'published': '2014-02-22T00:00:00',
'url': 'http://some.website.com/a.html',
'summary': 'A summary of the sample post'
}]
}
open(self.output_file, 'w').close()
composer = self._make_one()
composer._config.force = True
composer.compose(None, self.source_file, self.outdir)
self.assertTrue(json.loads.called)


class TestCopyComposer(TestCase):

Expand Down Expand Up @@ -153,6 +172,19 @@ def test_output_extension(self):
composer = self._make_one()
self.assertRaises(AttributeError, lambda: composer.output_extension)

@mock.patch('handroll.composers.shutil')
def test_copies_when_forced(self, shutil):
marker = 'marker.txt'
source = tempfile.mkdtemp()
source_file = os.path.join(source, marker)
outdir = tempfile.mkdtemp()
open(source_file, 'w').close()
open(os.path.join(outdir, marker), 'w').close()
composer = self._make_one()
composer._config.force = True
composer.compose(None, source_file, outdir)
self.assertTrue(shutil.copy.called)


class TestGenericHTMLComposer(TestCase):

Expand Down Expand Up @@ -241,6 +273,20 @@ def test_output_extension(self):
composer = self._make_one()
self.assertEqual('.html', composer.output_extension)

def test_forces_update(self):
site = tempfile.mkdtemp()
output_file = os.path.join(site, 'output.md')
open(output_file, 'w').close()
past = os.path.getmtime(output_file) - 10
source_file = os.path.join(site, 'test.md')
open(source_file, 'w').close()
os.utime(source_file, (past, past))
template = mock.MagicMock(last_modified=past)
composer = self._make_one()
composer._config.force = True
self.assertTrue(
composer._needs_update(template, source_file, output_file))


class TestMarkdownComposer(TestCase):

Expand Down

0 comments on commit 061a639

Please sign in to comment.