-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,99 @@ | ||
from dataclasses import dataclass | ||
|
||
import pytest | ||
from click import Path | ||
|
||
from joystick_diagrams import exceptions, plugin_manager | ||
from joystick_diagrams.plugin_manager import load_plugin | ||
from joystick_diagrams.plugin_manager import ( | ||
check_expected_files, | ||
check_folder_validity, | ||
load_plugin, | ||
) | ||
|
||
TEST_PLUGIN_PATH = "../tests/data/test_plugins" | ||
|
||
|
||
# Test when the plugin is loaded successfully | ||
def test_load_plugin_not_valid(caplog): | ||
# Call the function with a test plugin_package and module_path | ||
plugin_package = "tests.data.test_plugins." | ||
module_path = "notPlugin" | ||
|
||
with pytest.raises(exceptions.PluginNotValid) as e: | ||
load_plugin(plugin_package, module_path) | ||
|
||
assert "notPlugin.main" in caplog.text | ||
|
||
|
||
@pytest.fixture | ||
def plugin_path_directory(request): | ||
"""Used to provide fixtures for Pathlib | ||
Accepts FileNames, is_File and is_Dir checks per item | ||
""" | ||
|
||
@dataclass | ||
class PathItem: | ||
stem: str | ||
file: bool | ||
|
||
def is_file(self): | ||
return self.file if self.file else False | ||
|
||
def is_dir(self): | ||
return self.file if not self.file else True | ||
|
||
@dataclass | ||
class MockPathObject: | ||
items: set[PathItem] | ||
|
||
def iterdir(self): | ||
for i in self.items: | ||
yield i | ||
|
||
path_items = [PathItem(x, request.param.get(x)) for x in request.param] | ||
return MockPathObject(path_items) | ||
|
||
|
||
@pytest.mark.parametrize("plugin_path_directory", [{"FileA": True, "FileB": True, "FolderC": False}], indirect=True) | ||
def test_check_expected_files_valid(monkeypatch, plugin_path_directory): | ||
_expected_files = ["FileA", "FileB"] | ||
monkeypatch.setattr(plugin_manager, "EXPECTED_PLUGIN_FILES", _expected_files) | ||
|
||
_mock_path = plugin_path_directory | ||
_check = check_expected_files(_mock_path) | ||
|
||
assert _check is True | ||
|
||
|
||
@pytest.mark.parametrize("plugin_path_directory", [{"FileA": True, "FileB": True, "FileC": False}], indirect=True) | ||
def test_check_expected_files_invalid(monkeypatch, plugin_path_directory): | ||
_expected_files = ["FileC", "FileD"] | ||
monkeypatch.setattr(plugin_manager, "EXPECTED_PLUGIN_FILES", _expected_files) | ||
|
||
_mock_path = plugin_path_directory | ||
_check = check_expected_files(_mock_path) | ||
|
||
assert _check is False | ||
|
||
|
||
@pytest.mark.parametrize("plugin_path_directory", [{"Folder": True}], indirect=True) | ||
def test_check_folder_validity_valid(monkeypatch, plugin_path_directory): | ||
def mock_check_files(monkeypatch): | ||
return True | ||
|
||
monkeypatch.setattr(plugin_manager, "check_expected_files", mock_check_files) | ||
|
||
_check = check_folder_validity(plugin_path_directory.items[0]) | ||
|
||
assert _check is True | ||
|
||
|
||
@pytest.mark.parametrize("plugin_path_directory", [{"Folder": False}], indirect=True) | ||
def test_check_folder_validity_invalid(monkeypatch, plugin_path_directory): | ||
def mock_check_files(monkeypatch): | ||
return True | ||
|
||
monkeypatch.setattr(plugin_manager, "check_expected_files", mock_check_files) | ||
|
||
_check = check_folder_validity(plugin_path_directory.items[0]) | ||
|
||
assert _check is False |