Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 50 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from unittest.mock import patch, MagicMock
from struct_module.commands.generate import GenerateCommand
from struct_module.commands.info import InfoCommand
from struct_module.commands.validate import ValidateCommand
from struct_module.commands.list import ListCommand
import argparse

@pytest.fixture
def parser():
return argparse.ArgumentParser()

def test_generate_command(parser):
command = GenerateCommand(parser)
args = parser.parse_args(['structure.yaml', 'base_path'])
with patch.object(command, '_create_structure') as mock_create_structure:
command.execute(args)
mock_create_structure.assert_called_once()

def test_info_command(parser):
command = InfoCommand(parser)
args = parser.parse_args(["github/workflows/pre-commit"])
with patch('builtins.print') as mock_print:
command.execute(args)
mock_print.assert_called()

def test_list_command(parser):
command = ListCommand(parser)
args = parser.parse_args([])
with patch('os.walk', return_value=[('root', [], ['file.yaml'])]):
with patch('builtins.print') as mock_print:
command.execute(args)
mock_print.assert_called()

def test_validate_command(parser):
command = ValidateCommand(parser)
args = parser.parse_args(['config.yaml'])
with patch.object(command, '_validate_structure_config') as mock_validate_structure, \
patch.object(command, '_validate_folders_config') as mock_validate_folders, \
patch.object(command, '_validate_variables_config') as mock_validate_variables, \
patch('builtins.open', new_callable=MagicMock) as mock_open, \
patch('yaml.safe_load', return_value={
'structure': [],
'folders': [],
'variables': []
}):
command.execute(args)
mock_validate_structure.assert_called_once()
mock_validate_folders.assert_called_once()
mock_validate_variables.assert_called_once()
18 changes: 18 additions & 0 deletions tests/test_completers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from struct_module.completers import log_level_completer, file_strategy_completer

def test_log_level_completer():
completer = log_level_completer()
assert 'DEBUG' in completer
assert 'INFO' in completer
assert 'WARNING' in completer
assert 'ERROR' in completer
assert 'CRITICAL' in completer

def test_file_strategy_completer():
completer = file_strategy_completer()
assert 'overwrite' in completer
assert 'skip' in completer
assert 'append' in completer
assert 'rename' in completer
assert 'backup' in completer
24 changes: 24 additions & 0 deletions tests/test_file_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from unittest.mock import patch, MagicMock
from struct_module.file_item import FileItem

@pytest.fixture
def file_item():
properties = {
"name": "test.txt",
"content": "file content",
"config_variables": [],
"input_store": "/tmp/input.json"
}
return FileItem(properties)

def test_apply_template_variables(file_item):
template_vars = {"var1": "value1"}
file_item.apply_template_variables(template_vars)
assert file_item.content == "file content"

def test_fetch_content(file_item):
with patch('struct_module.content_fetcher.ContentFetcher.fetch_content') as mock_fetch:
mock_fetch.return_value = "fetched content"
file_item.fetch_content()
assert file_item.content == "file content"
10 changes: 0 additions & 10 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ def test_get_latest_release(mock_getenv, mock_github):
# Test with a valid release
assert get_latest_release('fake/repo') == 'v1.0.0'

# Test with an exception in get_latest_release
mock_repo.get_latest_release.side_effect = Exception()
assert get_latest_release('fake/repo') == 'main'

# Test with an exception in default_branch
mock_repo.default_branch = 'LATEST_RELEASE_ERROR'
mock_repo.get_latest_release.side_effect = Exception()
# mock_repo.default_branch.side_effect = Exception()
assert get_latest_release('fake/repo') == 'LATEST_RELEASE_ERROR'

def test_slugify():
assert slugify('Hello World') == 'hello-world'
assert slugify('Python 3.8') == 'python-38'
Expand Down
36 changes: 36 additions & 0 deletions tests/test_input_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest
import json
import os
from struct_module.input_store import InputStore

@pytest.fixture
def input_store(tmp_path):
input_file = tmp_path / "input.json"
return InputStore(input_file)

def test_load(input_store):
data = {"key": "value"}
with open(input_store.input_file, 'w') as f:
json.dump(data, f)
input_store.load()
assert input_store.get_data() == data

def test_get_value(input_store):
data = {"key": "value"}
with open(input_store.input_file, 'w') as f:
json.dump(data, f)
input_store.load()
assert input_store.get_value("key") == "value"

def test_set_value(input_store):
input_store.load()
input_store.set_value("key", "value")
assert input_store.get_value("key") == "value"

def test_save(input_store):
input_store.load()
input_store.set_value("key", "value")
input_store.save()
with open(input_store.input_file, 'r') as f:
data = json.load(f)
assert data == {"key": "value"}
29 changes: 29 additions & 0 deletions tests/test_template_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from unittest.mock import patch, MagicMock
from struct_module.template_renderer import TemplateRenderer

@pytest.fixture
def renderer():
config_variables = [
{"var1": {"type": "string", "default": "default1"}},
{"var2": {"type": "string", "default": "default2"}}
]
input_store = "/tmp/input.json"
return TemplateRenderer(config_variables, input_store)

def test_render_template(renderer):
content = "Hello, {{@ var1 @}}!"
vars = {"var1": "World"}
rendered_content = renderer.render_template(content, vars)
assert rendered_content == "Hello, World!"

def test_prompt_for_missing_vars(renderer):
content = "Hello, {{@ var1 @}} and {{@ var2 @}}!"
vars = {"var1": "World"}
with patch('builtins.input', side_effect=["Universe"]):
missing_vars = renderer.prompt_for_missing_vars(content, vars)
assert missing_vars["var2"] == "Universe"

def test_get_defaults_from_config(renderer):
defaults = renderer.get_defaults_from_config()
assert defaults == {"var1": "default1", "var2": "default2"}
Loading