Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxy-v committed Sep 1, 2024
1 parent f57cb02 commit c75b218
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 3 deletions.
161 changes: 161 additions & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
name: Build
on:
push:
jobs:
build-project:
name: Build Project
runs-on: ubuntu-latest
steps:
- name: Checkout Project
uses: actions/[email protected]

- name: Fetch master
run: |
git fetch --no-tags --prune --depth=1 origin master
- name: Setup Ninja
uses: seanmiddleditch/gha-setup-ninja@v5

- name: Build Project
uses: threeal/[email protected]
with:
generator: Ninja
options: |
CMAKE_EXPORT_COMPILE_COMMANDS=ON
ENABLE_COVERAGE=ON
run-build: false

- name: Get modified source
id: get-modified-source
uses: actions/github-script@v7
with:
script: |
const diff = require('./scripts/diff.js');
console.log(diff())
- name: Get affected targets
id: find-targets
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const fs = require('fs');
const { execSync } = require('child_process');
let sources = execSync(`git diff --name-only origin/master`).toString().split('\n')
let outputs = [];
let commands = JSON.parse(fs.readFileSync('build/compile_commands.json', 'utf8'));
for (let src of sources) {
if (src.endsWith('.cpp') || src.endsWith('.h') || src.endsWith('.c')
|| src.endsWith('.hpp')) {
const idx = commands.findIndex(entry => entry.file.endsWith(src));
if (idx >= 0) {
let entry = commands[idx];
if (entry.output !== 'undefined') {
entry.output = entry.command.split(' ').find(token => token.endsWith('.o'));
}
outputs.push(entry.output);
commands.splice(idx, 1);
}
}
}
targets = new Set();
while (outputs.length > 0) {
level_targets = new Set();
for (const output of outputs) {
let lines = execSync(`ninja -C build -t query ${output}`).toString().split('\n');
let insert = false;
for (const line of lines) {
if (line.endsWith('outputs:')) {
insert = true;
continue;
}
const value = line.trim();
if (insert && value.length > 0) {
level_targets.add(value);
}
}
}
outputs = [...level_targets];
targets = new Set([...targets, ...level_targets]);
}
return [...targets].join(' ');
- name: Build affected targets
if: ${{steps.find-targets.outputs.result != ''}}
run: ninja -C build ${{steps.find-targets.outputs.result}}

- name: Run ctest
if: ${{steps.find-targets.outputs.result != ''}}
run: ctest --test-dir build

- name: Generate a code coverage report
if: ${{steps.find-targets.outputs.result != ''}}
uses: threeal/gcovr-action@xml-out
with:
xml-out: coverage.xml

- run: npm install xml2js

- name: Check coverage report
id: check-coverage
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const parseString = require('xml2js').parseString;
const result = await new Promise((resolve, reject) => parseString(fs.readFileSync('coverage.xml', 'utf8'), (err, result) => {
if (err) reject(err);
else resolve(result);
}));
console.log('${{ github.base_ref }}');
console.log('${{ github.head_ref }}');
console.log('${{ github.ref }}');
console.log('${{ github.ref_name }}');
console.log('${{ github.path }}');
# "packages": [
# {
# "package": [
# {
# "$": {
# "name": "src",
# "line-rate": "0.5",
# "branch-rate": "0.5",
# "complexity": "0.0"
# },
# "classes": [
# {
# "class": [
# {
# "$": {
# "name": "main_cpp",
# "filename": "src/main.cpp",
# "line-rate": "0.0",
# "branch-rate": "1.0",
# "complexity": "0.0"
# },
# "methods": [
# ""
# ],
# "lines": [
# {
# "line": [
# {
# "$": {
# "number": "6",
# "hits": "0",
# "branch": "false"
# }
# },
# {
# "$": {
# "number": "7",
# "hits": "0",
# "branch": "false"
# }
# },
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
project(coverage_action)
cmake_minimum_required(VERSION 3.22)

project(coverage_action)
include(CTest)

add_library(my_static_lib STATIC src/my_static_lib.cpp)
add_library(my_shared_lib SHARED src/my_shared_lib.cpp)


if (BUILD_TESTING)
enable_testing()
include(coverage.cmake)
add_executable(test_shared_lib src/test1.cpp)
target_link_libraries(test_shared_lib my_shared_lib)

add_executable(test_static_lib src/test2.cpp)
target_link_libraries(test_static_lib my_static_lib)

add_test(NAME test_shared_lib COMMAND test_shared_lib)
add_test(NAME test_static_lib COMMAND test_static_lib)
endif()


add_executable(main src/main.cpp)
target_link_libraries(main my_static_lib my_shared_lib)
37 changes: 37 additions & 0 deletions coverage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#=========================================================================
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
#=========================================================================

# This code has been adapted from remus (https://gitlab.kitware.com/cmb/remus)

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(CMAKE_COMPILER_IS_CLANGXX 1)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)

include(CheckCXXCompilerFlag)

#Add option for enabling gcov coverage
option(ENABLE_COVERAGE "Build with gcov support." OFF)
mark_as_advanced(ENABLE_COVERAGE)

if(ENABLE_COVERAGE)
#We're setting the CXX flags and C flags beacuse they're propagated down
#independent of build type.
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fno-elide-constructors -fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping")
endif()
endif()
endif()
36 changes: 36 additions & 0 deletions scripts/diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { execSync } = require('child_process');
module.exports = () => {
const output = execSync(`git diff origin/master`).toString().split('\n')
let count = 0;
let start = 0;
let current = 0;
let sources = [];
for (let i = 0; i < output.length; i++) {
if (output[i].startsWith('diff --git')) {
// skip 'diff --git a/' prefix
const file = output[i].split(' ')[2].substring(2);
sources.push({'file': file, 'lines': []});
// go to header line, e.g. '@@ -1,2 +1,3 @@'
do {
++i;
}
while (!output[i].startsWith('@@'));
output[i].split(' ').forEach((c) => {
if (c.startsWith('+')) {
[start, count] = c.split(',');
start = parseInt(start);
count = parseInt(count);
current = 0;
}
});
}
else if (current < count && !output[i].startsWith('-')) {
if (output[i].startsWith('+')) {
sources[sources.length - 1].lines.push(start + current);
}
++current;
}
}

return sources;
};
45 changes: 45 additions & 0 deletions scripts/targets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const fs = require('fs');
const { execSync } = require('child_process');

module.exports = (sources) => {
let outputs = [];
let commands = JSON.parse(fs.readFileSync('build/compile_commands.json', 'utf8'));
for (let {'file': src, ..._} of sources) {
if (src.endsWith('.cpp') || src.endsWith('.h') || src.endsWith('.c')
|| src.endsWith('.hpp')) {
const idx = commands.findIndex(entry => entry.file.endsWith(src));
if (idx >= 0) {
let entry = commands[idx];
if (entry.output !== 'undefined') {
entry.output = entry.command.split(' ').find(token => token.endsWith('.o'));
}
outputs.push(entry.output);
commands.splice(idx, 1);
}
}
}

targets = new Set();
while (outputs.length > 0) {
level_targets = new Set();
for (const output of outputs) {
let lines = execSync(`ninja -C build -t query ${output}`).toString().split('\n');

let insert = false;
for (const line of lines) {
if (line.endsWith('outputs:')) {
insert = true;
continue;
}
const value = line.trim();
if (insert && value.length > 0) {
level_targets.add(value);
}
}
}

outputs = [...level_targets];
targets = new Set([...targets, ...level_targets]);
}
return [...targets].join(' ');
}
5 changes: 3 additions & 2 deletions src/my_static_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ int method_in_static_lib() {
}

int method_in_static_lib2() {
return 43;
}
int p = 65;
return p;
}
9 changes: 9 additions & 0 deletions src/test1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int method_in_shared_lib();

int main() {
if (42 == method_in_shared_lib()) {
return 0;
} else {
return 1;
}
}
9 changes: 9 additions & 0 deletions src/test2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int method_in_static_lib();

int main() {
if (42 == method_in_static_lib()) {
return 0;
} else {
return 1;
}
}

0 comments on commit c75b218

Please sign in to comment.