-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeExec.js
More file actions
207 lines (163 loc) · 7.21 KB
/
codeExec.js
File metadata and controls
207 lines (163 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* eslint-disable no-unused-vars */
const fs = require('fs');
const path = require('path');
const vscode = require('vscode');
const {spawn} = require('child_process');
function displayResults(allTestsPassed) {
if (allTestsPassed) {
vscode.window.showInformationMessage('All test cases passed! 🎉');
} else {
vscode.window.showErrorMessage('Some test cases failed. Please check your code. ❌');
}
}
function updateStatusBar(allTestsPassed) {
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
statusBarItem.text = allTestsPassed ? '$(check) All tests passed!' : '$(x) Some tests failed.';
statusBarItem.tooltip = 'Click to view details';
statusBarItem.show();
// statusBarItem.command = 'extension.showTestDetails'; // replace with your command ID
}
function writeResultsToOutputPanel(results,outputs,counter) {
const outputChannel = vscode.window.createOutputChannel('Test Results');
outputChannel.clear();
outputChannel.appendLine('========== Test Results ==========');
results.forEach((result, index) => {
const { testCase, passed } = result;
const {expectedOutput,actualOutput} = outputs[index];
if(passed){
outputChannel.appendLine(`Test Case ${index + 1}: ✅ Passed`);
}
else{
outputChannel.appendLine(`Test Case ${index + 1}: ❌ Failed`);
outputChannel.appendLine(`Expected: ${expectedOutput}`);
outputChannel.appendLine(` Actual : ${actualOutput}`);
}
});
outputChannel.show(); // Focus on the output panel
}
async function runCodeCpp(userSolutionFile,executableFile,problemFolderPath) {
// Compile the C++ code
const compileProcess = spawn('g++', [userSolutionFile, '-o', executableFile]);
await new Promise((resolve, reject) => {
compileProcess.on('close', (code) => {
if (code === 0) {
console.log('Compilation successful.');
resolve();
} else {
console.error('Compilation failed.');
reject(new Error('Compilation error.'));
}
});
compileProcess.stderr.on('data', (data) => {
console.error(`Compilation Error: ${data.toString()}`);
});
});
const inputFiles = fs.readdirSync(problemFolderPath).filter(file => file.startsWith('ip') && file.endsWith('.txt'));
let allTestsPassed = true;
const results = []
const outputs = []
for (const inputFile of inputFiles) {
const testCaseNumber = inputFile.match(/\d+/)[0]; // Extract test case number
const expectedOutputFile = `op${testCaseNumber}.txt`;
const inputPath = path.join(problemFolderPath, inputFile);
const outputPath = path.join(problemFolderPath, expectedOutputFile);
if (!fs.existsSync(outputPath)) {
console.error(`Expected output file ${expectedOutputFile} not found.`);
continue;
}
const inputContent = fs.readFileSync(inputPath, 'utf-8');
const expectedOutput = fs.readFileSync(outputPath, 'utf-8').trim();
console.log(inputContent);
console.log(expectedOutput);
console.log(`Running test case ${testCaseNumber}...`);
const child = spawn(executableFile);
let actualOutput = '';
child.stdout.on('data', (data) => {
actualOutput += data.toString();
});
child.stderr.on('data', (data) => {
console.error(`Error in test case ${testCaseNumber}: ${data}`);
});
child.on('close', (code) => {
actualOutput = actualOutput.trim();
const passed = (actualOutput === expectedOutput);
outputs.push({actualOutput,expectedOutput});
if (passed) {
console.log(`Test case ${testCaseNumber} passed!`);
} else {
console.error(`Test case ${testCaseNumber} failed.`);
console.error(`Expected: ${expectedOutput}`);
console.error(`Got: ${actualOutput}`);
allTestsPassed = false;
}
results.push({ testCase: testCaseNumber, passed });
});
// Send input to the program
child.stdin.write(inputContent);
child.stdin.end();
// Wait for the process to finish before running the next test case
await new Promise((resolve) => child.on('close', resolve));
}
// Display Results
displayResults(allTestsPassed);
updateStatusBar(allTestsPassed);
writeResultsToOutputPanel(results,outputs);
}
async function runCodePython(scriptPath,problemFolderPath) {
const inputFiles = fs.readdirSync(problemFolderPath).filter(file => file.startsWith('ip') && file.endsWith('.txt'));
let allTestsPassed = true;
const results = []
const outputs = []
for (const inputFile of inputFiles) {
const testCaseNumber = inputFile.match(/\d+/)[0]; // Extract test case number
const expectedOutputFile = `op${testCaseNumber}.txt`;
const inputPath = path.join(problemFolderPath, inputFile);
const outputPath = path.join(problemFolderPath, expectedOutputFile);
if (!fs.existsSync(outputPath)) {
console.error(`Expected output file ${expectedOutputFile} not found.`);
continue;
}
const inputContent = fs.readFileSync(inputPath, 'utf-8');
const expectedOutput = fs.readFileSync(outputPath, 'utf-8').trim();
console.log(`Running test case ${testCaseNumber}...`);
let actualOutput = '';
let errorOutput = '';
await new Promise((resolve, reject) => {
const process = spawn('python', [scriptPath]);
process.stdout.on('data', (data) => {
actualOutput += data.toString();
});
process.stderr.on('data', (data) => {
errorOutput += data.toString();
});
process.on('close', (code) => {
if (errorOutput) {
console.error(`stderr: ${errorOutput}`);
reject(new Error(`Test case ${testCaseNumber} encountered an error.`));
} else {
actualOutput = actualOutput.trim();
const passed = (actualOutput === expectedOutput);
outputs.push({expectedOutput,actualOutput});
if (passed) {
console.log(`Test case ${testCaseNumber} passed!`);
} else {
console.error(`Test case ${testCaseNumber} failed.`);
console.error(`Expected: ${expectedOutput}`);
console.error(`Got: ${actualOutput}`);
allTestsPassed = false;
}
results.push({ testCase: testCaseNumber, passed });
resolve();
}
});
// Send input to the program
process.stdin.write(inputContent);
process.stdin.end();
});
}
// Display Results
displayResults(allTestsPassed);
updateStatusBar(allTestsPassed);
writeResultsToOutputPanel(results,outputs);
}
module.exports = {runCodeCpp,runCodePython};