Skip to content

Commit ddf7039

Browse files
authored
Able to run as main script. Add integration test (#140)
1 parent ed61a07 commit ddf7039

6 files changed

Lines changed: 115 additions & 56 deletions

File tree

.github/workflows/ci.yml

Lines changed: 91 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,49 @@ on:
44
push:
55
branches: [ master ]
66
tags:
7-
- "*"
7+
- "*"
88
paths-ignore:
9-
- 'README.md'
10-
- 'LICENSE'
11-
- '.gitignore'
12-
- 'codecov.yml'
13-
- '.python-version'
14-
- 'poetry.toml'
9+
- 'README.md'
10+
- 'LICENSE'
11+
- '.gitignore'
12+
- 'codecov.yml'
13+
- '.python-version'
14+
- 'poetry.toml'
1515
pull_request:
1616
branches: [ master ]
1717
paths-ignore:
18-
- 'README.md'
19-
- 'LICENSE'
20-
- '.gitignore'
21-
- 'codecov.yml'
22-
- '.python-version'
23-
- 'poetry.toml'
18+
- 'README.md'
19+
- 'LICENSE'
20+
- '.gitignore'
21+
- 'codecov.yml'
22+
- '.python-version'
23+
- 'poetry.toml'
2424
workflow_dispatch:
2525
schedule:
26-
- cron: '30 5 * * 1'
26+
- cron: '30 5 * * 1'
2727

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

3232
jobs:
33-
test:
34-
name: pre-commit and test
33+
pre-commit:
34+
runs-on: ubuntu-22.04
35+
steps:
36+
- uses: actions/checkout@master
37+
- uses: actions/setup-python@v2
38+
with:
39+
python-version: 3.7
40+
- name: Install pre-commit
41+
run: |
42+
pip install pre-commit==2.9.2
43+
- name: Run pre-commit
44+
run: |
45+
pre-commit install
46+
pre-commit run --all-files
47+
unit-test:
3548
runs-on: ${{ matrix.os }}
49+
needs: pre-commit
3650
strategy:
3751
matrix:
3852
python-version: [3.7, 3.8]
@@ -42,12 +56,10 @@ jobs:
4256
shell: bash
4357
steps:
4458
- uses: actions/checkout@master
45-
- name: Install Python
46-
uses: actions/setup-python@v2
59+
- uses: actions/setup-python@v2
4760
with:
4861
python-version: ${{ matrix.python-version }}
49-
- name: Install latest Poetry
50-
uses: snok/install-poetry@v1.3.3
62+
- uses: snok/install-poetry@v1.3.3
5163
with:
5264
virtualenvs-create: true
5365
virtualenvs-in-project: true
@@ -58,20 +70,65 @@ jobs:
5870
path: ~/.cache
5971
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
6072
- name: Install dependencies
61-
run: poetry install --no-interaction --no-root --with dev,pdf
73+
run: |
74+
poetry install --no-interaction --no-root --with dev,pdf
6275
- name: Install library
6376
run: poetry install --no-interaction
6477
- run: |
6578
source $VENV
6679
pytest --version
67-
- name: Run pre-commit
68-
run: |
69-
poetry run pre-commit install
70-
poetry run pre-commit run --all-files
71-
- name: Run tests
80+
- name: Run unit tests and Codecov
7281
run: poetry run pytest -n auto --cov=./ --cov-report=xml
7382
- name: Upload coverage to Codecov
7483
uses: codecov/codecov-action@v1
84+
build:
85+
runs-on: ubuntu-22.04
86+
needs: unit-test
87+
steps:
88+
- uses: actions/checkout@master
89+
- uses: actions/setup-python@v2
90+
with:
91+
python-version: 3.7
92+
- uses: snok/install-poetry@v1.3.3
93+
with:
94+
virtualenvs-create: true
95+
virtualenvs-in-project: true
96+
- name: Build wheels
97+
run: |
98+
poetry build
99+
- name: Upload wheel and tar ball
100+
uses: actions/upload-artifact@v3
101+
with:
102+
name: dist
103+
path: ./dist/*
104+
integration-test:
105+
runs-on: ${{ matrix.os }}
106+
needs: build
107+
strategy:
108+
matrix:
109+
python-version: [3.7, 3.8]
110+
os: [ubuntu-22.04, windows-2022, macos-11]
111+
defaults:
112+
run:
113+
shell: bash
114+
steps:
115+
- uses: actions/setup-python@v2
116+
with:
117+
python-version: ${{ matrix.python-version }}
118+
- name: Download wheel and tar ball built previously
119+
uses: actions/download-artifact@master
120+
with:
121+
name: dist
122+
path: ./dist/
123+
- name: Install wheel
124+
run: |
125+
python -m pip install dist/cmdict-*py3-none-any.whl
126+
- name: Run module as script
127+
run: |
128+
python -m cmdict --help
129+
- name: Run module as executable
130+
run: |
131+
cmdict --help
75132
publish:
76133
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') && github.repository == 'pastydev/cmdict'
77134
environment:
@@ -80,23 +137,12 @@ jobs:
80137
permissions:
81138
id-token: write
82139
runs-on: ubuntu-22.04
83-
needs: test
140+
needs: integration-test
84141
steps:
85-
- uses: actions/checkout@master
86-
- name: Install dependecies
87-
uses: actions/setup-python@v2
88-
with:
89-
python-version: 3.7
90-
cache: pip
91-
- name: Install latest Poetry
92-
uses: snok/install-poetry@v1.3.3
93-
- name: Build wheels
94-
run: |
95-
poetry build
96-
- name: Upload wheel and tar ball as artifacts
97-
uses: actions/upload-artifact@v3
98-
with:
99-
name: dist
100-
path: ./dist/*
101-
- name: Publish to PyPI
102-
uses: pypa/gh-action-pypi-publish@release/v1
142+
- name: Download wheel and tar ball built previously
143+
uses: actions/download-artifact@master
144+
with:
145+
name: dist
146+
path: ./dist/
147+
- name: Publish to PyPI
148+
uses: pypa/gh-action-pypi-publish@release/v1

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ colorama = ">=0.4.3"
2020
requests = ">=2.24.0"
2121
tqdm = ">=4.48.0"
2222
pyyaml = ">=5.3.1"
23+
virtualenv = ">=20.10.0"
2324

2425
[tool.poetry.group.pdf]
2526
optional = true
@@ -31,11 +32,16 @@ PyMuPDF = "1.22.3"
3132
optional = true
3233

3334
[tool.poetry.group.dev.dependencies]
34-
pre-commit = "^2.9.2"
3535
pytest = "^7.0.0"
3636
pytest-cov = "^2.9.0"
3737
pytest-xdist = "^1.32.0"
3838

39+
[tool.poetry.group.check]
40+
optional = true
41+
42+
[tool.poetry.group.check.dependencies]
43+
pre-commit = "2.21.0"
44+
3945
[tool.black]
4046
line-length = 79
4147
include = '\.pyi?$'

src/cmdict/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Main cmdict module."""
1+
"""Init, when module is imported."""
22
from cmdict import run_script
33

44
__all__ = ["run_script"]

src/cmdict/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Execute command line interface, when module is run as a script."""
2+
from cmdict.run_script import cli
3+
4+
5+
cli()

src/cmdict/history.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
"""Functions for recording and reading user history."""
1+
"""Function to record and read user history."""
22
import os
33
import pathlib
4+
from typing import Optional
45

56
from loguru import logger
67
import yaml
@@ -10,7 +11,7 @@
1011
)
1112

1213

13-
def record(word, path=_path):
14+
def record(word: str, path: Optional[str] = _path):
1415
"""Append queried word in a yaml file in lowercase format.
1516
1617
If the path is directed to yaml file, there will be three
@@ -19,28 +20,29 @@ def record(word, path=_path):
1920
file does exist, we read its content first.
2021
2122
Args:
22-
word (str): to be appended at the end of the yaml file.
23-
path (str): to the yaml file.
23+
word: to be appended at the end of the yaml file.
24+
path: to the yaml file.
2425
2526
Raises:
2627
ValueError: when the path is not to a yaml file.
2728
"""
2829
if not path.endswith(".yaml"):
29-
raise ValueError(f"{path} is not a path to yaml file.")
30+
raise ValueError(f'"{path}" is not a yaml file.')
3031

3132
if not pathlib.Path(path).is_file():
3233
with open(path, "w") as f:
3334
yaml.safe_dump([word], f)
3435
else:
36+
history: list[str] = None
3537
with open(path, "r") as f:
3638
try:
37-
hist = yaml.safe_load(f)
39+
history = yaml.safe_load(f)
3840
except yaml.YAMLError as exc:
3941
logger.exception(exc)
4042

41-
if hist is None:
43+
if history is None:
4244
with open(path, "w") as f:
4345
yaml.safe_dump([word], f)
44-
elif word not in hist:
46+
elif word not in history:
4547
with open(path, "a") as f:
4648
yaml.safe_dump([word], f)

0 commit comments

Comments
 (0)