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

elm_deps_sync: avoid unwanted diffs #16

Merged
merged 4 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: python
cache: pip
python:
# see https://docs.travis-ci.com/user/languages/python/ for options
- "2.7" # hypothesis's minimum version for 2.x
- "3.3" # hypothesis's minimum version for 3.x
- "3.4" # trusty
- "3.5" # >= xenial
- "3.6-dev"
- "nightly"
# command to install dependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ thanks

install:
- "pip install -r dev-requirements.txt"
# command to run tests
script:
- "python -m pytest tests/ -v"
notifications:
email: false
4 changes: 4 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum34==1.1.6
hypothesis==3.6.0
py==1.4.31
pytest==3.0.3
10 changes: 8 additions & 2 deletions 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 All @@ -15,7 +16,7 @@ def sync_versions(top_level_file, spec_file, quiet=False, dry=False, note_test_d
top_level = json.load(f)

with open(spec_file) as f:
spec = json.load(f)
spec = json.load(f, object_pairs_hook=OrderedDict)

messages = []

Expand All @@ -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
72 changes: 72 additions & 0 deletions tests/test_elm_deps_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from collections import OrderedDict
import json

import pytest
from hypothesis import given
import hypothesis.strategies as st

import elm_deps_sync


package_skeleton = {
"version": "1.0.0",
"summary": "test elm deps sync",
"repository": "https://github.com/NoRedInk/elm-ops-tooling",
"license": "BSD-3",
"source-directories": ".",
"exposed-modules": [],
"native-modules": True,
"dependencies": {},
"elm-version": "0.17.0 <= v <= 0.17.0",
}

top_level_deps = [
('NoRedInk/top-1', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/top-2', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/top-3', '1.0.0 <= v <= 1.0.0'),
]

spec_deps = [
('NoRedInk/top-1', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/top-2', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/spec-1', '1.0.0 <= v <= 1.0.0'),
('NoRedInk/spec-2', '1.0.0 <= v <= 1.0.0'),
]


@given(top_level_keys=st.permutations(package_skeleton.keys()),
top_level_deps=st.permutations(top_level_deps),
spec_keys=st.permutations(package_skeleton.keys()),
spec_deps=st.permutations(spec_deps))
def test_spec_order_is_preserved(
tmpdir,
top_level_keys,
top_level_deps,
spec_keys,
spec_deps):
top_level_file = tmpdir.join('elm-package.json')
spec_file = tmpdir.join('spec-elm-package.json')

top_level = _make_package(top_level_keys, top_level_deps)
top_level_file.write(json.dumps(top_level))

spec = _make_package(spec_keys, spec_deps)
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 list(new_spec.keys()) == spec_keys + ['test-dependencies']
assert list(new_spec['dependencies'].keys()) == ['NoRedInk/spec-1', 'NoRedInk/spec-2', 'NoRedInk/top-1', 'NoRedInk/top-2', 'NoRedInk/top-3']
assert list(new_spec['test-dependencies'].keys()) == ['NoRedInk/spec-1', 'NoRedInk/spec-2']


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