|
|
const { exec } = require('child_process');
|
|
|
const path = require('path');
|
|
|
|
|
|
// 测试Flake8的实际输出
|
|
|
function testFlake8() {
|
|
|
const testFile = path.join(__dirname, 'test_sample.py');
|
|
|
|
|
|
console.log('测试文件路径:', testFile);
|
|
|
|
|
|
// 测试1: 默认格式
|
|
|
console.log('\n=== 测试1: flake8 默认格式 ===');
|
|
|
exec(`flake8 "${testFile}"`, (error, stdout, stderr) => {
|
|
|
console.log('默认格式输出:');
|
|
|
console.log('stdout:', stdout);
|
|
|
console.log('stderr:', stderr);
|
|
|
});
|
|
|
|
|
|
// 测试2: JSON格式
|
|
|
console.log('\n=== 测试2: flake8 --format=json ===');
|
|
|
exec(`flake8 --format=json "${testFile}"`, (error, stdout, stderr) => {
|
|
|
console.log('JSON格式输出:');
|
|
|
console.log('stdout:', stdout);
|
|
|
console.log('stderr:', stderr);
|
|
|
|
|
|
// 尝试解析JSON
|
|
|
try {
|
|
|
if (stdout.trim()) {
|
|
|
const parsed = JSON.parse(stdout);
|
|
|
console.log('JSON解析成功:', parsed);
|
|
|
} else {
|
|
|
console.log('输出为空');
|
|
|
}
|
|
|
} catch (e) {
|
|
|
console.log('JSON解析失败:', e.message);
|
|
|
console.log('原始输出长度:', stdout.length);
|
|
|
console.log('输出前50个字符:', stdout.substring(0, 50));
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 测试3: quiet格式(不使用JSON)
|
|
|
console.log('\n=== 测试3: flake8 --quiet ===');
|
|
|
exec(`flake8 --quiet "${testFile}"`, (error, stdout, stderr) => {
|
|
|
console.log('quiet格式输出:');
|
|
|
console.log('stdout:', stdout);
|
|
|
console.log('stderr:', stderr);
|
|
|
});
|
|
|
|
|
|
// 测试4: 使用临时文件路径(模拟实际使用场景)
|
|
|
const tempFile = 'C:\\Users\\张洋\\AppData\\Local\\Temp\\test_temp_file.py';
|
|
|
console.log('\n=== 测试4: 使用临时文件路径 ===');
|
|
|
exec(`flake8 --quiet "${testFile}"`, (error, stdout, stderr) => {
|
|
|
console.log('临时文件格式输出:');
|
|
|
console.log('stdout:', stdout);
|
|
|
console.log('stderr:', stderr);
|
|
|
|
|
|
// 模拟我们的解析逻辑
|
|
|
if (stdout.trim()) {
|
|
|
const lines = stdout.trim().split('\n').filter(line => line.trim());
|
|
|
console.log('解析后的行数:', lines.length);
|
|
|
|
|
|
for (const line of lines) {
|
|
|
console.log('处理行:', line);
|
|
|
const match = line.match(/^(.+?):(\d+):(\d+):\s*([A-Z]\d+)\s*(.+)$/);
|
|
|
if (match) {
|
|
|
console.log('解析成功:', {
|
|
|
file: match[1],
|
|
|
line: parseInt(match[2]),
|
|
|
column: parseInt(match[3]),
|
|
|
code: match[4],
|
|
|
message: match[5]
|
|
|
});
|
|
|
} else {
|
|
|
console.log('解析失败:', line);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
testFlake8();
|