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

Commit

Permalink
test: coverage, ci
Browse files Browse the repository at this point in the history
test: more cov

chore: disable consider-using-with
  • Loading branch information
mcataford committed Mar 18, 2022
1 parent 5cfca91 commit be2afa0
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 0 deletions.
133 changes: 133 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
name: CICD

on:
push:
branches: main
pull_request:

jobs:
setup:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- uses: abatilo/actions-poetry@v2.0.0
with:
poetry-version: 1.1.7
- uses: actions/cache@v2
id: dep-cache
with:
key: ${{ runner.os }}-py${{ matrix.python }}-${{ hashFiles('poetry.lock') }}
path: |
/home/runner/.cache/pypoetry/virtualenvs
- name: Setup dependencies
if: steps.dep-cache.outputs.cache-hit != 'true'
run: |
poetry install
test:
runs-on: ubuntu-latest
needs: setup
strategy:
matrix:
python: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- uses: abatilo/actions-poetry@v2.0.0
with:
poetry-version: 1.1.7
- uses: actions/cache@v2
id: dep-cache
with:
key: ${{ runner.os }}-py${{matrix.python }}-${{ hashFiles('poetry.lock') }}
path: |
/home/runner/.cache/pypoetry/virtualenvs
- name: Setup dependencies
if: steps.dep-cache.outputs.cache-hit != 'true'
run: |
poetry install
- name: Tests
run: |
poetry run pytest --cov
- uses: actions/upload-artifact@v2
with:
name: coverage-report
path: ./coverage.xml

coverage:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
with:
name: coverage-report
path: ./
- name: Upload coverage
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
fail_ci_if_error: true
verbose: true
lint:
runs-on: ubuntu-latest
needs: setup
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.9
- uses: abatilo/actions-poetry@v2.0.0
with:
poetry-version: 1.1.7
- uses: actions/cache@v2
id: dep-cache
with:
key: ${{ runner.os }}-py3.9-${{ hashFiles('poetry.lock') }}
path: |
/home/runner/.cache/pypoetry/virtualenvs
- name: Setup dependencies
if: steps.dep-cache.outputs.cache-hit != 'true'
run: |
poetry install
- name: Lint and format
run: |
poetry run pylint **/*.py
poetry run black . --check
build:
runs-on: ubuntu-latest
needs: setup
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.9
- uses: abatilo/actions-poetry@v2.0.0
with:
poetry-version: 1.1.7
- uses: actions/cache@v2
id: dep-cache
with:
key: ${{ runner.os }}-py3.9-${{ hashFiles('poetry.lock') }}
path: |
/home/runner/.cache/pypoetry/virtualenvs
- name: Setup dependencies
if: steps.dep-cache.outputs.cache-hit != 'true'
run: |
poetry install
- name: Lint and format
run: |
poetry build
- uses: actions/upload-artifact@v2
with:
name: current-build
path: dist/*

7 changes: 7 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[MESSAGES CONTROL]
disable=consider-using-f-string,
broad-except,
invalid-name,
missing-module-docstring,
missing-function-docstring,
consider-using-with
Empty file added tests/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions tests/__snapshots__/test_integration.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# name: test_sends_request_to_slack_api_on_success
<class 'dict'> {
'profile': <class 'list'> [
"{'status_text': 'test', 'status_emoji': '', 'status_expiration': 0}",
],
}
---
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture
def slack_api_token():
return "mock-slack-token"
59 changes: 59 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import unittest.mock
import urllib.parse
import os
import sys
import typing

import slack_status_cli.main


class MockResponse(typing.NamedTuple):
"""
Stand-in for http.client.HTTPResponse.
"""

status: int
response_text: str

def read(self):
return self.response_text


def test_errors_if_no_slack_token_provided(monkeypatch):
monkeypatch.setattr(sys, "argv", ["slack-status-cli", "set", "--text", "test"])

with unittest.mock.patch("sys.exit", autospec=True) as mock_exit:
slack_status_cli.main.run()

mock_exit.assert_called_with(1)


def test_sends_request_to_slack_api_on_success(
slack_api_token, monkeypatch, snapshot, tmp_path
):
env = os.environ.copy()
env["SLACK_TOKEN"] = slack_api_token

mock_response = MockResponse(status=200, response_text='{ "ok": true }')

monkeypatch.setenv("SLACK_TOKEN", slack_api_token)
monkeypatch.setattr(sys, "argv", ["slack-status-cli", "set", "--text", "test"])

with unittest.mock.patch(
"urllib.request.urlopen",
autospec=True,
return_value=mock_response,
) as mock_request, unittest.mock.patch(
"pathlib.Path.home", autospec=True, return_value=tmp_path
):
slack_status_cli.main.run()

request = mock_request.call_args_list[0][0][0]

assert request.get_full_url() == "https://slack.com/api/users.profile.set"
assert request.get_method() == "POST"
assert request.get_header("Authorization") == "Bearer %s" % slack_api_token

request_body = urllib.parse.parse_qs(request.data.decode())

assert request_body == snapshot

0 comments on commit be2afa0

Please sign in to comment.