added new tests
Gitea Actions For Tree-Structurer / Explore-Gitea-Actions (push) Successful in 20s
Details
Gitea Actions For Tree-Structurer / Explore-Gitea-Actions (push) Successful in 20s
Details
This commit is contained in:
parent
3765449064
commit
9907388a14
|
@ -1,20 +1,22 @@
|
||||||
import pytest
|
import pytest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from prodir import create_tree_structurer, get_structure
|
from prodir import create_tree_structurer, get_structure, create_structure_from_file
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
def test_basic_structure(temp_directory):
|
def test_basic_structure(temp_directory):
|
||||||
"""Test that the basic directory structure is correctly represented."""
|
"""Test that the basic directory structure is correctly represented."""
|
||||||
create_tree_structurer()
|
create_tree_structurer()
|
||||||
structure = get_structure(str(temp_directory))
|
structure = get_structure(str(temp_directory))
|
||||||
|
|
||||||
# Convert structure to set for easier comparison
|
# Convert structure to set for easier comparison
|
||||||
structure_set = set(structure)
|
structure_set = set(structure)
|
||||||
|
|
||||||
# Print actual structure for debugging
|
# Print actual structure for debugging
|
||||||
print("\nActual structure:")
|
print("\nActual structure:")
|
||||||
for line in structure:
|
for line in structure:
|
||||||
print(f"'{line}'")
|
print(f"'{line}'")
|
||||||
|
|
||||||
# Expected entries (adjusted based on actual implementation)
|
# Expected entries (adjusted based on actual implementation)
|
||||||
must_contain = [
|
must_contain = [
|
||||||
"README.md",
|
"README.md",
|
||||||
|
@ -25,12 +27,12 @@ def test_basic_structure(temp_directory):
|
||||||
"utils",
|
"utils",
|
||||||
"helper.py"
|
"helper.py"
|
||||||
]
|
]
|
||||||
|
|
||||||
# Check that all required components are present somewhere in the structure
|
# Check that all required components are present somewhere in the structure
|
||||||
for entry in must_contain:
|
for entry in must_contain:
|
||||||
assert any(entry in line for line in structure), \
|
assert any(entry in line for line in structure), \
|
||||||
f"Required entry '{entry}' not found in structure"
|
f"Required entry '{entry}' not found in structure"
|
||||||
|
|
||||||
# Check that ignored directories/files are not present
|
# Check that ignored directories/files are not present
|
||||||
ignored_patterns = {
|
ignored_patterns = {
|
||||||
"__pycache__",
|
"__pycache__",
|
||||||
|
@ -39,7 +41,7 @@ def test_basic_structure(temp_directory):
|
||||||
"main.pyc",
|
"main.pyc",
|
||||||
".gitignore"
|
".gitignore"
|
||||||
}
|
}
|
||||||
|
|
||||||
for entry in structure:
|
for entry in structure:
|
||||||
for ignored in ignored_patterns:
|
for ignored in ignored_patterns:
|
||||||
assert ignored not in entry, \
|
assert ignored not in entry, \
|
||||||
|
@ -69,21 +71,89 @@ def test_nested_structure(temp_directory):
|
||||||
deep_path = temp_directory / "deep" / "nested" / "structure"
|
deep_path = temp_directory / "deep" / "nested" / "structure"
|
||||||
deep_path.mkdir(parents=True)
|
deep_path.mkdir(parents=True)
|
||||||
(deep_path / "test.py").touch()
|
(deep_path / "test.py").touch()
|
||||||
|
|
||||||
create_tree_structurer()
|
create_tree_structurer()
|
||||||
structure = get_structure(str(temp_directory))
|
structure = get_structure(str(temp_directory))
|
||||||
|
|
||||||
# Print actual structure for debugging
|
# Print actual structure for debugging
|
||||||
print("\nDeep structure:")
|
print("\nDeep structure:")
|
||||||
for line in structure:
|
for line in structure:
|
||||||
print(f"'{line}'")
|
print(f"'{line}'")
|
||||||
|
|
||||||
# Verify that all components of the deep path are present
|
# Verify that all components of the deep path are present
|
||||||
deep_components = ["deep", "nested", "structure", "test.py"]
|
deep_components = ["deep", "nested", "structure", "test.py"]
|
||||||
for component in deep_components:
|
for component in deep_components:
|
||||||
assert any(component in line for line in structure), \
|
assert any(component in line for line in structure), \
|
||||||
f"Deep component '{component}' not found in structure"
|
f"Deep component '{component}' not found in structure"
|
||||||
|
|
||||||
# Verify tree-like formatting is present
|
# Verify tree-like formatting is present
|
||||||
assert any("└" in line or "├" in line for line in structure), \
|
assert any("└" in line or "├" in line for line in structure), \
|
||||||
"Tree-like formatting characters not found in structure"
|
"Tree-like formatting characters not found in structure"
|
||||||
|
|
||||||
|
def test_invalid_indentation_structure(temp_directory):
|
||||||
|
"""Test handling of invalid indentation in the directory structure."""
|
||||||
|
create_tree_structurer()
|
||||||
|
# Create a file with invalid indentation
|
||||||
|
invalid_file_path = temp_directory / "invalid_structure.txt"
|
||||||
|
invalid_content = [
|
||||||
|
"root/",
|
||||||
|
"├── child1/",
|
||||||
|
"│ └── grandchild1",
|
||||||
|
"grandchild2" # Invalid indentation jump
|
||||||
|
]
|
||||||
|
invalid_file_path.write_text("\n".join(invalid_content), encoding='utf-8')
|
||||||
|
|
||||||
|
try:
|
||||||
|
create_structure_from_file(str(invalid_file_path), str(temp_directory))
|
||||||
|
pytest.fail("Expected an exception for invalid indentation")
|
||||||
|
except Exception as e: # Changed from RuntimeError
|
||||||
|
assert "Invalid indentation structure in the file" in str(e)
|
||||||
|
|
||||||
|
def test_create_structure_from_file(temp_directory):
|
||||||
|
"""Test creating a directory structure from a file."""
|
||||||
|
structure_file_path = temp_directory / "valid_structure.txt"
|
||||||
|
valid_content = """
|
||||||
|
project/
|
||||||
|
├── src/
|
||||||
|
│ ├── __init__.py
|
||||||
|
│ ├── main.py
|
||||||
|
│ ├── module1.py
|
||||||
|
│ └── module2.py
|
||||||
|
├── config/
|
||||||
|
│ └── config.yaml
|
||||||
|
├── .gitignore
|
||||||
|
├── pyproject.toml
|
||||||
|
├── setup.py
|
||||||
|
├── LICENSE
|
||||||
|
└── README.md
|
||||||
|
"""
|
||||||
|
structure_file_path.write_text(valid_content, encoding='utf-8')
|
||||||
|
|
||||||
|
# Create the structure
|
||||||
|
target_dir = temp_directory / "project"
|
||||||
|
create_structure_from_file(str(structure_file_path), str(target_dir))
|
||||||
|
|
||||||
|
# Remove the source file
|
||||||
|
structure_file_path.unlink()
|
||||||
|
|
||||||
|
# Define expected structure with proper tree markers
|
||||||
|
expected_structure = [
|
||||||
|
"LICENSE",
|
||||||
|
"README.md",
|
||||||
|
"config/",
|
||||||
|
"└── config.yaml",
|
||||||
|
"pyproject.toml",
|
||||||
|
"setup.py",
|
||||||
|
"src/",
|
||||||
|
"├── __init__.py",
|
||||||
|
"├── main.py",
|
||||||
|
"├── module1.py",
|
||||||
|
"└── module2.py"
|
||||||
|
]
|
||||||
|
|
||||||
|
# Get actual structure
|
||||||
|
actual_structure = get_structure(str(target_dir))
|
||||||
|
|
||||||
|
# Compare the structures
|
||||||
|
assert actual_structure == expected_structure, \
|
||||||
|
f"Expected structure:\n{expected_structure}\n\nGot:\n{actual_structure}"
|
Loading…
Reference in New Issue