|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import * as vscode from 'vscode'; |
|
|
import {Uri} from 'vscode'; |
|
|
import {println} from './logger'; |
|
|
import { |
|
|
getOSSFuzzCloudURL, |
|
|
getLocalOutBuildDir, |
|
|
downloadRemoteURL, |
|
|
} from './utils'; |
|
|
|
|
|
const path = require('path'); |
|
|
let isCodeCoverageEnabled = false; |
|
|
|
|
|
|
|
|
const codeCoveredLineDecorationType = |
|
|
vscode.window.createTextEditorDecorationType({ |
|
|
backgroundColor: '#184916', |
|
|
overviewRulerColor: 'blue', |
|
|
overviewRulerLane: vscode.OverviewRulerLane.Right, |
|
|
light: { |
|
|
|
|
|
borderColor: 'darkblue', |
|
|
}, |
|
|
dark: { |
|
|
|
|
|
borderColor: 'lightblue', |
|
|
}, |
|
|
}); |
|
|
|
|
|
const missingLineDecorationType = vscode.window.createTextEditorDecorationType({ |
|
|
backgroundColor: '#6C2B34', |
|
|
overviewRulerColor: 'blue', |
|
|
overviewRulerLane: vscode.OverviewRulerLane.Right, |
|
|
light: { |
|
|
|
|
|
borderColor: 'darkblue', |
|
|
}, |
|
|
dark: { |
|
|
|
|
|
borderColor: 'lightblue', |
|
|
}, |
|
|
}); |
|
|
|
|
|
export async function loadSummaryJsonCoverage( |
|
|
context: vscode.ExtensionContext, |
|
|
codeCoverageFile: Uri |
|
|
) { |
|
|
const coverageSummaryRawJson = await vscode.workspace.openTextDocument( |
|
|
codeCoverageFile |
|
|
); |
|
|
const jsonCodeCoverage = JSON.parse(coverageSummaryRawJson.getText()); |
|
|
return jsonCodeCoverage; |
|
|
} |
|
|
|
|
|
export async function compareLocalToRemoteCoverage( |
|
|
context: vscode.ExtensionContext, |
|
|
projectName: string |
|
|
) { |
|
|
println('Checking the file matching'); |
|
|
|
|
|
const urlString = |
|
|
(await getOSSFuzzCloudURL(projectName)) + '/linux/summary.json'; |
|
|
|
|
|
println('URL: ' + urlString); |
|
|
const codeCoverageFile: false | vscode.Uri = await downloadRemoteURL( |
|
|
urlString, |
|
|
'summary.json', |
|
|
context |
|
|
); |
|
|
if (!codeCoverageFile) { |
|
|
println('Could not get the coverage summary file'); |
|
|
return; |
|
|
} |
|
|
const remoteCoverage = await loadSummaryJsonCoverage( |
|
|
context, |
|
|
codeCoverageFile |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const localSummaryCovPath = |
|
|
(await getLocalOutBuildDir(projectName)) + '/report/linux/summary.json'; |
|
|
const localCodeCoverage = await loadSummaryJsonCoverage( |
|
|
context, |
|
|
vscode.Uri.file(localSummaryCovPath) |
|
|
); |
|
|
|
|
|
for (let i = 0; i < localCodeCoverage.data[0].files.length; i++) { |
|
|
for (let j = 0; j < remoteCoverage.data[0].files.length; j++) { |
|
|
|
|
|
const localFileData = localCodeCoverage.data[0].files[i]; |
|
|
const remoteFileData = remoteCoverage.data[0].files[j]; |
|
|
|
|
|
|
|
|
if (localFileData.filename === remoteFileData.filename) { |
|
|
const remoteFuncCount = remoteFileData.summary.functions.count; |
|
|
const localFuncCount = localFileData.summary.functions.count; |
|
|
|
|
|
if (localFuncCount > remoteFuncCount) { |
|
|
println( |
|
|
'Coverage improved in :' + |
|
|
localFileData.filename + |
|
|
' [' + |
|
|
localFuncCount + |
|
|
' : ' + |
|
|
remoteFuncCount + |
|
|
']' |
|
|
); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function loadCoverageIntoWorkspace( |
|
|
context: vscode.ExtensionContext, |
|
|
codeCoverageFile: Uri |
|
|
) { |
|
|
isCodeCoverageEnabled = true; |
|
|
|
|
|
const doc3 = await vscode.workspace.openTextDocument(codeCoverageFile); |
|
|
const jsonCodeCoverageObj3 = JSON.parse(doc3.getText()); |
|
|
|
|
|
const codeCoverageMappingWithCoverage = new Map(); |
|
|
const codeCoverageMapMissingCoverage = new Map(); |
|
|
|
|
|
Object.entries(jsonCodeCoverageObj3['files']).forEach(entry => { |
|
|
const [key, value] = entry; |
|
|
println(key); |
|
|
const filename = path.parse(key).base; |
|
|
println('Filename base: ' + filename); |
|
|
const objectDictionary: any = value as any; |
|
|
const linesWithCodeCoverage: unknown[] = []; |
|
|
println(objectDictionary['executed_lines']); |
|
|
Object.entries(objectDictionary['executed_lines']).forEach(entryInner => { |
|
|
const lineNumber = entryInner[1]; |
|
|
|
|
|
linesWithCodeCoverage.push(lineNumber); |
|
|
}); |
|
|
codeCoverageMappingWithCoverage.set(filename, linesWithCodeCoverage); |
|
|
|
|
|
const linesMissingCodeCoverage: unknown[] = []; |
|
|
Object.entries(objectDictionary['missing_lines']).forEach(entryInner => { |
|
|
const lineNumber = entryInner[1]; |
|
|
|
|
|
linesMissingCodeCoverage.push(lineNumber); |
|
|
}); |
|
|
codeCoverageMapMissingCoverage.set(filename, linesMissingCodeCoverage); |
|
|
}); |
|
|
println('=========>'); |
|
|
|
|
|
println('Enabling code coverage decorator'); |
|
|
println('decorator sample is activated'); |
|
|
|
|
|
let timeout: NodeJS.Timer | undefined = undefined; |
|
|
|
|
|
|
|
|
|
|
|
let activeEditor = vscode.window.activeTextEditor; |
|
|
|
|
|
function updateDecorations( |
|
|
linesWithCodeCoverage: any, |
|
|
linesWithoNoCodeCoverage: any |
|
|
) { |
|
|
if (!isCodeCoverageEnabled) { |
|
|
return; |
|
|
} |
|
|
if (!activeEditor) { |
|
|
return; |
|
|
} |
|
|
println('Filename'); |
|
|
println(activeEditor.document.fileName); |
|
|
|
|
|
|
|
|
const nameOfCurrentFile = path.parse(activeEditor.document.fileName).base; |
|
|
|
|
|
println('Base filename: ' + nameOfCurrentFile); |
|
|
println('Done filename'); |
|
|
const lineNumbersWithCoverage: vscode.DecorationOptions[] = []; |
|
|
const missingLineNumbers: vscode.DecorationOptions[] = []; |
|
|
|
|
|
if (linesWithCodeCoverage.has(nameOfCurrentFile)) { |
|
|
println('Has this file'); |
|
|
const elemWithCov = linesWithCodeCoverage.get(nameOfCurrentFile); |
|
|
for (let idx = 0; idx < elemWithCov.length; idx++) { |
|
|
const lineNo = elemWithCov[idx]; |
|
|
println('Setting up: ' + lineNo); |
|
|
lineNumbersWithCoverage.push({ |
|
|
range: new vscode.Range(lineNo - 1, 0, lineNo, 0), |
|
|
}); |
|
|
} |
|
|
|
|
|
const elemNoCov = linesWithoNoCodeCoverage.get(nameOfCurrentFile); |
|
|
for (let idx = 0; idx < elemNoCov.length; idx++) { |
|
|
const lineNo = elemNoCov[idx]; |
|
|
println('Setting up: ' + lineNo); |
|
|
missingLineNumbers.push({ |
|
|
range: new vscode.Range(lineNo - 1, 0, lineNo, 0), |
|
|
}); |
|
|
} |
|
|
} else { |
|
|
println('Does not have this file'); |
|
|
} |
|
|
|
|
|
activeEditor.setDecorations( |
|
|
codeCoveredLineDecorationType, |
|
|
lineNumbersWithCoverage |
|
|
); |
|
|
activeEditor.setDecorations(missingLineDecorationType, missingLineNumbers); |
|
|
|
|
|
} |
|
|
|
|
|
function triggerUpdateDecorations( |
|
|
throttle = false, |
|
|
covMap: any, |
|
|
covMisMap: any |
|
|
) { |
|
|
if (timeout) { |
|
|
clearTimeout(timeout); |
|
|
timeout = undefined; |
|
|
} |
|
|
if (throttle) { |
|
|
|
|
|
updateDecorations(covMap, covMisMap); |
|
|
} else { |
|
|
updateDecorations(covMap, covMisMap); |
|
|
} |
|
|
} |
|
|
|
|
|
if (activeEditor) { |
|
|
triggerUpdateDecorations( |
|
|
false, |
|
|
codeCoverageMappingWithCoverage, |
|
|
codeCoverageMapMissingCoverage |
|
|
); |
|
|
} |
|
|
|
|
|
vscode.window.onDidChangeActiveTextEditor( |
|
|
editor => { |
|
|
activeEditor = editor; |
|
|
if (editor) { |
|
|
triggerUpdateDecorations( |
|
|
false, |
|
|
codeCoverageMappingWithCoverage, |
|
|
codeCoverageMapMissingCoverage |
|
|
); |
|
|
} |
|
|
}, |
|
|
null, |
|
|
context.subscriptions |
|
|
); |
|
|
|
|
|
vscode.workspace.onDidChangeTextDocument( |
|
|
event => { |
|
|
if (activeEditor && event.document === activeEditor.document) { |
|
|
triggerUpdateDecorations( |
|
|
true, |
|
|
codeCoverageMappingWithCoverage, |
|
|
codeCoverageMapMissingCoverage |
|
|
); |
|
|
} |
|
|
}, |
|
|
null, |
|
|
context.subscriptions |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function clearCoverage() { |
|
|
|
|
|
const activeEditor = vscode.window.activeTextEditor; |
|
|
isCodeCoverageEnabled = false; |
|
|
if (activeEditor) { |
|
|
activeEditor.setDecorations(codeCoveredLineDecorationType, []); |
|
|
activeEditor.setDecorations(missingLineDecorationType, []); |
|
|
} |
|
|
} |
|
|
|