Skip to content

Commit

Permalink
Able to run as main script. Add integration test (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
edxu96 authored Jun 8, 2023
1 parent ed61a07 commit ddf7039
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 56 deletions.
136 changes: 91 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,49 @@ on:
push:
branches: [ master ]
tags:
- "*"
- "*"
paths-ignore:
- 'README.md'
- 'LICENSE'
- '.gitignore'
- 'codecov.yml'
- '.python-version'
- 'poetry.toml'
- 'README.md'
- 'LICENSE'
- '.gitignore'
- 'codecov.yml'
- '.python-version'
- 'poetry.toml'
pull_request:
branches: [ master ]
paths-ignore:
- 'README.md'
- 'LICENSE'
- '.gitignore'
- 'codecov.yml'
- '.python-version'
- 'poetry.toml'
- 'README.md'
- 'LICENSE'
- '.gitignore'
- 'codecov.yml'
- '.python-version'
- 'poetry.toml'
workflow_dispatch:
schedule:
- cron: '30 5 * * 1'
- cron: '30 5 * * 1'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
test:
name: pre-commit and test
pre-commit:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@master
- uses: actions/setup-python@v2
with:
python-version: 3.7
- name: Install pre-commit
run: |
pip install pre-commit==2.9.2
- name: Run pre-commit
run: |
pre-commit install
pre-commit run --all-files
unit-test:
runs-on: ${{ matrix.os }}
needs: pre-commit
strategy:
matrix:
python-version: [3.7, 3.8]
Expand All @@ -42,12 +56,10 @@ jobs:
shell: bash
steps:
- uses: actions/checkout@master
- name: Install Python
uses: actions/setup-python@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install latest Poetry
uses: snok/install-poetry@v1.3.3
- uses: snok/install-poetry@v1.3.3
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand All @@ -58,20 +70,65 @@ jobs:
path: ~/.cache
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install --no-interaction --no-root --with dev,pdf
run: |
poetry install --no-interaction --no-root --with dev,pdf
- name: Install library
run: poetry install --no-interaction
- run: |
source $VENV
pytest --version
- name: Run pre-commit
run: |
poetry run pre-commit install
poetry run pre-commit run --all-files
- name: Run tests
- name: Run unit tests and Codecov
run: poetry run pytest -n auto --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
build:
runs-on: ubuntu-22.04
needs: unit-test
steps:
- uses: actions/checkout@master
- uses: actions/setup-python@v2
with:
python-version: 3.7
- uses: snok/install-poetry@v1.3.3
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Build wheels
run: |
poetry build
- name: Upload wheel and tar ball
uses: actions/upload-artifact@v3
with:
name: dist
path: ./dist/*
integration-test:
runs-on: ${{ matrix.os }}
needs: build
strategy:
matrix:
python-version: [3.7, 3.8]
os: [ubuntu-22.04, windows-2022, macos-11]
defaults:
run:
shell: bash
steps:
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Download wheel and tar ball built previously
uses: actions/download-artifact@master
with:
name: dist
path: ./dist/
- name: Install wheel
run: |
python -m pip install dist/cmdict-*py3-none-any.whl
- name: Run module as script
run: |
python -m cmdict --help
- name: Run module as executable
run: |
cmdict --help
publish:
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') && github.repository == 'pastydev/cmdict'
environment:
Expand All @@ -80,23 +137,12 @@ jobs:
permissions:
id-token: write
runs-on: ubuntu-22.04
needs: test
needs: integration-test
steps:
- uses: actions/checkout@master
- name: Install dependecies
uses: actions/setup-python@v2
with:
python-version: 3.7
cache: pip
- name: Install latest Poetry
uses: snok/install-poetry@v1.3.3
- name: Build wheels
run: |
poetry build
- name: Upload wheel and tar ball as artifacts
uses: actions/upload-artifact@v3
with:
name: dist
path: ./dist/*
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
- name: Download wheel and tar ball built previously
uses: actions/download-artifact@master
with:
name: dist
path: ./dist/
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ colorama = ">=0.4.3"
requests = ">=2.24.0"
tqdm = ">=4.48.0"
pyyaml = ">=5.3.1"
virtualenv = ">=20.10.0"

[tool.poetry.group.pdf]
optional = true
Expand All @@ -31,11 +32,16 @@ PyMuPDF = "1.22.3"
optional = true

[tool.poetry.group.dev.dependencies]
pre-commit = "^2.9.2"
pytest = "^7.0.0"
pytest-cov = "^2.9.0"
pytest-xdist = "^1.32.0"

[tool.poetry.group.check]
optional = true

[tool.poetry.group.check.dependencies]
pre-commit = "2.21.0"

[tool.black]
line-length = 79
include = '\.pyi?$'
Expand Down
2 changes: 1 addition & 1 deletion src/cmdict/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Main cmdict module."""
"""Init, when module is imported."""
from cmdict import run_script

__all__ = ["run_script"]
5 changes: 5 additions & 0 deletions src/cmdict/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Execute command line interface, when module is run as a script."""
from cmdict.run_script import cli


cli()
18 changes: 10 additions & 8 deletions src/cmdict/history.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Functions for recording and reading user history."""
"""Function to record and read user history."""
import os
import pathlib
from typing import Optional

from loguru import logger
import yaml
Expand All @@ -10,7 +11,7 @@
)


def record(word, path=_path):
def record(word: str, path: Optional[str] = _path):
"""Append queried word in a yaml file in lowercase format.
If the path is directed to yaml file, there will be three
Expand All @@ -19,28 +20,29 @@ def record(word, path=_path):
file does exist, we read its content first.
Args:
word (str): to be appended at the end of the yaml file.
path (str): to the yaml file.
word: to be appended at the end of the yaml file.
path: to the yaml file.
Raises:
ValueError: when the path is not to a yaml file.
"""
if not path.endswith(".yaml"):
raise ValueError(f"{path} is not a path to yaml file.")
raise ValueError(f'"{path}" is not a yaml file.')

if not pathlib.Path(path).is_file():
with open(path, "w") as f:
yaml.safe_dump([word], f)
else:
history: list[str] = None
with open(path, "r") as f:
try:
hist = yaml.safe_load(f)
history = yaml.safe_load(f)
except yaml.YAMLError as exc:
logger.exception(exc)

if hist is None:
if history is None:
with open(path, "w") as f:
yaml.safe_dump([word], f)
elif word not in hist:
elif word not in history:
with open(path, "a") as f:
yaml.safe_dump([word], f)

0 comments on commit ddf7039

Please sign in to comment.