Skip to content

Commit

Permalink
Feat/add template system (#5)
Browse files Browse the repository at this point in the history
* feat: add template system in json

* feat: update .gitignore to not ignore the tests folder
  • Loading branch information
andreasbaumgartner committed Apr 24, 2024
1 parent 7f7282b commit 866f721
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __pycache__/
*$py.class

# test projects
test*
src/test*

# C extensions
*.so
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h1 align="center">POMPEJI</h1>
</p>
<p align="center">
<em>Automated Python Project Setup!</em>
<em>Automated Python Project Setup with Templates!</em>
</p>
<p align="center">
<img src="https://img.shields.io/github/license/eli64s/readme-ai?style=flat&logo=opensourceinitiative&logoColor=white&color=0080ff" alt="license">
Expand Down
56 changes: 45 additions & 11 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import inquirer
import os
import sys
Expand All @@ -12,14 +13,21 @@ def main():
print("Please provide an argument like: 'project_name'")
sys.exit(1)
project_name = sys.argv[1]
generator = BaseStructureGenerator(project_name)
generator.create_dir()
generator.create_subdirs()
file_generator = BaseFileGenerator(project_name)
file_generator.create_base_files()
service = BaseService()
service.ask_service()
service.create_service()
# generator = BaseStructureGenerator(project_name)
# generator.create_dir()
# generator.create_subdirs()
# file_generator = BaseFileGenerator(project_name)
# file_generator.create_base_files()
# service = BaseService()
# service.ask_service()
# service.create_service()
# TEST: Test with the template class

template = Template()
template.get_templates()
template.read_template("base.json")
template.convert_template()

print(f"Project structure for '{project_name}' created successfully.")


Expand Down Expand Up @@ -73,13 +81,13 @@ def __init__(self, project_name):
def create_base_files(self):
"""Create base files for a project."""
for file in self.base_files:
open(os.path.join(self.current_path, self.project_name, file), "w").close()
open(os.path.join(self.current_path, self.project_name, file), "w").close() # noqa: E501

def create_subdir_files(self):
"""Create files in subdirectories."""
for file in self.subdir_files:
open(
os.path.join(self.current_path, self.project_name, self.subdir, file),
os.path.join(self.current_path, self.project_name, self.subdir, file), # noqa: E501
"w",
).close()

Expand Down Expand Up @@ -193,7 +201,7 @@ def create_pytest(self):

def create_license(self) -> str:
"""Create LICENSE File."""
open(os.path.join(self.current_path, self.project_name, "LICENSE"), "a").close()
open(os.path.join(self.current_path, self.project_name, "LICENSE"), "a").close() # noqa: E501
return "License created"

def create_setup_cfg(self):
Expand All @@ -207,5 +215,31 @@ def create_setup_nox(self):
return sys.exit(1)


class Template:
"""Base class for templates."""

def __init__(self):
self.template_path = os.path.join(os.getcwd(), "templates")
self.templates = []
self.template = None
self.template_name = None
self.worker = None

def get_templates(self):
"""Return all available templates."""
self.templates = os.listdir(self.template_path)
return self.templates

def read_template(self, file_name):
with open(os.path.join(self.template_path, file_name), "r") as file:
self.template = file.read()
return self.template

def convert_template(self):
if self.template is not None:
self.worker = json.loads(self.template)
return self.worker


if __name__ == "__main__":
Application.main()
23 changes: 23 additions & 0 deletions src/templates/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"base": [
"README.md",
"LICENSE"
],

"services": [
"pip",
"virtualenv",
"git",
"docker",
"pytest",
"ruff"
],

"python": [
"python3.11"
],

"config": [
"pyproject.toml"
]
}
Empty file added tests/__init__.py
Empty file.
90 changes: 90 additions & 0 deletions tests/test_sys_reqs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import pytest

from unittest.mock import patch
from src.check_sys import (
SystemCheck,
)


def test_check_os_system_linux():
"""Test if the system check passes on a Linux system without exiting."""
with patch("src.check_sys.platform", new="linux"):
with patch("src.check_sys.sys.exit") as mock_exit:
SystemCheck()
mock_exit.assert_not_called()


def test_check_os_system_non_linux():
"""Test if the system check exits on non-Linux systems."""
with patch("src.check_sys.platform", new="win32"):
with patch("src.check_sys.sys.exit") as mock_exit:
SystemCheck()
mock_exit.assert_called_once_with(1)


def test_check_python_installed_when_python_is_installed():
with patch("os.system") as mock_system:
mock_system.return_value = 0
instance = SystemCheck()
# Execute the function, expecting no exceptions or sys.exit
instance.check_python_installed()
mock_system.assert_called_with("python3 --version")


def test_check_python_installed_when_python_is_not_installed():
with (
patch("os.system") as mock_system,
pytest.raises(SystemExit) as pytest_wrapped_e,
):
mock_system.return_value = 1
with patch("sys.exit") as mock_exit:
mock_exit.side_effect = SystemExit
instance = SystemCheck()
instance.check_python_installed()
mock_system.assert_called_with("python3 --version")
mock_exit.assert_called_once_with(1)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1


def test_check_python_version_correct():
"""Test that no exit occurs if Python 3.10 or higher is installed."""
with patch(
"src.check_sys.os.system", return_value=0
): # Assuming Python 3.10+ is correctly installed
with patch("src.check_sys.sys.exit") as mock_exit:
SystemCheck()
mock_exit.assert_not_called()


def test_check_git_installed_when_git_is_installed():
with patch("os.system") as mock_system:
mock_system.return_value = 0
instance = SystemCheck()
# Execute the function, expecting no exceptions or sys.exit
instance.check_git_installed()
mock_system.assert_called_with("git --version")


def test_check_git_installed_when_git_is_not_installed():
with (
patch("os.system") as mock_system,
pytest.raises(SystemExit) as pytest_wrapped_e,
):
mock_system.return_value = 1
with patch("sys.exit") as mock_exit:
mock_exit.side_effect = SystemExit
instance = SystemCheck()
instance.check_git_installed()
mock_system.assert_called_with("git --version")
mock_exit.assert_called_once_with(1)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1


def test_check_git_installed_correctly():
"""Test that no exit occurs if Git is installed."""
with patch("src.check_sys.os.system", return_value=0):
with patch("src.check_sys.sys.exit") as mock_exit:
_ = SystemCheck()
mock_exit.assert_not_called()

0 comments on commit 866f721

Please sign in to comment.