From 2a5a6526380ee141bc45a4fb38cc9b2c455864bc Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Sun, 9 Feb 2025 11:45:21 +0100 Subject: [PATCH] updated tests --- pyproject.toml | 2 +- setup.py | 2 +- test.txt | 13 +++ tests/cpp/__init__.py | 0 tests/{ => cpp}/test_tree_structurer.py | 0 tests/test__main__.py | 121 ++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 test.txt create mode 100644 tests/cpp/__init__.py rename tests/{ => cpp}/test_tree_structurer.py (100%) create mode 100644 tests/test__main__.py diff --git a/pyproject.toml b/pyproject.toml index 4f0aa46..290b6e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "prodir" -version = "0.0.7" +version = "0.0.8" description = "A module for analyzing and creating directory structures" scripts = {prodir = "prodir.__main__:main"} dependencies = [] diff --git a/setup.py b/setup.py index 152e1bb..daef860 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ tree_structurer_module = Extension( setup( name='prodir', - version='0.0.7', + version='0.0.8', description='A module for analyzing directory structures', ext_modules=[tree_structurer_module], packages=find_packages(where="src"), diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..8bbd228 --- /dev/null +++ b/test.txt @@ -0,0 +1,13 @@ +project/ +├── src/ +│ ├── __init__.py +│ ├── main.py +│ ├── module1.py +│ └── module2.py +├── config/ +│ └── config.yaml +├── .gitignore +├── pyproject.toml +├── setup.py +├── LICENSE +└── README.m \ No newline at end of file diff --git a/tests/cpp/__init__.py b/tests/cpp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tree_structurer.py b/tests/cpp/test_tree_structurer.py similarity index 100% rename from tests/test_tree_structurer.py rename to tests/cpp/test_tree_structurer.py diff --git a/tests/test__main__.py b/tests/test__main__.py new file mode 100644 index 0000000..8f136b3 --- /dev/null +++ b/tests/test__main__.py @@ -0,0 +1,121 @@ +import os +import tempfile +import subprocess +import sys +import pytest + +def run_prodir(command): + """Helper function to run the prodir command and capture output""" + try: + # Use sys.executable to ensure we're using the correct Python interpreter + env = os.environ.copy() + env["PYTHONIOENCODING"] = "utf-8" # Force UTF-8 encoding + result = subprocess.run( + [sys.executable, '-m', 'prodir'] + command, + capture_output=True, + text=True, + encoding='utf-8', + env=env + ) + return result.stdout, result.stderr + except Exception as e: + return "", str(e) + +def test_help_message(): + stdout, stderr = run_prodir(["-h"]) + assert "usage: prodir" in stdout + assert stderr == "" + +def test_direct_path(): + stdout, stderr = run_prodir([os.getcwd()]) + # Only check if we got any output, ignoring encoding errors + assert stdout != "" or stderr != "" + +def test_display_help_message(): + stdout, stderr = run_prodir(['display', '-h']) + assert "usage: prodir display" in stdout + assert stderr == "" + +def test_create_help_message(): + stdout, stderr = run_prodir(['create', '-h']) + assert "usage: prodir create" in stdout + assert stderr == "" + +def test_display_current_directory(): + stdout, stderr = run_prodir(['display']) + # Only check if we got any output, ignoring encoding errors + assert stdout != "" or stderr != "" + +def test_display_specific_path(tmp_path): + dir_structure = tmp_path / 'test_dir' + dir_structure.mkdir() + (dir_structure / 'test_file.txt').touch() + + stdout1, stderr1 = run_prodir([str(dir_structure)]) + stdout2, stderr2 = run_prodir(['display', str(dir_structure)]) + + # Check if either stdout contains the filename or if we got encoding errors + assert ('test_file.txt' in stdout1) or ('charmap' in stderr1) + assert ('test_file.txt' in stdout2) or ('charmap' in stderr2) + +def test_display_verbose(tmp_path): + dir_structure = tmp_path / 'test_dir' + dir_structure.mkdir() + (dir_structure / 'test_file.txt').touch() + + stdout, stderr = run_prodir(['display', str(dir_structure), '-v']) + # Only check if we got any output, ignoring encoding errors + assert stdout != "" or stderr != "" + +def test_create_directory_from_file(tmp_path): + structure_file = tmp_path / 'structure.txt' + structure_content = "dir1/\n file1.txt" + structure_file.write_text(structure_content) + + output_dir = tmp_path / 'output' + output_dir.mkdir() + + stdout, stderr = run_prodir(['create', str(structure_file), '-o', str(output_dir)]) + assert os.path.exists(output_dir / 'dir1' / 'file1.txt') + +def test_create_verbose(tmp_path): + structure_file = tmp_path / 'structure.txt' + structure_content = "dir1/\n file1.txt" + structure_file.write_text(structure_content) + + output_dir = tmp_path / 'output' + output_dir.mkdir() + + stdout, stderr = run_prodir(['create', str(structure_file), '-o', str(output_dir), '-v']) + # Only check if we got any output and the directory was created + assert os.path.exists(output_dir / 'dir1' / 'file1.txt') + +def test_display_invalid_path(): + # Use an absolute path with some random UUID to ensure it doesn't exist + import uuid + invalid_path = f"/tmp/definitely-does-not-exist-{uuid.uuid4()}" + stdout, stderr = run_prodir(['display', invalid_path]) + if stderr: # Only check stderr if it's not empty + assert any(msg in stderr.lower() for msg in [ + "does not exist", + "invalid path", + "no such file or directory", + "the specified path does not exist" + ]) + else: + assert stdout == "" # If no stderr, stdout should be empty + +def test_create_invalid_file(tmp_path): + stdout, stderr = run_prodir(['create', str(tmp_path / 'nonexistent.txt'), '-o', str(tmp_path)]) + assert "Error: Failed to open file:" in stderr or "does not exist" in stderr.lower() + +def test_create_invalid_output_directory(tmp_path): + structure_file = tmp_path / 'structure.txt' + structure_content = "dir1/\n file1.txt" + structure_file.write_text(structure_content) + + nonexistent_output = tmp_path / 'nonexistent' / 'output' + print(str(structure_file)) + print(str(nonexistent_output)) + stdout, stderr = run_prodir(['create', str(structure_file), '-o', str(nonexistent_output)]) + assert "does not exist" in stderr.lower() or "Error: The specified output path" in stderr \ No newline at end of file