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

Commit

Permalink
Sort deps when saving to avoid surprising diffs down the road
Browse files Browse the repository at this point in the history
  • Loading branch information
Marica Odagaki committed Nov 4, 2016
1 parent 67679fc commit 035918e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
8 changes: 7 additions & 1 deletion elm_deps_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import print_function

import sys
from collections import OrderedDict
import json
import argparse

Expand Down Expand Up @@ -41,12 +42,13 @@ def sync_versions(top_level_file, spec_file, quiet=False, dry=False, note_test_d
test_deps[package_name] = package_version

if note_test_deps:
spec['test-dependencies'] = test_deps
spec['test-dependencies'] = sorted_deps(test_deps)

if len(messages) > 0 or note_test_deps:
print('{number} packages changed.'.format(number=len(messages)))

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)
else:
Expand All @@ -58,6 +60,10 @@ def sync_versions(top_level_file, spec_file, quiet=False, dry=False, note_test_d
print('No changes needed.')


def sorted_deps(deps):
return OrderedDict(sorted(deps.items()))


def main():

parser = argparse.ArgumentParser(description='Sync deps between a parent and a sub')
Expand Down
38 changes: 38 additions & 0 deletions tests/test_elm_deps_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from collections import OrderedDict
import json

import pytest

import elm_deps_sync


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

top_level = {
'dependencies': OrderedDict([
('NoRedInk/top-3', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/top-1', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/top-2', '1.0.0 <= v <= 1.0.0'),
])
}
top_level_file.write(json.dumps(top_level))

spec = {
'dependencies': OrderedDict([
('NoRedInk/top-2', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/top-1', '1.0.0 <= v <= 1.0.0'),
])
}
spec_file.write(json.dumps(spec))

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

new_spec = json.loads(spec_file.read(), object_pairs_hook=OrderedDict)
assert new_spec['dependencies'].keys() == ['NoRedInk/top-1', 'NoRedInk/top-2', 'NoRedInk/top-3']

0 comments on commit 035918e

Please sign in to comment.