Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test_charm.py to pytest fixtures and parametrize #1193

Merged
merged 20 commits into from
Apr 24, 2024
Merged
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions test/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import typing
import unittest

from _pytest.monkeypatch import MonkeyPatch

import ops
from ops.model import _ModelBackend
from ops.storage import SQLiteStorage
Expand Down Expand Up @@ -74,6 +76,59 @@ def fake_script_calls(test_case: unittest.TestCase,
return calls # type: ignore


class FakeScriptFixture:
def __init__(self,
monkeypatch: MonkeyPatch,
tmp_path: pathlib.Path,
fake_script_path: typing.Union[pathlib.Path, None] = None):
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
self.monkeypatch = monkeypatch
if fake_script_path is None:
self.fake_script_path = tmp_path
self.monkeypatch.setenv(
"PATH",
str(self.fake_script_path),
prepend=os.pathsep) # type: ignore
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
else:
self.fake_script_path = fake_script_path

def fake_script(self,
name: str,
content: str):
template_args: typing.Dict[str, str] = {
'name': name,
'path': self.fake_script_path.as_posix(), # type: ignore
'content': content,
}

path: pathlib.Path = self.fake_script_path / name # type: ignore
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
with path.open('wt') as f: # type: ignore
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
# Before executing the provided script, dump the provided arguments in calls.txt.
# ASCII 1E is RS 'record separator', and 1C is FS 'file separator', which
# seem appropriate.
f.write( # type: ignore
'''#!/bin/sh
{{ printf {name}; printf "\\036%s" "$@"; printf "\\034"; }} >> {path}/calls.txt
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
{content}'''.format_map(template_args))
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
os.chmod(str(path), 0o755) # type: ignore # noqa: S103
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
# TODO: this hardcodes the path to bash.exe, which works for now but might
# need to be set via environ or something like that.
path.with_suffix(".bat").write_text( # type: ignore
f'@"C:\\Program Files\\git\\bin\\bash.exe" {path} %*\n')
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

def fake_script_calls(self, clear: bool = False) -> typing.List[typing.List[str]]:
calls_file: pathlib.Path = self.fake_script_path / 'calls.txt' # type: ignore
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
if not calls_file.exists(): # type: ignore
return []

# newline and encoding forced to linuxy defaults because on
# windows they're written from git-bash
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
with calls_file.open('r+t', newline='\n', encoding='utf8') as f: # type: ignore
calls = [line.split('\x1e') for line in f.read().split('\x1c')[:-1]] # type: ignore
if clear:
f.truncate(0) # type: ignore
return calls # type: ignore


class FakeScriptTest(unittest.TestCase):

def test_fake_script_works(self):
Expand Down
Loading