updated tests

This commit is contained in:
Falko Victor Habel 2025-02-09 11:45:21 +01:00
parent 9907388a14
commit 2a5a652638
6 changed files with 136 additions and 2 deletions

View File

@ -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 = []

View File

@ -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"),

13
test.txt Normal file
View File

@ -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

0
tests/cpp/__init__.py Normal file
View File

121
tests/test__main__.py Normal file
View File

@ -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