74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
import tempfile
|
|
import os
|
|
from pathlib import Path
|
|
from codemap.parsers import PythonParser
|
|
|
|
|
|
def test_parse_simple_python_file():
|
|
parser = PythonParser()
|
|
code = '''
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def hello():
|
|
pass
|
|
'''
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
|
f.write(code)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
result = parser.parse(Path(temp_path))
|
|
assert result.module_name == Path(temp_path).stem
|
|
assert result.file_type == "python"
|
|
assert len(result.dependencies) >= 2
|
|
dep_names = [d.module_name for d in result.dependencies]
|
|
assert "os" in dep_names
|
|
assert "sys" in dep_names
|
|
assert "pathlib" in dep_names or "Path" in dep_names
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
|
|
def test_parse_python_with_from_import():
|
|
parser = PythonParser()
|
|
code = '''
|
|
from typing import List, Dict
|
|
from collections import Counter
|
|
'''
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
|
f.write(code)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
result = parser.parse(Path(temp_path))
|
|
assert len(result.dependencies) >= 2
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
|
|
def test_parse_python_with_syntax_error():
|
|
parser = PythonParser()
|
|
code = '''
|
|
def hello(:
|
|
pass
|
|
'''
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
|
f.write(code)
|
|
temp_path = f.name
|
|
|
|
try:
|
|
result = parser.parse(Path(temp_path))
|
|
assert result.file_type == "python"
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
|
|
def test_can_parse():
|
|
parser = PythonParser()
|
|
assert parser.can_parse(Path("test.py")) == True
|
|
assert parser.can_parse(Path("test.pyi")) == True
|
|
assert parser.can_parse(Path("test.js")) == False
|
|
assert parser.can_parse(Path("test.go")) == False
|