diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7b3f9b0..a41d88b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -29,12 +29,13 @@ language_version: python3.5 - id: fix-encoding-pragma language_version: python3.5 + args: ['--remove'] - repo: https://github.com/asottile/reorder_python_imports sha: v0.3.1 hooks: - id: reorder-python-imports language_version: python3.5 args: [ - '--add-import', 'from __future__ import absolute_import', - '--add-import', 'from __future__ import unicode_literals', + '--remove-import', 'from __future__ import absolute_import', + '--remove-import', 'from __future__ import unicode_literals', ] diff --git a/lazy_build/cache.py b/lazy_build/cache.py index 01c84ce..27124d9 100644 --- a/lazy_build/cache.py +++ b/lazy_build/cache.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import -from __future__ import unicode_literals - import collections import os import tempfile diff --git a/lazy_build/cli.py b/lazy_build/cli.py index 3de773a..8148c3c 100644 --- a/lazy_build/cli.py +++ b/lazy_build/cli.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import -from __future__ import unicode_literals - import argparse import json import os @@ -116,7 +112,7 @@ def main(argv=None): # TODO: is there a way we can get argparse to do this for us? if args.command[0] != '--': raise ValueError( - 'You must separate the command from the other arguments with a --!', + 'You must separate the command from the other arguments with a --!', # noqa ) del args.command[0] diff --git a/lazy_build/color.py b/lazy_build/color.py index b28b4fd..7ae8a81 100644 --- a/lazy_build/color.py +++ b/lazy_build/color.py @@ -1,8 +1,4 @@ -# -*- coding: utf-8 -*- """Stolen from ocflib.misc.shell""" -from __future__ import absolute_import -from __future__ import unicode_literals - import sys # terminal text color wrappers; diff --git a/lazy_build/config.py b/lazy_build/config.py index ec1adcb..c3454e1 100644 --- a/lazy_build/config.py +++ b/lazy_build/config.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import -from __future__ import unicode_literals - import collections from lazy_build import cache diff --git a/lazy_build/context.py b/lazy_build/context.py index cb05219..eadebb9 100644 --- a/lazy_build/context.py +++ b/lazy_build/context.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import -from __future__ import unicode_literals - import collections import fnmatch import hashlib @@ -90,8 +86,7 @@ def build_context(conf, command): if os.path.isdir(path) and not os.path.islink(path): for child in os.listdir(path): child = os.path.relpath(os.path.join(path, child)) - if child not in explored: - fringe.add(child) + fringe.add(child) else: ctx[path] = FileContext.from_path(path) diff --git a/setup.py b/setup.py index 7b85f46..faa6c80 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import -from __future__ import unicode_literals - from setuptools import find_packages from setuptools import setup @@ -10,8 +6,6 @@ name='lazy-build', version='0.0.0.dev1', classifiers=[ - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.5', ], diff --git a/tests/color_test.py b/tests/color_test.py new file mode 100644 index 0000000..d2d3254 --- /dev/null +++ b/tests/color_test.py @@ -0,0 +1,41 @@ +import sys +from unittest import mock + +import pytest + +import lazy_build.color + + +COLORS = ( + 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', +) + + +@pytest.mark.parametrize('color', COLORS) +def test_color_wrappers_with_tty(color): + with mock.patch.object(sys.stdout, 'isatty', return_value=True): + fore = getattr(lazy_build.color, color) + assert fore('hi') == fore('hi', tty_only=True) + assert fore('hi') == ( + lazy_build.color.FG_CODES[color] + + 'hi' + + lazy_build.color.FG_CODES['reset'] + ) + + bg = getattr(lazy_build.color, 'bg_' + color) + assert bg('hi') == bg('hi', tty_only=True) + assert bg('hi') == ( + lazy_build.color.BG_CODES[color] + + 'hi' + + lazy_build.color.BG_CODES['reset'] + ) + + +@pytest.mark.parametrize('color', COLORS) +def test_color_wrappers_without_tty(color): + with mock.patch('sys.stdout.isatty', return_value=False): + fore = getattr(lazy_build.color, color) + assert fore('hi') == 'hi' + + bg = getattr(lazy_build.color, 'bg_' + color) + assert bg('hi') == 'hi' diff --git a/tests/config_test.py b/tests/config_test.py new file mode 100644 index 0000000..9496795 --- /dev/null +++ b/tests/config_test.py @@ -0,0 +1,15 @@ +from unittest import mock + +from lazy_build import config + + +def test_from_args(): + conf = config.Config.from_args(mock.Mock( + context=['requirements.txt', 'setup.py'], + ignore=['*.py[co]', '*.swp'], + output=['venv', 'node_modules'], + )) + assert conf.context == {'requirements.txt', 'setup.py'} + assert conf.ignore == {'*.py[co]', '*.swp'} + assert conf.output == {'venv', 'node_modules'} + # TODO: make some assertions about the backend diff --git a/tests/context_test.py b/tests/context_test.py index f49bb67..e40f03c 100644 --- a/tests/context_test.py +++ b/tests/context_test.py @@ -1,6 +1,7 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import -from __future__ import unicode_literals +import io +import json +import os +import tarfile import pytest @@ -35,7 +36,7 @@ def test_build_context_simple(tmpdir): conf = config.Config( context={tmpdir.strpath, tmpdir.join('a').strpath}, ignore={'d'}, - output=('output',), + output=None, backend=None, ) tmpdir.join('a').write(b'foo') @@ -44,7 +45,9 @@ def test_build_context_simple(tmpdir): tmpdir.join('d').mkdir() tmpdir.join('d/e').write(b'baz') tmpdir.join('f').mksymlinkto('/etc/passwd') - assert context.build_context(conf, 'command') == context.BuildContext( + + ctx = context.build_context(conf, 'command') + assert ctx == context.BuildContext( files={ 'a': context.FileContext('file', context.hash(b'foo')), 'b/c': context.FileContext('file', context.hash(b'bar')), @@ -52,3 +55,58 @@ def test_build_context_simple(tmpdir): }, command='command', ) + assert ctx.hash == context.hash( + json.dumps(('command', ctx.files), sort_keys=True).encode('utf8'), + ) + + +def test_package_artifact(tmpdir): + tmpdir.chdir() + tmpdir.join('a').ensure() + tmpdir.join('b').mkdir() + tmpdir.join('b/c').ensure() + tmpdir.join('c').ensure() + + tmp = context.package_artifact(config.Config( + context=None, + ignore=None, + output=('b', 'c'), + backend=None, + )) + try: + with tarfile.TarFile(tmp) as tf: + members = {member.name for member in tf.getmembers()} + finally: + os.remove(tmp) + + assert members == {'b/c', 'c'} + + +def test_extract_artifact(tmpdir): + tmpdir.chdir() + tmpdir.join('my.txt').write('this is not the text you are looking for') + tmpdir.join('a').mkdir() + tmpdir.join('a/b').mkdir() + tmpdir.join('a/sup').ensure() + tmpdir.join('a/b/sup').ensure() + + tar = tmpdir.join('my.tar').strpath + with tarfile.TarFile(tar, 'w') as tf: + for path in ('my.txt', 'hello/there.txt', 'a/b/c/d.txt'): + ti = tarfile.TarInfo(path) + ti.size = 6 + tf.addfile(ti, fileobj=io.BytesIO(b'wuddup')) + + context.extract_artifact(config.Config( + context=None, + ignore=None, + output=('my.txt', 'hello', 'a/b'), + backend=None, + ), tar) + + assert tmpdir.join('my.txt').read() == 'wuddup' + assert tmpdir.join('hello/there.txt').isfile() + assert tmpdir.join('a/b/c/d.txt').isfile() + + assert tmpdir.join('a/sup').isfile() + assert not tmpdir.join('a/b/sup').isfile() diff --git a/tox.ini b/tox.ini index 1c83eba..7e6586c 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27,py35 +envlist = py35 [testenv] deps = -rrequirements-dev.txt @@ -22,7 +22,7 @@ deps = commands = [flake8] -max-line-length = 119 +max-line-length = 79 [pep8] ignore = E265,E309,E501