feat:优化了enhancedmultiFileRunner.ts的逻辑,使其能兼容其他功能
This commit is contained in:
parent
7ea49fb495
commit
76eab0ff6c
@ -183,17 +183,11 @@ export const runMultiFileCodeWithOptionalTestData = async (params: {
|
|||||||
return { error: `Execution failed with exit code ${exitCode.StatusCode}. Output: ${output}` };
|
return { error: `Execution failed with exit code ${exitCode.StatusCode}. Output: ${output}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果指定了结果输出文件和正确答案文件,执行比较
|
// 如果指定了结果输出文件,读取其内容
|
||||||
if (resultOutputFile && expectedAnswerFiles && expectedAnswerFiles.length > 0) {
|
if (resultOutputFile) {
|
||||||
const resultFilePath = path.join(tempDir, resultOutputFile);
|
const resultFilePath = path.join(tempDir, resultOutputFile);
|
||||||
|
const resultFileContent = await fs.readFile(resultFilePath, 'utf-8');
|
||||||
const comparisonResults = await Promise.all(expectedAnswerFiles.map(async (expectedFile) => {
|
return { output, resultFileContent };
|
||||||
const expectedFilePath = path.join(tempDir, testDataPath, expectedFile);
|
|
||||||
const isMatch = await compareResults(resultFilePath, expectedFilePath);
|
|
||||||
return { expectedFile, isMatch };
|
|
||||||
}));
|
|
||||||
|
|
||||||
return { output, comparisonResults };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { output };
|
return { output };
|
||||||
|
@ -1,19 +1,21 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import path from 'path';
|
|
||||||
import fs from 'fs';
|
|
||||||
import { runMultiFileCodeWithOptionalTestData } from '@/actions/enhancedmultiFileRunner';
|
import { runMultiFileCodeWithOptionalTestData } from '@/actions/enhancedmultiFileRunner';
|
||||||
|
|
||||||
const TestPage = () => {
|
const TestPage = () => {
|
||||||
const [code, setCode] = useState<string>('');
|
const [code, setCode] = useState<string>('');
|
||||||
const [language, setLanguage] = useState<string>('c');
|
const [language, setLanguage] = useState<string>('c');
|
||||||
const [result, setResult] = useState<string>('');
|
const [result, setResult] = useState<string>('');
|
||||||
|
const [consoleOutput, setConsoleOutput] = useState<string>('');
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
|
const [resultFileContent, setResultFileContent] = useState<string>('');
|
||||||
|
|
||||||
const handleRun = async () => {
|
const handleRun = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setResult('');
|
setResult('');
|
||||||
|
setConsoleOutput('');
|
||||||
|
setResultFileContent('');
|
||||||
|
|
||||||
const testFiles = [
|
const testFiles = [
|
||||||
{ name: 'input1.txt', content: '1 2 3' },
|
{ name: 'input1.txt', content: '1 2 3' },
|
||||||
@ -39,10 +41,21 @@ const TestPage = () => {
|
|||||||
resultOutputFile: 'result.txt',
|
resultOutputFile: 'result.txt',
|
||||||
});
|
});
|
||||||
|
|
||||||
setResult(JSON.stringify(result, null, 2));
|
if (result.error) {
|
||||||
|
setConsoleOutput(result.error);
|
||||||
|
} else {
|
||||||
|
setConsoleOutput(result.output);
|
||||||
|
if (result.comparisonResults) {
|
||||||
|
setResult(JSON.stringify(result.comparisonResults, null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置 result.txt 的内容
|
||||||
|
if (result.resultFileContent) {
|
||||||
|
setResultFileContent(result.resultFileContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 使用类型断言确保 error 是 Error 类型
|
setConsoleOutput(`Error: ${(error as Error).message}`);
|
||||||
setResult(`Error: ${(error as Error).message}`);
|
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
@ -90,6 +103,20 @@ const TestPage = () => {
|
|||||||
>
|
>
|
||||||
{loading ? 'Running...' : 'Run Code'}
|
{loading ? 'Running...' : 'Run Code'}
|
||||||
</button>
|
</button>
|
||||||
|
<div style={{ marginTop: '20px' }}>
|
||||||
|
<h2>Console Output</h2>
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
background: '#f4f4f4',
|
||||||
|
padding: '10px',
|
||||||
|
border: '1px solid #ddd',
|
||||||
|
borderRadius: '5px',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{consoleOutput || 'No console output yet.'}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
{result && (
|
||||||
<div style={{ marginTop: '20px' }}>
|
<div style={{ marginTop: '20px' }}>
|
||||||
<h2>Result</h2>
|
<h2>Result</h2>
|
||||||
<pre
|
<pre
|
||||||
@ -103,6 +130,22 @@ const TestPage = () => {
|
|||||||
{result || 'No results yet.'}
|
{result || 'No results yet.'}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
|
{resultFileContent && (
|
||||||
|
<div style={{ marginTop: '20px' }}>
|
||||||
|
<h2>Result File Content (result.txt)</h2>
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
background: '#f4f4f4',
|
||||||
|
padding: '10px',
|
||||||
|
border: '1px solid #ddd',
|
||||||
|
borderRadius: '5px',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{resultFileContent || 'No result file content yet.'}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user