Skip to content

Commit

Permalink
Add tests (#7)
Browse files Browse the repository at this point in the history
* add tests

* undo change

* Correct requirements and bump version

* Bump version

* fix logging and improve test suite

* lock deps

* test conf module

* load conf test

* all tests done

* simplify test since I cant figure out CI
  • Loading branch information
Korijn authored Jul 26, 2023
1 parent 70ab6d5 commit c24649a
Show file tree
Hide file tree
Showing 13 changed files with 593 additions and 30 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,25 @@ jobs:
poetry run ruff keycmd
poetry run black --check keycmd
test:
name: Test
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install poetry
run: pip install "poetry==1.4.2"
- name: Install dependencies
run: poetry install
- name: Test
run: |
poetry run pytest -v tests
build:
name: Build and test wheel
name: Build and check wheel
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -59,7 +76,7 @@ jobs:
publish:
name: Publish release to Github and Pypi
runs-on: ubuntu-latest
needs: [lint, build]
needs: [lint, test, build]
if: success() && startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion keycmd/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.0"
__version__ = "0.3.0"
4 changes: 2 additions & 2 deletions keycmd/__main__.py → keycmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
cli.add_argument("command", nargs="*", help="command to run")


def main():
def main(args=None):
"""CLI entrypoint"""
args = cli.parse_args()
args = cli.parse_args(args=args)

if args.verbose:
set_verbose()
Expand Down
13 changes: 10 additions & 3 deletions keycmd/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from .logs import vlog


# exposed for testing
USERPROFILE = "~"


def load_toml(path):
"""Load a toml file"""
with path.open("rb") as fh:
Expand All @@ -23,7 +27,7 @@ def load_pyproj(path):

def defaults():
"""Generate the default config"""
return {}
return {"keys": {}}


def merge_conf(a, b):
Expand Down Expand Up @@ -53,14 +57,14 @@ def load_conf():
cwd = Path.cwd()

# ~/.keycmd
fpath = Path.home() / ".keycmd"
fpath = (Path(USERPROFILE).expanduser() / ".keycmd").resolve()
if fpath.is_file():
vlog(f"loading config file {fpath}")
conf = merge_conf(conf, load_toml(fpath))

# pyproject.toml
cur = cwd
while cur != cur.anchor:
while True:
pyproj = cur / "pyproject.toml"
if pyproj.is_file():
vlog(f"loading config file {pyproj}")
Expand All @@ -69,6 +73,9 @@ def load_conf():
# stop at the boundary of git repositories
if (cur / ".git").is_dir():
break
# stop if we can't go up anymore
if cur.parent == cur:
break
cur = cur.parent

# ./.keycmd
Expand Down
10 changes: 8 additions & 2 deletions keycmd/creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import keyring

from .logs import vlog
from .logs import error, vlog


def b64(value):
Expand All @@ -16,12 +16,18 @@ def get_env(conf):
env = environ.copy()
for key, src in conf["keys"].items():
password = keyring.get_password(src["credential"], src["username"])
if password is None:
error(
f"MISSING credential {src['credential']}"
f" with user {src['username']} "
f" as it does not exist"
)
if src.get("b64"):
password = b64(password)
env[key] = password
vlog(
f"exposing credential {src['credential']}"
f" belonging to user {src['username']}"
f" with user {src['username']} "
f" as environment variable {key} (b64: {src.get('b64', False)})"
)
return env
18 changes: 11 additions & 7 deletions keycmd/logs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from sys import exit
import sys


_verbose = False


def set_verbose():
def set_verbose(verbose=True):
global _verbose
_verbose = True
_verbose = verbose


def log(msg):
print(f"keycmd: {msg}")
def log(msg, err=False):
msg = f"keycmd: {msg}"
if err:
print(msg, file=sys.stderr)
else:
print(msg)


def vlog(msg):
Expand All @@ -19,8 +23,8 @@ def vlog(msg):


def error(msg):
log(f"error: {msg}")
exit(1)
log(f"error: {msg}", err=True)
sys.exit(1)


def vwarn(msg):
Expand Down
84 changes: 75 additions & 9 deletions poetry.lock

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

9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
[tool.poetry]
name = "keycmd"
version = "0.2.0"
version = "0.3.0"
description = ""
authors = ["Korijn van Golen <korijn.vangolen@zimmerbiomet.com>"]
license = "MIT"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.9"
keyring = "~24.2.0"
python = ">=3.9"
keyring = "^24.2.0"
tomli = "^2.0.1"
shellingham = "^1.5.0.post1"

[tool.poetry.scripts]
keycmd = 'keycmd.__main__:main'
keycmd = 'keycmd.cli:main'

[tool.poetry.group.dev.dependencies]
ruff = "^0.0.278"
black = "^23.7.0"
twine = "^4.0.2"
pytest = "^7.4.0"

[build-system]
requires = ["poetry-core"]
Expand Down
Loading

0 comments on commit c24649a

Please sign in to comment.