From 035918e22b38a0c3e9b570741a984591b916c94a Mon Sep 17 00:00:00 2001 From: Marica Odagaki Date: Thu, 3 Nov 2016 21:02:18 -0700 Subject: [PATCH] Sort deps when saving to avoid surprising diffs down the road --- elm_deps_sync.py | 8 +++++++- tests/test_elm_deps_sync.py | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/test_elm_deps_sync.py diff --git a/elm_deps_sync.py b/elm_deps_sync.py index 9f097c7..95bffc8 100755 --- a/elm_deps_sync.py +++ b/elm_deps_sync.py @@ -2,6 +2,7 @@ from __future__ import print_function import sys +from collections import OrderedDict import json import argparse @@ -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: @@ -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') diff --git a/tests/test_elm_deps_sync.py b/tests/test_elm_deps_sync.py new file mode 100644 index 0000000..08b3d99 --- /dev/null +++ b/tests/test_elm_deps_sync.py @@ -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']