Add test configuration and CLI tests
This commit is contained in:
124
tests/conftest.py
Normal file
124
tests/conftest.py
Normal file
@@ -0,0 +1,124 @@
|
||||
"""Pytest configuration and fixtures."""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_python_code():
|
||||
"""Sample Python code with various issues."""
|
||||
return '''
|
||||
import os
|
||||
|
||||
def dangerous_function(user_input):
|
||||
password = "secret123"
|
||||
result = eval(user_input)
|
||||
cursor.execute("SELECT * FROM users WHERE name = '" + user_input + "'")
|
||||
try:
|
||||
do_something()
|
||||
except:
|
||||
pass
|
||||
return 42 * multiplier
|
||||
|
||||
AWS_KEY = "AKIAIOSFODNN7EXAMPLE"
|
||||
|
||||
def process_items(items):
|
||||
for i in range(len(items)):
|
||||
print(items[i])
|
||||
result = list(list(items))
|
||||
return result
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_javascript_code():
|
||||
"""Sample JavaScript code with various issues."""
|
||||
return '''
|
||||
const password = "secret123";
|
||||
const AWS_KEY = "AKIAIOSFODNN7EXAMPLE";
|
||||
|
||||
function process(userInput) {
|
||||
const result = eval(userInput);
|
||||
const query = "SELECT * FROM users WHERE name = " + userInput;
|
||||
try {
|
||||
doSomething();
|
||||
} catch (e) {
|
||||
// silent catch
|
||||
}
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
console.log(items[i]);
|
||||
}
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_typescript_code():
|
||||
"""Sample TypeScript code with various issues."""
|
||||
return '''
|
||||
const apiKey = "AIzaSyDxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
||||
const token = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
||||
|
||||
async function fetchData(userId: string) {
|
||||
try {
|
||||
const result = eval("getData(" + userId + ")");
|
||||
return result;
|
||||
} catch {
|
||||
// empty catch
|
||||
}
|
||||
}
|
||||
|
||||
const config = {
|
||||
password: "secret123"
|
||||
};
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def clean_python_code():
|
||||
"""Sample clean Python code without issues."""
|
||||
return '''
|
||||
def calculate_total(items: list[int]) -> int:
|
||||
"""Calculate the total of all items."""
|
||||
total = 0
|
||||
for item in items:
|
||||
total += item
|
||||
return total
|
||||
|
||||
class Calculator:
|
||||
"""A simple calculator class."""
|
||||
|
||||
DEFAULT_MULTIPLIER = 1
|
||||
|
||||
def __init__(self, multiplier: int = DEFAULT_MULTIPLIER):
|
||||
self.multiplier = multiplier
|
||||
|
||||
def multiply(self, value: int) -> int:
|
||||
"""Multiply the value by the multiplier."""
|
||||
return value * self.multiplier
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_file_with_content():
|
||||
"""Create a temporary file with given content."""
|
||||
def _create_temp_file(content: str, suffix: str = ".py") -> Path:
|
||||
with tempfile.NamedTemporaryFile(mode="w", suffix=suffix, delete=False) as f:
|
||||
f.write(content)
|
||||
return Path(f.name)
|
||||
return _create_temp_file
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_directory():
|
||||
"""Create a temporary directory with test files."""
|
||||
def _create_temp_dir(files: dict[str, str]) -> Path:
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
for filename, content in files.items():
|
||||
filepath = Path(tmpdir) / filename
|
||||
filepath.parent.mkdir(parents=True, exist_ok=True)
|
||||
filepath.write_text(content)
|
||||
return Path(tmpdir)
|
||||
return _create_temp_dir
|
||||
Reference in New Issue
Block a user