Jaiwincr7 commited on
Commit
85b1c76
·
1 Parent(s): 74054d6

FIX: Resolved UnboundLocalError in test_case by correctly invoking LLM and assigning to 'test_cases'.

Browse files
Files changed (1) hide show
  1. main.py +35 -17
main.py CHANGED
@@ -51,43 +51,61 @@ test_prompt = ChatPromptTemplate.from_messages(
51
  )
52
 
53
  def test_case(code):
54
- # ... (LLM generation and cleaning remains the same) ...
 
 
 
 
 
 
55
 
56
- # -----------------------------------------------------------
57
- # (Existing cleaning and check)
 
 
58
  test_cases = re.sub(r"```.*?```", "", test_cases, flags=re.DOTALL)
59
  test_cases = re.sub(r"```", "", test_cases)
60
  test_cases = test_cases.strip()
61
 
62
- # If the LLM returns nothing, force a known output
63
  if not test_cases:
64
- test_cases = "Error: Test case generation failed or returned empty content. Check LLM output in logs."
65
- # -----------------------------------------------------------
 
66
 
 
67
  safe_text = test_cases
 
68
  pdf = FPDF()
69
  pdf.add_page()
70
 
71
- # ... (FPDF Unicode font setup remains the same) ...
72
-
 
 
 
 
 
 
 
 
73
  pdf.multi_cell(0, 10, txt="--- Generated Test Cases ---", align='C')
74
  pdf.multi_cell(0, 10, txt=safe_text)
75
 
76
-
77
  # -----------------------------------------------------------
78
- # CRITICAL CHANGE: Get bytes object directly from FPDF2
79
  try:
80
- # dest='S' returns a string in FPDF2. We need to encode this string.
81
  pdf_string = pdf.output(dest='S')
82
- pdf_bytes = pdf_string.encode('latin-1')
 
 
83
 
84
- # Check to ensure the file is not empty before returning
85
  if len(pdf_bytes) < 100:
86
  print("[CRITICAL LOG] FPDF generated very small file, likely failed.")
87
- # Return a non-empty, guaranteed failure PDF to force the button to display
88
- # This is a temporary diagnostic tool.
89
 
90
- # Recreate PDF with ONLY the error message
91
  error_pdf = FPDF()
92
  error_pdf.add_page()
93
  error_pdf.set_font("Arial", size=12)
@@ -98,7 +116,7 @@ def test_case(code):
98
 
99
  except Exception as e:
100
  print(f"[FATAL LOG] PDF output failed with error: {e}")
101
- # Return a guaranteed failure PDF to force button display
102
  error_pdf = FPDF()
103
  error_pdf.add_page()
104
  error_pdf.set_font("Arial", size=12)
 
51
  )
52
 
53
  def test_case(code):
54
+
55
+ # ----------------------------------------------------
56
+ # FIX: Define and INVOKE the test_chain to get the LLM output.
57
+ # This resolves the UnboundLocalError.
58
+ test_chain = test_prompt | llm | StrOutputParser()
59
+ test_cases = test_chain.invoke({"code": code})
60
+ # ----------------------------------------------------
61
 
62
+ print("\n[LOG] 1. LLM Raw Output Length:", len(test_cases))
63
+
64
+ # Aggressive cleaning
65
+ # Remove markdown blocks (```...```) and standalone ``` markers
66
  test_cases = re.sub(r"```.*?```", "", test_cases, flags=re.DOTALL)
67
  test_cases = re.sub(r"```", "", test_cases)
68
  test_cases = test_cases.strip()
69
 
70
+ # Guardrail: If the LLM returns nothing, force a known output
71
  if not test_cases:
72
+ test_cases = "Error: Test case generation failed or returned empty content."
73
+
74
+ print("\n[LOG] 2. Cleaned Text (for PDF):", test_cases)
75
 
76
+ # Pass the clean UTF-8 string directly (resolves FPDF encoding issues)
77
  safe_text = test_cases
78
+
79
  pdf = FPDF()
80
  pdf.add_page()
81
 
82
+ # FPDF FIX: Add and use a Unicode-compatible font (DejaVuSans)
83
+ try:
84
+ # Path where the font is installed via the Dockerfile's apt-get command
85
+ pdf.add_font("DejaVuSans", style="", fname="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf")
86
+ pdf.set_font("DejaVuSans", size=12)
87
+ except Exception as e:
88
+ print(f"[LOG] FPDF Font Error: {e}. Falling back to Arial.")
89
+ pdf.set_font("Arial", size=12)
90
+
91
+ # Set title and content cell
92
  pdf.multi_cell(0, 10, txt="--- Generated Test Cases ---", align='C')
93
  pdf.multi_cell(0, 10, txt=safe_text)
94
 
 
95
  # -----------------------------------------------------------
96
+ # CRITICAL CHANGE: Get bytes object directly from FPDF2 with error handling
97
  try:
98
+ # dest='S' returns a string in FPDF2. We encode this string.
99
  pdf_string = pdf.output(dest='S')
100
+ pdf_bytes = pdf_string.encode('latin-1', 'replace')
101
+
102
+ print("\n[LOG] 3. PDF Bytes Length:", len(pdf_bytes))
103
 
104
+ # Check to ensure the file is not empty (e.g., must be > 100 bytes)
105
  if len(pdf_bytes) < 100:
106
  print("[CRITICAL LOG] FPDF generated very small file, likely failed.")
 
 
107
 
108
+ # Fallback PDF: Creates a new PDF with the error message
109
  error_pdf = FPDF()
110
  error_pdf.add_page()
111
  error_pdf.set_font("Arial", size=12)
 
116
 
117
  except Exception as e:
118
  print(f"[FATAL LOG] PDF output failed with error: {e}")
119
+ # Fatal Fallback PDF: Creates a new PDF with the fatal error message
120
  error_pdf = FPDF()
121
  error_pdf.add_page()
122
  error_pdf.set_font("Arial", size=12)