Skip to content

Commit

Permalink
fix: global interpreter was used in subprocess instead of venv
Browse files Browse the repository at this point in the history
  • Loading branch information
BartSte committed Apr 8, 2024
1 parent 44fda4e commit 37306c8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
17 changes: 4 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install dependencies
- name: Run tests
run: |
sudo apt-get update
sudo apt-get install -y python3 python3-venv python3-pip
python3 -m pip install --upgrade pip
python3 -m venv .venv
source .venv/bin/activate
pip install ".[test]"
- name: Run Pytest
shell: 'script -q -e -c "bash {0}"' # work around tty issues
env:
TERM: linux # fix tput for tty issue work around
run: |
source .venv/bin/activate
python3 -m pytest
python3 -m pip install ".[test]"
python3 -m pytest --log-level=DEBUG
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"vdirsyncer",
"orgparse"
]
version = "0.0.0"
version = "0.0.1"

[project.optional-dependencies]
debug = ["ipdb", "ipython"]
Expand Down
3 changes: 2 additions & 1 deletion src/khalorg/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def list_command(
calendar: str,
khalorg_format: str = get_khalorg_format(),
khalorg_format: str | None = None,
start: str = 'today',
stop: str = '1d',
**_) -> str:
Expand All @@ -31,6 +31,7 @@ def list_command(
stdout of the `khal list` command after post processing
"""
khalorg_format = khalorg_format or get_khalorg_format()
args: KhalArgs = KhalArgs()
args['-a'] = calendar
args['-f'] = get_khal_format()
Expand Down
4 changes: 2 additions & 2 deletions src/khalorg/khal/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def __init__(self, name: str):
"""
path_config: Union[str, None] = find_configuration_file()

new_item_args: list = ['khal', 'new']
list_args: list = ['khal', 'list', '-df', '']
new_item_args: list = ["python3", "-m", "khal", "new"]
list_args: list = ["python3", "-m", "khal", "list", "-df", ""]

self._collection: CalendarCollection
self._new_item: Callable = subprocess_callback(new_item_args)
Expand Down
20 changes: 15 additions & 5 deletions src/khalorg/khal/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import sys
from datetime import date, datetime
from subprocess import STDOUT, CalledProcessError, check_output
from typing import Callable
Expand Down Expand Up @@ -35,11 +36,18 @@ def is_future(timestamp: datetime | date) -> bool:
True if the `timestamp` is in the future
"""
logging.debug(
"Check if timestamp %s (type: %s) is in the future",
timestamp,
type(timestamp),
)
if isinstance(timestamp, datetime):
now: datetime = datetime.now(timestamp.tzinfo)
return timestamp >= now
now = datetime.now(timestamp.tzinfo)
else:
return timestamp >= datetime.now().date()
now = datetime.now().date()

logging.debug("Now is %s", now)
return timestamp >= now


def subprocess_callback(cmd: list) -> Callable:
Expand All @@ -56,6 +64,7 @@ def subprocess_callback(cmd: list) -> Callable:
callback function
"""

def callback(args: list) -> str:
return try_check_output([*cmd, *args]).decode()

Expand All @@ -64,12 +73,13 @@ def callback(args: list) -> str:

def try_check_output(args: list) -> bytes:
try:
return check_output(args, stderr=STDOUT)
return check_output(args, stderr=STDOUT, executable=sys.executable)
except CalledProcessError as error:
error_message: str = (
f"The following arguments were sent to khal:\n\n{' '.join(args)}"
"\n\nNext, the following error was received from khal:\n\n"
f"{error.output.decode()}\n\n")
f"{error.output.decode()}\n\n"
)
logging.critical(error_message)
raise Exception(error_message) from error

Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def get_start_end(delta: timedelta = timedelta(hours=1),
"""
# must be in the future
start: datetime = datetime.now() + timedelta(minutes=1)
start: datetime = datetime.now() + timedelta(minutes=2)
end: datetime = start + delta

if all_day:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date, datetime, timedelta
import logging
from os.path import join
from tests import static
from tests.helpers import (
Expand Down Expand Up @@ -84,8 +85,12 @@ def _list_test(runner, expected: OrgAgendaItem):
expected.properties['UID'] = actual.properties['UID']
expected.properties['CALENDAR'] = actual.properties['CALENDAR']
expected.properties['RRULE'] = actual.properties['RRULE']
assert str(expected) == str(actual), f'Org item: {expected}'

logging.debug("khalorg_format: %s", khalorg_format)
logging.debug("Stdout: %s", stdout)
logging.debug("Expected: %s", expected)
logging.debug("Actual: %s", actual)
assert str(expected) == str(actual)
# A recurring item should have a non empty rrule
assert bool(actual.properties['RRULE']) == bool(expected.first_timestamp._repeater) # noqa

Expand Down

0 comments on commit 37306c8

Please sign in to comment.