feat: diff blocks
This commit is contained in:
		
							parent
							
								
									fd69aebac5
								
							
						
					
					
						commit
						756a531bac
					
				|  | @ -39,13 +39,13 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( | ||||||
| 			return nil, true, err | 			return nil, true, err | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		comment = fmt.Sprintf( | ||||||
|  | 			"executor status: run time: %d ns, memory: %d bytes\n", | ||||||
|  | 			result.RunTime, result.Memory, | ||||||
|  | 		) | ||||||
| 		// If no difference, assign score
 | 		// If no difference, assign score
 | ||||||
| 		if string(stdout) == result.Files["stdout"] { | 		if string(stdout) == result.Files["stdout"] { | ||||||
| 			score = caseConf.Score | 			score = caseConf.Score | ||||||
| 			comment = fmt.Sprintf( |  | ||||||
| 				"executor status: run time: %d ns, memory: %d bytes", |  | ||||||
| 				result.RunTime, result.Memory, |  | ||||||
| 			) |  | ||||||
| 		} else { | 		} else { | ||||||
| 			// Convert stdout to string and split by lines
 | 			// Convert stdout to string and split by lines
 | ||||||
| 			stdoutLines := strings.Split(string(stdout), "\n") | 			stdoutLines := strings.Split(string(stdout), "\n") | ||||||
|  | @ -54,14 +54,12 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( | ||||||
| 			// Find the first difference
 | 			// Find the first difference
 | ||||||
| 			diffIndex := findFirstDifferenceIndex(stdoutLines, resultLines) | 			diffIndex := findFirstDifferenceIndex(stdoutLines, resultLines) | ||||||
| 			if diffIndex != -1 { | 			if diffIndex != -1 { | ||||||
| 				// Get the surrounding lines from both stdout and result
 | 				// Generate diff block with surrounding context
 | ||||||
| 				stdoutContext := getContextLines(stdoutLines, diffIndex, 10) | 				diffOutput := generateDiffWithContext(stdoutLines, resultLines, diffIndex, 10) | ||||||
| 				resultContext := getContextLines(resultLines, diffIndex, 10) | 				comment += fmt.Sprintf( | ||||||
| 				comment = comment + fmt.Sprintf( | 					"difference found at line %d:\n```diff\n%s```", | ||||||
| 					"difference found at line %d:\nExpected output:\n%s\nActual output:\n%s", |  | ||||||
| 					diffIndex+1, | 					diffIndex+1, | ||||||
| 					resultContext, | 					diffOutput, | ||||||
| 					stdoutContext, |  | ||||||
| 				) | 				) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | @ -90,20 +88,89 @@ func findFirstDifferenceIndex(stdoutLines, resultLines []string) int { | ||||||
| 	return -1 | 	return -1 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // getContextLines returns the surrounding lines of the specified index.
 | // generateDiffWithContext creates a diff block with surrounding context from stdout and result.
 | ||||||
| func getContextLines(lines []string, index, contextSize int) string { | func generateDiffWithContext(stdoutLines, resultLines []string, index, contextSize int) string { | ||||||
|  | 	var diffBuilder strings.Builder | ||||||
|  | 
 | ||||||
| 	start := index - contextSize | 	start := index - contextSize | ||||||
| 	if start < 0 { | 	if start < 0 { | ||||||
| 		start = 0 | 		start = 0 | ||||||
| 	} | 	} | ||||||
| 	end := index + contextSize + 1 | 	end := index + contextSize + 1 | ||||||
| 	if end > len(lines) { | 	if end > len(stdoutLines) { | ||||||
| 		end = len(lines) | 		end = len(stdoutLines) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	var context strings.Builder | 	// Adding context before the diff
 | ||||||
| 	for i := start; i < end; i++ { | 	for i := start; i < index; i++ { | ||||||
| 		context.WriteString(fmt.Sprintf("%d: %s\n", i+1, lines[i])) | 		if i < len(stdoutLines) || i < len(resultLines) { | ||||||
|  | 			stdoutLine := "" | ||||||
|  | 			if i < len(stdoutLines) { | ||||||
|  | 				stdoutLine = stdoutLines[i] | ||||||
| 			} | 			} | ||||||
| 	return context.String() | 			resultLine := "" | ||||||
|  | 			if i < len(resultLines) { | ||||||
|  | 				resultLine = resultLines[i] | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			if stdoutLine != resultLine { | ||||||
|  | 				if stdoutLine != "" { | ||||||
|  | 					diffBuilder.WriteString(fmt.Sprintf("- %s\n", stdoutLine)) | ||||||
|  | 				} | ||||||
|  | 				if resultLine != "" { | ||||||
|  | 					diffBuilder.WriteString(fmt.Sprintf("+ %s\n", resultLine)) | ||||||
|  | 				} | ||||||
|  | 			} else { | ||||||
|  | 				diffBuilder.WriteString(fmt.Sprintf("  %s\n", stdoutLine)) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Adding the diff line
 | ||||||
|  | 	if index < len(stdoutLines) || index < len(resultLines) { | ||||||
|  | 		stdoutLine := "" | ||||||
|  | 		if index < len(stdoutLines) { | ||||||
|  | 			stdoutLine = stdoutLines[index] | ||||||
|  | 		} | ||||||
|  | 		resultLine := "" | ||||||
|  | 		if index < len(resultLines) { | ||||||
|  | 			resultLine = resultLines[index] | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if stdoutLine != resultLine { | ||||||
|  | 			if stdoutLine != "" { | ||||||
|  | 				diffBuilder.WriteString(fmt.Sprintf("- %s\n", stdoutLine)) | ||||||
|  | 			} | ||||||
|  | 			if resultLine != "" { | ||||||
|  | 				diffBuilder.WriteString(fmt.Sprintf("+ %s\n", resultLine)) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Adding context after the diff
 | ||||||
|  | 	for i := index + 1; i < end; i++ { | ||||||
|  | 		if i < len(stdoutLines) || i < len(resultLines) { | ||||||
|  | 			stdoutLine := "" | ||||||
|  | 			if i < len(stdoutLines) { | ||||||
|  | 				stdoutLine = stdoutLines[i] | ||||||
|  | 			} | ||||||
|  | 			resultLine := "" | ||||||
|  | 			if i < len(resultLines) { | ||||||
|  | 				resultLine = resultLines[i] | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			if stdoutLine != resultLine { | ||||||
|  | 				if stdoutLine != "" { | ||||||
|  | 					diffBuilder.WriteString(fmt.Sprintf("- %s\n", stdoutLine)) | ||||||
|  | 				} | ||||||
|  | 				if resultLine != "" { | ||||||
|  | 					diffBuilder.WriteString(fmt.Sprintf("+ %s\n", resultLine)) | ||||||
|  | 				} | ||||||
|  | 			} else { | ||||||
|  | 				diffBuilder.WriteString(fmt.Sprintf("  %s\n", stdoutLine)) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return diffBuilder.String() | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 zzjc1234
						zzjc1234