Skip to content

Commit

Permalink
Merge pull request #19 from mapbox/pep8isgr8
Browse files Browse the repository at this point in the history
Pep8isgr8
  • Loading branch information
dnomadb authored Sep 17, 2019
2 parents ecb59dd + 03edddc commit 35c5b55
Show file tree
Hide file tree
Showing 18 changed files with 581 additions and 307 deletions.
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
repos:
-
repo: 'https://github.com/ambv/black'
# 18.6b1
rev: stable
hooks:
- id: black
args: ['--safe']
-
repo: 'https://github.com/pre-commit/pre-commit-hooks'
rev: v2.0.0
hooks:
- id: flake8
args: [
# E501 let black handle all line length decisions
# W503 black conflicts with "line break before operator" rule
# E203 black conflicts with "whitespace before ':'" rule
# E722 bare excepts need to be addressed
'--ignore=E501,W503,E203,E722']
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ install:
script:
- python --version
- pytest --version
- pre-commit run --all-files
- pytest --cov --cov-config=.coveragerc
after_script:
- echo "uploading to codecov"
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## 0.2.1 (20190-09-16)
- Reformatting using `black`
- More robust tileset id checking

## 0.2.0 (2019-09-11)

- Add `tilesets list <account>` command
- Add `tilesets list <account>` command

## 0.1.0 (2019-08-01)

Expand Down
44 changes: 43 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
## Welcome

Hi there! Welcome to the tilesets-cli contributing document. Issues, comments, and pull requests are welcome. Please tag @mapsam, @dianeschulze, and @millzpaugh for any questions or reviews.
Hi there! Welcome to the tilesets-cli contributing document. Issues, comments, and pull requests are welcome. Please tag @mapsam, @dianeschulze, and @dnomadb for any questions or reviews.

## Installation
First, clone the repo and `cd` into the folder:
```
$ git clone [email protected]:mapbox/tilesets-cli.git
$ cd tilesets-cli
```
Then, in a virtual environment, install the module with `[test]` extras:
```
pip install -e '.[test]'
```
## Pre-commit hooks
We use [pre-commit hooks](https://pre-commit.com/) to auto-format and validate code before committing. `pre-commit` is included with the `[test]` extras, but you must run:
```
$ pre-commit install
```
within the repo to have the actions specified in `.pre-commit-config.yaml` registered.

After this, when committing, you'll see:
```
git commit -m 'update version'
black....................................................................Passed
Flake8...................................................................Passed
```
If your pre-commit hooks ran successfully. Note that `black` modifies your code, which means that if there is a syntax error you'll first see something like:
```
git commit -m '{message}'
black....................................................................Failed
hookid: black
Files were modified by this hook. Additional output:
reformatted this/file/was/reformatted.py
All done! ✨ 🍰 ✨
1 file reformatted.
Flake8...................................................................Failed
hookid: flake8
this/file/was/reformatted.py:{line}:{character}: {what was incorrect}
```
After which you can add these changes and commit again. Note that failing pre-commit commands mean that the commit has not taken place: you must commit again!

## Release process

Expand Down
51 changes: 30 additions & 21 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,40 @@


# Get the long description from the relevant file
with codecs_open('README.md', encoding='utf-8') as f:
with codecs_open("README.md", encoding="utf-8") as f:
long_description = f.read()


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(name='tilesets-cli',
version='0.2.0',
description=u"CLI for interacting with and preparing data for the Tilesets API",
long_description=long_description,
classifiers=[],
keywords='',
author=u"Mapbox",
author_email='[email protected]',
url='https://github.com/mapbox/tilesets-cli',
license='BSD-2',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
install_requires=["boto3", "click~=7.0", "requests", "jsonschema~=3.0", "jsonseq~=1.0"],
include_package_data=True,
zip_safe=False,
extras_require={
'test': ['pytest>=3.6.0', 'pytest-cov'],
},
entry_points="""

setup(
name="tilesets-cli",
version="0.2.0",
description=u"CLI for interacting with and preparing data for the Tilesets API",
long_description=long_description,
classifiers=[],
keywords="",
author=u"Mapbox",
author_email="[email protected]",
url="https://github.com/mapbox/tilesets-cli",
license="BSD-2",
packages=find_packages(exclude=["ez_setup", "examples", "tests"]),
install_requires=[
"boto3",
"click~=7.0",
"requests",
"jsonschema~=3.0",
"jsonseq~=1.0",
],
include_package_data=True,
zip_safe=False,
extras_require={
"test": ["pytest>=3.6.0", "pytest-cov", "pre-commit", "black", "pep8"]
},
entry_points="""
[console_scripts]
tilesets=tilesets.cli:cli
"""
)
""",
)
114 changes: 82 additions & 32 deletions tests/test_cli_create.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from unittest import mock
import json

from click.testing import CliRunner
import pytest

from tilesets.cli import create


class MockResponse():
class MockResponse:
def __init__(self, mock_text):
self.text = mock_text

def MockResponse(self):
return self

Expand All @@ -18,7 +18,7 @@ def MockResponse(self):
def test_cli_create_missing_recipe():
runner = CliRunner()
# missing --recipe option
result = runner.invoke(create, ['test.id'])
result = runner.invoke(create, ["test.id"])
assert result.exit_code == 2
assert 'Missing option "--recipe"' in result.output

Expand All @@ -27,69 +27,119 @@ def test_cli_create_missing_recipe():
def test_cli_create_missing_name():
runner = CliRunner()
# missing --name option
result = runner.invoke(create, ['test.id', '--recipe', 'tests/fixtures/recipe.json'])
result = runner.invoke(
create, ["test.id", "--recipe", "tests/fixtures/recipe.json"]
)
assert result.exit_code == 2
assert 'Missing option "--name"' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
@mock.patch("requests.post")
def test_cli_create_success(mock_request_post):
runner = CliRunner()
# sends request to proper endpoints
mock_request_post.return_value = MockResponse('{"message":"mock message"}')
result = runner.invoke(create, ['test.id', '--recipe', 'tests/fixtures/recipe.json', '--name', 'test name'])
result = runner.invoke(
create,
["test.id", "--recipe", "tests/fixtures/recipe.json", "--name", "test name"],
)
assert result.exit_code == 0
mock_request_post.assert_called_with('https://api.mapbox.com/tilesets/v1/test.id?access_token=fake-token', json={'name': 'test name', 'description': '', 'recipe': {'minzoom': 0, 'maxzoom': 10, 'layer_name': 'test_layer'}})
mock_request_post.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id?access_token=fake-token",
json={
"name": "test name",
"description": "",
"recipe": {"minzoom": 0, "maxzoom": 10, "layer_name": "test_layer"},
},
)
assert '{\n "message": "mock message"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
@mock.patch("requests.post")
def test_cli_create_success_description(mock_request_post):
runner = CliRunner()
# sends request with "description" included
mock_request_post.return_value = MockResponse('{"message":"mock message with description"}')
result = runner.invoke(create, [
'test.id',
'--recipe',
'tests/fixtures/recipe.json',
'--name',
'test name',
'--description',
'test description']
mock_request_post.return_value = MockResponse(
'{"message":"mock message with description"}'
)
result = runner.invoke(
create,
[
"test.id",
"--recipe",
"tests/fixtures/recipe.json",
"--name",
"test name",
"--description",
"test description",
],
)
assert result.exit_code == 0
mock_request_post.assert_called_with('https://api.mapbox.com/tilesets/v1/test.id?access_token=fake-token', json={'name': 'test name', 'description': 'test description', 'recipe': {'minzoom': 0, 'maxzoom': 10, 'layer_name': 'test_layer'}})
mock_request_post.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id?access_token=fake-token",
json={
"name": "test name",
"description": "test description",
"recipe": {"minzoom": 0, "maxzoom": 10, "layer_name": "test_layer"},
},
)
assert '{\n "message": "mock message with description"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
@mock.patch("requests.post")
def test_cli_create_private_invalid(mock_request_post):
runner = CliRunner()
# sends request with "description" included
mock_request_post.return_value = MockResponse('{"message":"mock message with description"}')
result = runner.invoke(create, [
'test.id',
'--recipe',
'tests/fixtures/recipe.json',
'--name',
'test name',
'--privacy',
'invalid-privacy-value']
mock_request_post.return_value = MockResponse(
'{"message":"mock message with description"}'
)
result = runner.invoke(
create,
[
"test.id",
"--recipe",
"tests/fixtures/recipe.json",
"--name",
"test name",
"--privacy",
"invalid-privacy-value",
],
)
assert result.exit_code == 2
assert 'Invalid value for "--privacy" / "-p": invalid choice: invalid-privacy-value. (choose from public, private)' in result.output
assert (
'Invalid value for "--privacy" / "-p": invalid choice: invalid-privacy-value. (choose from public, private)'
in result.output
)


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
@mock.patch("requests.post")
def test_cli_use_token_flag(mock_request_post):
runner = CliRunner()
mock_request_post.return_value = MockResponse('{"message":"mock message"}')
# Provides the flag --token
result = runner.invoke(create, ['test.id', '--recipe', 'tests/fixtures/recipe.json', '--name', 'test name', '--token', 'flag-token'])
result = runner.invoke(
create,
[
"test.id",
"--recipe",
"tests/fixtures/recipe.json",
"--name",
"test name",
"--token",
"flag-token",
],
)
assert result.exit_code == 0
mock_request_post.assert_called_with('https://api.mapbox.com/tilesets/v1/test.id?access_token=flag-token', json={'name': 'test name', 'description': '', 'recipe': {'minzoom': 0, 'maxzoom': 10, 'layer_name': 'test_layer'}})
mock_request_post.assert_called_with(
"https://api.mapbox.com/tilesets/v1/test.id?access_token=flag-token",
json={
"name": "test name",
"description": "",
"recipe": {"minzoom": 0, "maxzoom": 10, "layer_name": "test_layer"},
},
)
assert '{\n "message": "mock message"\n}\n' in result.output
Loading

0 comments on commit 35c5b55

Please sign in to comment.