Repository: iamDyeus/tkreload (language: python)

Issue:
Implement Tests
Currently, there are no tests implemented for the library during the setup process. 

This issue aims to:
- Determine and implement the most suitable testing framework.
- Develop and integrate tests for the library to ensure its functionality and reliability.


Discussion / hints:
@iamDyeus I would be glad working on it 🙂
Go ahead, I'll assign the issue to you.
Please make sure you update the documentation for running tests so that other members can contribute as well.

Fix patch:
diff --git a/README.MD b/README.MD
index 624e4e9..de2c4e2 100644
--- a/README.MD
+++ b/README.MD
@@ -3,12 +3,15 @@
 ## Introduction
 
 ### The Problem
+
 Developers often spend a significant amount of time restarting or reloading applications, especially when working with terminal-based scripts. Each reload can add up to valuable minutes, or even hours, lost during development. Manually restarting terminal programs every time a change is made, or debugging with constant reloads, can become a major source of inefficiency.
 
 ### Time-Saving Analysis
+
 Let’s assume that a developer restarts their terminal application approximately 15 times per day, and each reload takes 30 seconds. This adds up to 7.5 minutes daily or around 3.12 hours per month. Multiply that by the number of developers working on a project, and you quickly see how terminal reloading becomes a significant bottleneck in development productivity.
 
 ### The Solution: tkreload
+
 **tkreload** aims to solve this issue by providing developers with an automatic reload mechanism for terminal-based Python applications. With tkreload, you no longer need to manually restart your applications, saving hours of development time and making debugging much more efficient. The tool is designed for seamless integration with Tkinter-based applications or any terminal-based Python script, ensuring smooth reloading after every code change.
 
 ---
@@ -19,21 +22,27 @@ Follow these steps to set up and install **tkreload**:
 
 1. **Create a Python Virtual Environment**  
    Open your terminal and run:
+
    ```bash
    python3 -m venv venv
    ```
+
 2. **Activate the Virtual Environment**
+
 - On Windows:
+
 ```bash
 .\venv\Scripts\activate
 ```
+
 - On macOS/Linux:
+
 ```bash
 source venv/bin/activate
 ```
 
 3. **Install Dependencies**
-Inside the activated virtual environment, install the required libraries listed in requirements.txt:
+   Inside the activated virtual environment, install the required libraries listed in requirements.txt:
 
 ```bash
 cd src # if not in /src
@@ -41,16 +50,67 @@ pip install -r requirements.txt
 ```
 
 4. **Install tkreload**
-Once the environment is set up and dependencies are installed, you can install the tkreload library via pip:
+   Once the environment is set up and dependencies are installed, you can install the tkreload library via pip:
 
 ```bash
 cd src # if not in /src
 pip install .
 ```
 
+---
+
+## Running Tests
+
+Tests are critical to ensuring that the functionality and reliability of tkreload are maintained. Here’s how you can run the test suite for the project:
+
+### 1. Install Testing Dependencies
+
+To run the tests, you will need to install the testing dependencies (e.g., `pytest` if it’s being used). Make sure to install the dependencies from the `requirements.txt` file:
+
+```bash
+pip install -r requirements.txt
+```
+
+### 2. Running Tests with `pytest`
+
+After installing the dependencies, you can run all tests using `pytest`. From the root directory of the project, execute the following command:
+
+```bash
+pytest
+```
+
+This will automatically discover and run all tests in the `tests/` folder.
+
+### 3. Running Tests with `unittest`
+
+If the project is using Python's built-in `unittest` framework, you can run the tests using:
+
+```bash
+python -m unittest discover tests
+```
+
+### 4. Checking Test Coverage
+
+To ensure the project has good test coverage, you can use the `pytest-cov` plugin. If you're using `pytest`, install the coverage package:
+
+```bash
+pip install pytest-cov
+```
+
+Then run:
+
+```bash
+pytest --cov=src
+```
+
+This will display the test coverage for the `tkreload` library.
+
+---
+
 ## Contributing
-I believe that tkreload can significantly improve developer efficiency, but I can't do it alone. So, I invite you to contribute to this project to help it reach its full potential.
 
+I believe that tkreload can significantly improve developer efficiency, but I can't do it alone. So, I invite you to contribute to this project to help it reach its full potential.
 
+---
 
-![alt text](.github\images\COMMUNISM.png)
\ No newline at end of file
+![alt text](.github\images\COMMUNISM.png)
diff --git a/src/requirements.txt b/src/requirements.txt
index 40b429a..9f99820 100644
--- a/src/requirements.txt
+++ b/src/requirements.txt
@@ -1,3 +1,5 @@
 rich==13.9.2
 setuptools==75.2.0
-watchdog==5.0.3
\ No newline at end of file
+watchdog==5.0.3
+pytest==7.4.2
+pytest-cov==4.1.0
\ No newline at end of file


Test patch:
diff --git a/tests/test_app_event_handler.py b/tests/test_app_event_handler.py
new file mode 100644
index 0000000..b8c7c9b
--- /dev/null
+++ b/tests/test_app_event_handler.py
@@ -0,0 +1,25 @@
+import unittest
+from src.tkreload.app_event_handler import AppFileEventHandler
+from unittest.mock import Mock
+from watchdog.events import FileModifiedEvent
+
+class TestAppFileEventHandler(unittest.TestCase):
+
+    def setUp(self):
+        self.callback = Mock()
+        self.handler = AppFileEventHandler(self.callback, 'test_app.py')
+
+    def test_on_modified_app_file(self):
+        # Simulate modifying the app file and check if callback is called
+        event = FileModifiedEvent('test_app.py')
+        self.handler.on_modified(event)
+        self.callback.assert_called_once()
+
+    def test_on_modified_unrelated_file(self):
+        # Simulate modifying an unrelated file and ensure callback is not called
+        event = FileModifiedEvent('other_file.py')
+        self.handler.on_modified(event)
+        self.callback.assert_not_called()
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/test_auto_reload.py b/tests/test_auto_reload.py
new file mode 100644
index 0000000..567fd35
--- /dev/null
+++ b/tests/test_auto_reload.py
@@ -0,0 +1,27 @@
+import unittest
+from src.tkreload.auto_reload import AutoReloadManager
+from rich.console import Console
+
+class TestAutoReloadManager(unittest.TestCase):
+
+    def setUp(self):
+        self.console = Console()
+        self.manager = AutoReloadManager(self.console)
+
+    def test_initial_status(self):
+        # Test if the auto-reload is initially set to False
+        self.assertFalse(self.manager.get_status())
+
+    def test_toggle_on(self):
+        # Test if toggling changes the auto-reload status to True
+        self.manager.toggle()
+        self.assertTrue(self.manager.get_status())
+
+    def test_toggle_off(self):
+        # Test if toggling twice turns auto-reload off again
+        self.manager.toggle()  # First toggle to True
+        self.manager.toggle()  # Second toggle to False
+        self.assertFalse(self.manager.get_status())
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/test_file_utils.py b/tests/test_file_utils.py
new file mode 100644
index 0000000..d0bf649
--- /dev/null
+++ b/tests/test_file_utils.py
@@ -0,0 +1,23 @@
+import unittest
+import os
+from src.tkreload.file_utils import file_exists, clear_terminal
+
+class TestFileUtils(unittest.TestCase):
+
+    def test_file_exists(self):
+        # Test if file_exists detects an actual file
+        test_file = __file__  # Current file should exist
+        self.assertTrue(file_exists(test_file))
+        
+        # Test with a non-existent file
+        self.assertFalse(file_exists('non_existent_file.txt'))
+
+    def test_clear_terminal(self):
+        try:
+            clear_terminal()
+            self.assertTrue(True)  # Ensure it runs without throwing errors
+        except Exception as e:
+            self.fail(f"clear_terminal() raised an exception: {e}")
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/test_main.py b/tests/test_main.py
new file mode 100644
index 0000000..6884a1c
--- /dev/null
+++ b/tests/test_main.py
@@ -0,0 +1,30 @@
+import unittest
+from unittest.mock import patch, Mock
+from src.tkreload.main import TkreloadApp
+
+class TestTkreloadApp(unittest.TestCase):
+
+    @patch('subprocess.Popen')
+    def test_run_tkinter_app(self, mock_popen):
+        app = TkreloadApp('test_app.py')
+        process = Mock()
+        mock_popen.return_value = process
+        
+        # Run the app and check if subprocess is called
+        result = app.run_tkinter_app()
+        mock_popen.assert_called_once_with(['python', 'test_app.py'])
+        self.assertEqual(result, process)
+
+    @patch('main.AppFileEventHandler')
+    @patch('main.Observer')
+    def test_monitor_file_changes(self, mock_observer, mock_event_handler):
+        app = TkreloadApp('test_app.py')
+        mock_callback = Mock()
+        
+        # Start monitoring and check if the observer was set up correctly
+        observer = app.monitor_file_changes(mock_callback)
+        mock_event_handler.assert_called_once_with(mock_callback, 'test_app.py')
+        mock_observer().schedule.assert_called_once()
+
+if __name__ == '__main__':
+    unittest.main()