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

Commit

Permalink
Add native_package_install test and rename functions/variables to mat…
Browse files Browse the repository at this point in the history
…ch the nomenclature used in elm compiler
  • Loading branch information
ento committed Nov 16, 2016
1 parent 973baf2 commit b9d1532
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 36 deletions.
3 changes: 3 additions & 0 deletions dev-requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hypothesis
pytest
pytest-mock
17 changes: 14 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
enum34==1.1.6
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file dev-requirements.txt dev-requirements.in
#
enum34==1.1.6 # via hypothesis
funcsigs==1.0.2 # via mock
hypothesis==3.6.0
py==1.4.31
pytest==3.0.3
mock==2.0.0 # via pytest-mock
pbr==1.10.0 # via mock
py==1.4.31 # via pytest
pytest-mock==1.4.0
pytest==3.0.4
six==1.10.0 # via mock
66 changes: 33 additions & 33 deletions native_package_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,38 @@ def read_native_elm_package(package_file):
def format_url(package):
"""
Creates the url to fetch the tar from github.
>>> format_url({'namespace': 'elm-lang', 'name': 'navigation', 'version': '2.0.0'})
>>> format_url({'user': 'elm-lang', 'project': 'navigation', 'version': '2.0.0'})
'https://github.com/elm-lang/navigation/archive/2.0.0.tar.gz'
"""
return "https://github.com/{namespace}/{name}/archive/{version}.tar.gz".format(**package)
return "https://github.com/{user}/{project}/archive/{version}.tar.gz".format(**package)


def parse_json(raw):
def packages_from_exact_deps(exact_dependencies):
"""
Parses the json and returns a list of {version, namespace, name}.
>>> parse_json({'elm-lang/navigation': '2.0.0'})
[{'version': '2.0.0', 'namespace': 'elm-lang', 'name': 'navigation'}]
Parses the json and returns a list of {version, user, project}.
>>> packages_from_exact_deps({'elm-lang/navigation': '2.0.0'})
[{'version': '2.0.0', 'user': 'elm-lang', 'project': 'navigation'}]
"""
result = []

for name, version in raw.items():
namespace, package_name = name.split('/')
for package, version in exact_dependencies.items():
user, project = package.split('/')
result.append({
'namespace': namespace,
'name': package_name,
'user': user,
'project': project,
'version': version
})

return result


def format_vendor_dir(base, namespace):
def format_vendor_dir(base, user):
"""
Creates the path in the vendor folder.
>>> format_vendor_dir('foo', 'bar')
'foo/bar'
"""
path = os.path.join(base, namespace)
path = os.path.join(base, user)

try:
os.makedirs(path)
Expand All @@ -68,12 +68,12 @@ def format_vendor_dir(base, namespace):
def package_dir(vendor_dir, package):
"""
Creates the path to the elm package.
>>> package_dir('vendor/assets/elm', {'version': '2.0.0', 'namespace': 'elm-lang', 'name': 'navigation'})
>>> package_dir('vendor/assets/elm', {'version': '2.0.0', 'user': 'elm-lang', 'project': 'navigation'})
'vendor/assets/elm/elm-lang/navigation-2.0.0'
"""
return "{vendor_dir}/{package_name}-{version}".format(
vendor_dir=format_vendor_dir(vendor_dir, package['namespace']),
package_name=package['name'],
vendor_dir=format_vendor_dir(vendor_dir, package['user']),
package_name=package['project'],
version=package['version']
)

Expand All @@ -84,10 +84,10 @@ def fetch_packages(vendor_dir, packages):
"""
for package in packages:
tar_filename = format_tar_file(vendor_dir, package)
vendor = format_vendor_dir(vendor_dir, package['namespace'])
vendor = format_vendor_dir(vendor_dir, package['user'])
url = format_url(package)

print("Downloading {namespace}/{name} {version}".format(**package))
print("Downloading {user}/{project} {version}".format(**package))
tar_file = urllib2.urlopen(url)
with open(tar_filename, 'w') as tar:
tar.write(tar_file.read())
Expand All @@ -101,35 +101,35 @@ def fetch_packages(vendor_dir, packages):
def format_tar_file(vendor_dir, package):
"""
The name of the tar.
>>> format_tar_file('vendor/assets/elm', {'namespace': 'elm-lang', 'name': 'navigation', 'version': '2.0.0'})
>>> format_tar_file('vendor/assets/elm', {'user': 'elm-lang', 'project': 'navigation', 'version': '2.0.0'})
'vendor/assets/elm/elm-lang/navigation-2.0.0-tar.gz'
"""
vendor = format_vendor_dir(vendor_dir, package['namespace'])
vendor = format_vendor_dir(vendor_dir, package['user'])
return package_dir(vendor_dir, package) + "-tar.gz"

def format_native_name(namespace, name):
def format_native_name(user, project):
"""
Formates the package to the namespace used in elm native.
Formates the package to the user used in elm native.
>>> format_native_name('elm-lang', 'navigation')
'_elm_lang$navigation'
"""

underscored_namespace = namespace.replace("-", "_")
underscored_name = name.replace("-", "_")
return "_{owner}${repo}".format(owner=underscored_namespace, repo=underscored_name)
underscored_user = user.replace("-", "_")
underscored_project = project.replace("-", "_")
return "_{owner}${repo}".format(owner=underscored_user, repo=underscored_project)


def namespace_from_repo(repository):
def package_name_from_repo(repository):
"""
Namespace and name from repository.
>>> namespace_from_repo('https://github.com/NoRedInk/noredink.git')
User and project from repository.
>>> package_name_from_repo('https://github.com/NoRedInk/noredink.git')
['NoRedInk', 'noredink']
"""

repo_without_domain = repository.split('https://github.com/')[1].split('.git')[0]

(namespace, name) = repo_without_domain.split('/')
return [namespace, name]
(user, project) = repo_without_domain.split('/')
return (user, project)


def get_source_dirs(vendor_dir, package):
Expand Down Expand Up @@ -165,14 +165,14 @@ def munge_names(vendor_dir, repository, packages):
"""
Replaces the namespaced function names in all native code by the namespace from the given elm-package.json.
"""
namespace, name = namespace_from_repo(repository)
user, project = package_name_from_repo(repository)
for package in packages:
native_files = find_all_native_files(package_dir(vendor_dir, package))
for native_file in native_files:
replace_in_file(
native_file,
format_native_name(package['namespace'], package['name']),
format_native_name(namespace, name)
format_native_name(package['user'], package['project']),
format_native_name(user, project)
)


Expand Down Expand Up @@ -216,7 +216,7 @@ def filter_packages(vendor_dir, packages):

def main(native_elm_package, configs, vendor):
raw_json = read_native_elm_package(native_elm_package)
parsed = parse_json(raw_json)
parsed = packages_from_exact_deps(raw_json)
packages = filter_packages(vendor, parsed)
fetch_packages(vendor, packages)
repository = update_elm_package(vendor, configs, packages)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_native_package_install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json
import tarfile
import urllib2

import native_package_install



def test_main(tmpdir, mocker):
native_elm_package = {'elm-lang/core': '1.0.0'}
native_elm_package_path = tmpdir.join('elm-native-package.json')
native_elm_package_path.write(json.dumps(native_elm_package))

fake_native_tarball_path = tmpdir.join('core.tgz')
with tmpdir.as_cwd():
with tarfile.open(str(fake_native_tarball_path), 'w') as f:
fake_elm_package = tmpdir.mkdir('core-1.0.0').join('elm-package.json')
fake_elm_package.write(json.dumps({
'source-directories': ['src', 'src2'],
}))
f.add(str(fake_elm_package.relto(tmpdir)))

elm_package_one_path = tmpdir.join('elm-package-one.json')
elm_package_one_path.write(json.dumps({
'repository': 'https://github.com/NoRedInk/elm-ops-tooling.git',
'source-directories': ['.'],
}))

elm_package_two_path = tmpdir.join('elm-package-two.json')
elm_package_two_path.write(json.dumps({
'repository': 'https://github.com/NoRedInk/elm-ops-tooling-two.git',
'source-directories': ['src'],
}))

vendor = tmpdir.mkdir('vendor')

urlopen = mocker.patch.object(urllib2, 'urlopen')
with open(str(fake_native_tarball_path)) as f:
urlopen.return_value = f

native_package_install.main(
str(native_elm_package_path),
list(map(str, (elm_package_one_path, elm_package_two_path))),
str(vendor))

# mvp is to test that main runs without raising an exception;
# not asserting anything for now

0 comments on commit b9d1532

Please sign in to comment.