diff --git a/src/coverage/coverageService.ts b/src/coverage/coverageService.ts index 4bf0b0fb7..dd902e9b2 100644 --- a/src/coverage/coverageService.ts +++ b/src/coverage/coverageService.ts @@ -104,7 +104,7 @@ export async function buildJson(dirPath: vscode.Uri) { ], dirPath.fsPath.replace(/\\/g, "/") ); - return JSON.parse(result); + return JSON.stringify(result); } export async function buildHtml(dirPath: vscode.Uri) { diff --git a/src/test/suite/testCoverage.test.ts b/src/test/suite/testCoverage.test.ts index aa789cd3d..c4de00bff 100644 --- a/src/test/suite/testCoverage.test.ts +++ b/src/test/suite/testCoverage.test.ts @@ -1,19 +1,61 @@ import * as assert from "assert"; import * as vscode from "vscode"; -import { json } from "stream/consumers"; -import { getGcovExecutable, buildJson } from "../../coverage/coverageService"; -import { CoverageRenderer } from "../../coverage/renderer"; +import { join } from "path"; +import { + getGcovExecutable, + buildJson, + getGcovFilterPaths, + buildHtml, + generateCoverageForEditors, +} from "../../coverage/coverageService"; suite("Test Coverage Unit Tests", () => { + const workspace = vscode.Uri.file(join(__dirname, "../../../testFiles/gcov")); test("gcov executables based on idfTarget", () => { - const esp32c3 = getGcovExecutable("esp32c3") - const esp32s2 = getGcovExecutable("esp32s2") - const esp32s3 = getGcovExecutable("esp32s3") - const esp32 = getGcovExecutable("esp32") + const esp32c3 = getGcovExecutable("esp32c3"); + const esp32s2 = getGcovExecutable("esp32s2"); + const esp32s3 = getGcovExecutable("esp32s3"); + const esp32 = getGcovExecutable("esp32"); assert.equal(esp32c3, "riscv32-esp-elf-gcov"); assert.equal(esp32s2, "xtensa-esp32s2-elf-gcov"); assert.equal(esp32s3, "xtensa-esp32s3-elf-gcov"); assert.equal(esp32, "xtensa-esp32-elf-gcov"); }); + + test("getGcovFilterPaths", async () => { + const pathsToFilter = await getGcovFilterPaths(workspace); + const example = ["--filter", `${process.env.IDF_PATH}/components`]; + assert.equal(JSON.stringify(example), JSON.stringify(pathsToFilter)) + }) + + // Tests if buildJson returns a double stringified object that has the following properties: "files", "gcovr/format_version" + test("buildJson", async () => { + const result = await buildJson(workspace); + const parsedResult = JSON.parse(JSON.parse(result)); + assert.ok(parsedResult.files); + assert.ok(parsedResult["gcovr/format_version"]); + }) + + // Tests if buildHtml returns a string cointanting html content. + test("buildHtml", async () => { + const result = await buildHtml(workspace); + assert.equal(result.slice(result.length - 8), "\n"); + assert.equal(result.slice(0,15), ""); + }) + + // Tests the parameters and the result not to be undefinied + test("generateCoverageForEditors", async () => { + const editors = vscode.window.visibleTextEditors; + const gcovObj = await buildJson(workspace); + const result = await generateCoverageForEditors( + workspace, + editors, + gcovObj + ); + + assert.ok(editors); + assert.ok(gcovObj); + assert.ok(result); + }) }); diff --git a/testFiles/gcov/gcov_example_func.c b/testFiles/gcov/gcov_example_func.c new file mode 100755 index 000000000..6749df908 --- /dev/null +++ b/testFiles/gcov/gcov_example_func.c @@ -0,0 +1,8 @@ +#include + + +void blink_dummy_func(void) +{ + static int i; + printf("blink_dummy_func: Counter = %d\n", i++); +} diff --git a/testFiles/gcov/gcov_example_main.c b/testFiles/gcov/gcov_example_main.c new file mode 100755 index 000000000..b3a437075 --- /dev/null +++ b/testFiles/gcov/gcov_example_main.c @@ -0,0 +1,60 @@ +/* Blink Example with covergae info + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "driver/gpio.h" +#include "esp_app_trace.h" +#include "sdkconfig.h" + +/* Can use project configuration menu (idf.py menuconfig) to choose the GPIO + to blink, or you can edit the following line and set a number here. +*/ +#define BLINK_GPIO CONFIG_BLINK_GPIO + +void blink_dummy_func(void); +void some_dummy_func(void); + +static void blink_task(void *pvParameter) +{ + // The first two iterations GCOV data are dumped using call to esp_gcov_dump() and OOCD's "esp32 gcov dump" command. + // After that they can be dumped using OOCD's "esp32 gcov" command only. + int dump_gcov_after = -2; + /* Configure the IOMUX register for pad BLINK_GPIO (some pads are + muxed to GPIO on reset already, but some default to other + functions and need to be switched to GPIO. Consult the + Technical Reference for a list of pads and their default + functions.) + */ + gpio_reset_pin(BLINK_GPIO); + /* Set the GPIO as a push/pull output */ + gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); + + while(1) { + /* Blink off (output low) */ + gpio_set_level(BLINK_GPIO, 0); + vTaskDelay(500 / portTICK_PERIOD_MS); + /* Blink on (output high) */ + gpio_set_level(BLINK_GPIO, 1); + vTaskDelay(500 / portTICK_PERIOD_MS); + blink_dummy_func(); + some_dummy_func(); + if (dump_gcov_after++ < 0) { + // Dump gcov data + printf("Ready to dump GCOV data...\n"); + esp_gcov_dump(); + printf("GCOV data have been dumped.\n"); + } + } +} + +void app_main(void) +{ + xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL); +} diff --git a/testFiles/testWorkspace/.vscode/settings.json b/testFiles/testWorkspace/.vscode/settings.json index 6981da92c..daa23e00a 100644 --- a/testFiles/testWorkspace/.vscode/settings.json +++ b/testFiles/testWorkspace/.vscode/settings.json @@ -13,5 +13,6 @@ "window.dialogStyle": "custom", "idf.customExtraPaths": "${env:PATH}", "idf.customExtraVars": "{\"OPENOCD_SCRIPTS\": \"${env:OPENOCD_SCRIPTS}\" }", - "idf.notificationSilentMode": true + "idf.notificationSilentMode": true, + "idf.adapterTargetName": "esp32" } \ No newline at end of file