Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #17 from NoRedInk/trailing-whitespace-py27
Browse files Browse the repository at this point in the history
Avoid adding trailing whitespace on python < 3.4
  • Loading branch information
eeue56 authored Nov 8, 2016
2 parents 35c160e + fe59d10 commit 225fbe4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion elm_deps_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def sync_versions(top_level_file, spec_file, quiet=False, dry=False, note_test_d
if not dry:
spec['dependencies'] = sorted_deps(spec['dependencies'])
with open(spec_file, 'w') as f:
json.dump(spec, f, sort_keys=False, indent=4)
json.dump(spec, f, sort_keys=False, indent=4, separators=(',', ': '))
else:
print("No changes written.")

Expand Down
43 changes: 43 additions & 0 deletions tests/test_elm_deps_sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import OrderedDict
import json
import difflib

import pytest
from hypothesis import given
Expand Down Expand Up @@ -66,6 +67,48 @@ def test_spec_order_is_preserved(
assert list(new_spec['test-dependencies'].keys()) == ['NoRedInk/spec-1', 'NoRedInk/spec-2']


def test_no_trailing_whitespace(tmpdir):
top_level_file = tmpdir.join('elm-package.json')
spec_file = tmpdir.join('spec-elm-package.json')

new_dep = ('NoRedInk/top-99', '1.0.0 <= v <= 1.0.0')
top_level = _make_package(package_skeleton.keys(), spec_deps + [new_dep])
top_level_file.write(json.dumps(top_level))

sorted_spec_deps = sorted(spec_deps)
spec = _make_package(package_skeleton.keys(), sorted_spec_deps)
spec_file.write(json.dumps(spec, indent=4, separators=(',', ': ')))

prev_spec_lines = spec_file.read().splitlines()

# sanity check: we're operating on a clean JSON
for line in prev_spec_lines:
assert not line.endswith(' ')

elm_deps_sync.sync_versions(
str(top_level_file),
str(spec_file),
quiet=False,
dry=False,
note_test_deps=False)

for diff in difflib.ndiff(prev_spec_lines, spec_file.read().splitlines()):
if diff.startswith(' '):
continue

# assert there's only expected diffs
if diff.startswith('+ '):
# adds new dep or trailing comma
assert (new_dep[0] in diff) or (sorted_spec_deps[-1][0] in diff)
elif diff.startswith('- '):
# trailing comma
assert sorted_spec_deps[-1][0] in diff
elif diff.startswith('? '):
pass
else:
assert False, 'unexpected diff operator in: ' + diff


def _make_package(keys, deps):
package = OrderedDict((key, package_skeleton[key]) for key in keys)
package['dependencies'] = OrderedDict(deps)
Expand Down

0 comments on commit 225fbe4

Please sign in to comment.