fix: 改进输入文件处理 - Closes #8 #9
@@ -4,6 +4,7 @@ Reads API keys from a secrets.yaml file, falling back to environment variables.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import yaml
|
||||
|
||||
@@ -23,11 +24,9 @@ PROMPTS_DIR = os.path.join(BASE_DIR, "prompts")
|
||||
TESTS_DIR = os.path.join(BASE_DIR, "tests")
|
||||
OUTPUT_DIR = IR_OUTPUT # backward compatibility alias
|
||||
|
||||
# Input file (the parsed PRD JSON)
|
||||
_DEFAULT_INPUT = os.path.join(
|
||||
PROJECT_OUTPUT, "车机娱乐系统禁止功能文档_脱敏 v0.9_v2_updated.json",
|
||||
)
|
||||
INPUT_JSON = os.environ.get("IR_INPUT_JSON", _DEFAULT_INPUT)
|
||||
# Input file (the parsed PRD JSON) — must be set via env var or CLI
|
||||
# No hardcoded default to avoid silently processing the wrong document.
|
||||
INPUT_JSON = os.environ.get("IR_INPUT_JSON", None)
|
||||
|
||||
|
||||
def set_input_file(path: str) -> None:
|
||||
@@ -125,8 +124,26 @@ def llm_client():
|
||||
|
||||
|
||||
def load_input_document(path: str | None = None) -> dict:
|
||||
"""Load the parsed PRD JSON document."""
|
||||
"""Load the parsed PRD JSON document.
|
||||
|
||||
Args:
|
||||
path: Explicit file path. If None, reads from IR_INPUT_JSON env var.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If no path is configured.
|
||||
SystemExit: If the configured path does not exist.
|
||||
"""
|
||||
path = path or INPUT_JSON
|
||||
if not path:
|
||||
print("错误: 未指定输入文件。请通过以下任一方式指定:", file=sys.stderr)
|
||||
print(" 1. 设置环境变量: IR_INPUT_JSON=<path>", file=sys.stderr)
|
||||
print(" 2. 通过 main.py: python main.py --input <path>", file=sys.stderr)
|
||||
print(" 3. 通过 step 脚本: python step1_semantic_index.py --input <path>", file=sys.stderr)
|
||||
print(" 4. 程序调用: config.set_input_file(<path>)", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not os.path.isfile(path):
|
||||
print(f"错误: 输入文件不存在: {path}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
@@ -19,6 +19,33 @@ def test_set_input_file():
|
||||
config.set_input_file(original)
|
||||
|
||||
|
||||
def test_no_hardcoded_input_file():
|
||||
"""INPUT_JSON should not have a hardcoded default — comes from env or None."""
|
||||
# When IR_INPUT_JSON env is not set, INPUT_JSON should be None
|
||||
env_val = os.environ.pop("IR_INPUT_JSON", None)
|
||||
try:
|
||||
import importlib
|
||||
importlib.reload(config)
|
||||
# After reload without env var, INPUT_JSON should be None
|
||||
assert config.INPUT_JSON is None, \
|
||||
f"INPUT_JSON should be None when env not set, got: {config.INPUT_JSON}"
|
||||
finally:
|
||||
if env_val is not None:
|
||||
os.environ["IR_INPUT_JSON"] = env_val
|
||||
importlib.reload(config)
|
||||
|
||||
|
||||
def test_set_input_file_accepts_none():
|
||||
"""set_input_file(None) should work for resetting."""
|
||||
original = config.INPUT_JSON
|
||||
try:
|
||||
config.set_input_file("/tmp/test.json")
|
||||
config.set_input_file(None)
|
||||
assert config.INPUT_JSON is None
|
||||
finally:
|
||||
config.set_input_file(original)
|
||||
|
||||
|
||||
def test_config_constants_exist():
|
||||
"""Verify all expected path constants are defined."""
|
||||
assert config.BASE_DIR
|
||||
|
||||
Reference in New Issue
Block a user