fix: 清除硬编码API Key与机器特定路径,改用环境变量+混合式路径解析
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
"""
|
||||
|
||||
import sys, os
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
|
||||
from cobol_testgen.read import (
|
||||
@@ -17,8 +19,17 @@ from cobol_testgen.read import (
|
||||
from cobol_testgen.core import build_branch_tree
|
||||
from cobol_testgen.coverage import collect_decision_points
|
||||
|
||||
_SRC = r'C:\Users\marye\Desktop\2026技术大赛\cobol-tna-system\src'
|
||||
_CPY = r'C:\Users\marye\Desktop\2026技术大赛\cobol-tna-system\cpy'
|
||||
# TNA 测试数据目录: 环境变量优先 → 大赛兄弟目录兜底(见 .env.example)
|
||||
_TNA = Path(os.environ.get(
|
||||
"COBOL_TNA_ROOT",
|
||||
Path(__file__).resolve().parent.parent.parent.parent / "cobol-tna-system",
|
||||
))
|
||||
_SRC = str(_TNA / "src")
|
||||
_CPY = str(_TNA / "cpy")
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not _TNA.exists(), reason=f"TNA test data not found: {_TNA} (set COBOL_TNA_ROOT)"
|
||||
)
|
||||
|
||||
|
||||
def _load(pid):
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""E2E Tests for COBOL->Java Verification Platform
|
||||
|
||||
Run: cd D:/cobol-java/v3-gstack-code-gen && python -m pytest tests/e2e/ -v
|
||||
Requires: web server on http://127.0.0.1:8000, WSL available
|
||||
Run: cd <project-root> && python -m pytest tests/e2e/ -v
|
||||
Requires: web server on http://127.0.0.1:8000, WSL available,
|
||||
DEEPSEEK_API_KEY set in the environment.
|
||||
"""
|
||||
import json, os, sys, time, uuid, shutil
|
||||
from datetime import datetime
|
||||
@@ -50,10 +51,19 @@ def create_task(copybook: str, cobol: str, java_dir: str, mapping: str, runner="
|
||||
return tid
|
||||
|
||||
|
||||
def _wsl_path(p: Path) -> str:
|
||||
"""Convert a Windows path to its WSL /mnt/<drive> equivalent."""
|
||||
s = str(p).replace("\\", "/")
|
||||
if len(s) > 1 and s[1] == ":":
|
||||
return f"/mnt/{s[0].lower()}{s[2:]}"
|
||||
return s
|
||||
|
||||
|
||||
def run_worker_for_task(tid: str):
|
||||
api_key = os.environ.get("DEEPSEEK_API_KEY", "")
|
||||
script = (
|
||||
"cd /mnt/d/cobol-java/v3-gstack-code-gen && "
|
||||
"export LLM_API_KEY=sk-ca4961087c7f4aefa8ed0fc6f3d02329 && "
|
||||
f"cd {_wsl_path(PROJECT)} && "
|
||||
f"export LLM_API_KEY='{api_key}' && "
|
||||
"export LLM_API_BASE=https://api.deepseek.com/v1 && "
|
||||
"export LLM_MODEL=deepseek-chat && "
|
||||
f"python3 -c \"exec(open('write_result.py').read().replace('ec17bf32','{tid}'))\""
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
"""Prepare test data for Playwright E2E tests."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
FIXTURES = Path(__file__).parent / "fixtures"
|
||||
COBOL_GIT = Path(r"D:\cobol-java\jcl-cobol-git")
|
||||
COBOL_GIT = Path(
|
||||
os.environ.get("COBOL_GIT_ROOT",
|
||||
Path(__file__).resolve().parent.parent.parent / "jcl-cobol-git")
|
||||
)
|
||||
|
||||
|
||||
def prepare():
|
||||
|
||||
@@ -40,10 +40,10 @@ def test_normalize_injects_mapping_after_endsql(tmp_path):
|
||||
assert "SQLCODE = -2067" in out
|
||||
|
||||
|
||||
def test_normalize_mapping_before_if_sqlcode():
|
||||
def test_normalize_mapping_before_if_sqlcode(tmp_path):
|
||||
"""映射代码注入在 IF SQLCODE = -803 判断之前"""
|
||||
r = _make_runner()
|
||||
pp = Path(r'C:\Users\marye\AppData\Local\Temp\opencode\t3_pp.cbl')
|
||||
pp = tmp_path / "t3_pp.cbl"
|
||||
pp.parent.mkdir(parents=True, exist_ok=True)
|
||||
pp.write_text(_pp_text_with_if(), encoding="utf-8")
|
||||
r._patch_sqlcode_normalize(pp)
|
||||
@@ -56,10 +56,10 @@ def test_normalize_mapping_before_if_sqlcode():
|
||||
assert mapping_idx < if_idx
|
||||
|
||||
|
||||
def test_normalize_does_not_change_ok_path():
|
||||
def test_normalize_does_not_change_ok_path(tmp_path):
|
||||
"""归一化不影响 SQLCODE=0 成功路径"""
|
||||
r = _make_runner()
|
||||
pp = Path(r'C:\Users\marye\AppData\Local\Temp\opencode\t3b_pp.cbl')
|
||||
pp = tmp_path / "t3b_pp.cbl"
|
||||
pp.write_text(_pp_text_with_if(), encoding="utf-8")
|
||||
r._patch_sqlcode_normalize(pp)
|
||||
out = pp.read_text(encoding="utf-8")
|
||||
|
||||
@@ -8,6 +8,7 @@ from pathlib import Path
|
||||
from playwright.sync_api import Page, expect, sync_playwright
|
||||
|
||||
BASE_URL = "http://127.0.0.1:8000"
|
||||
COBOL_GIT = Path(os.environ.get("COBOL_GIT_ROOT", Path(__file__).resolve().parent.parent.parent / "jcl-cobol-git"))
|
||||
FIXTURES = Path(__file__).parent / "fixtures"
|
||||
TESTS_DIR = Path(__file__).parent
|
||||
|
||||
@@ -172,7 +173,7 @@ def test_file_size_limit(page: Page):
|
||||
@pytest.mark.skipif(not _cobol_available(), reason="WSL not available")
|
||||
def test_cobol_system_pipeline_exists(page: Page):
|
||||
"""TC-E2E-02 prep: Verify COBOL system data files exist."""
|
||||
data_dir = Path(r"D:\cobol-java\jcl-cobol-git\data")
|
||||
data_dir = COBOL_GIT / "data"
|
||||
assert (data_dir / "input/member.dat").exists(), "member.dat missing"
|
||||
assert (data_dir / "input/rate.dat").exists(), "rate.dat missing"
|
||||
assert (data_dir / "output/summary_report.dat").exists(), "summary_report missing"
|
||||
@@ -181,7 +182,7 @@ def test_cobol_system_pipeline_exists(page: Page):
|
||||
@pytest.mark.skipif(not _cobol_available(), reason="WSL not available")
|
||||
def test_cobol_output_consistent(page: Page):
|
||||
"""TC-E2E-02: CRDVAL output matches known golden data."""
|
||||
output = Path(r"D:\cobol-java\jcl-cobol-git\data\output")
|
||||
output = COBOL_GIT / "data" / "output"
|
||||
|
||||
# Verify error report has 7+ error types
|
||||
errors = (output / "error_report.dat").read_text()
|
||||
@@ -200,8 +201,8 @@ def test_cobol_output_consistent(page: Page):
|
||||
@pytest.mark.skipif(not _cobol_available(), reason="WSL not available")
|
||||
def test_java_output_equals_cobol(page: Page):
|
||||
"""TC-E2E-02: Java CRDVAL output matches COBOL."""
|
||||
cobol_dir = Path(r"D:\cobol-java\jcl-cobol-git\data\output")
|
||||
java_dir = Path(r"D:\cobol-java\jcl-cobol-git\data\output")
|
||||
cobol_dir = COBOL_GIT / "data" / "output"
|
||||
java_dir = COBOL_GIT / "data" / "output"
|
||||
|
||||
cobol_report = cobol_dir / "error_report.dat"
|
||||
assert cobol_report.exists(), "COBOL error report missing"
|
||||
@@ -218,7 +219,7 @@ def test_java_output_equals_cobol(page: Page):
|
||||
@pytest.mark.skipif(not _cobol_available(), reason="WSL not available")
|
||||
def test_file_format_consistency(page: Page):
|
||||
"""TC-E2E-03: COBOL LINE SEQUENTIAL → JSON → Java roundtrip works."""
|
||||
cobol_dir = Path(r"D:\cobol-java\jcl-cobol-git")
|
||||
cobol_dir = COBOL_GIT
|
||||
|
||||
# Check JSON conversion output exists
|
||||
json_file = cobol_dir / "data/work/validated_tx.json"
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
"""
|
||||
Golden tests — 对接真实 COBOL 信用卡月结系统
|
||||
数据源: D:\cobol-java\jcl-cobol-git\
|
||||
数据源: COBOL_GIT_ROOT 环境变量指定的 jcl-cobol-git 目录
|
||||
验证目标: 28 transactions → 20 valid + 8 rejected → 6 cards → ¥48,250.20 total
|
||||
"""
|
||||
import sys, os
|
||||
sys.path.insert(0, r"D:\cobol-java\v3-gstack-code-gen")
|
||||
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
from data.field_tree import Field, FieldTree
|
||||
from comparator.cobol_binary_reader import CobolBinaryReader
|
||||
from comparator.normalizer import Normalizer
|
||||
@@ -14,7 +14,10 @@ from comparator.field_compare import compare_field
|
||||
from comparator.aligner import align_records
|
||||
from preprocessor import CopybookPreprocessor
|
||||
|
||||
GOLDEN = Path(r"D:\cobol-java\jcl-cobol-git")
|
||||
GOLDEN = Path(
|
||||
os.environ.get("COBOL_GIT_ROOT",
|
||||
Path(__file__).resolve().parent.parent.parent / "jcl-cobol-git")
|
||||
)
|
||||
|
||||
|
||||
# ── Test 1: TXCPY COPYBOOK 结构验证 ──
|
||||
|
||||
Reference in New Issue
Block a user