Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/actions/env_setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: 'Setup environment'
description: 'Sets up analyzers, CodeChecker and Bazel'
runs:
using: "composite"
steps:
- name: Setup Bazel
uses: bazel-contrib/setup-bazel@0.15.0

# Writing out bazel version forces bazelisk to install bazel here
- name: Set bazel version to 6.5.0
run: |
echo "6.5.0" > .bazelversion
bazel version
shell: bash

- name: Install python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install CodeChecker analyzers
run: |
sudo apt-get update --quiet
sudo apt-get install --no-install-recommends \
clang \
clang-tools \
clang-tidy
# The default naming of the clang-extdef-mapping, needed for CTU, is
# installed by clang-tools also contains the major version
# (e.g. clang-extdef-mapping-18), but the bazel rules reference it
# without the version number. To this end, we use update-alternatives
# to rename the binary to omit it.
sudo update-alternatives --install \
/usr/bin/clang-extdef-mapping \
clang-extdef-mapping \
/usr/bin/clang-extdef-mapping-$(clang --version | head -n 1 |
sed -E 's/.*version ([0-9]+)\..*/\1/') \
100
shell: bash

- name: Install CodeChecker
run: pip3 install codechecker
shell: bash
111 changes: 111 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: codechecker-bazel-tests

# Triggers the workflow on push or pull request events.
on: [push, pull_request]

permissions: read-all

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
rules_test:
name: Unit tests
runs-on: ubuntu-24.04

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup environment
uses: ./.github/actions/env_setup

- name: Print versions
run: |
bazel version
CodeChecker version
echo "[NOTE]: If you are debugging, its possible that " \
"CodeChecker finds different analyzers when running in " \
"bazel's sandbox environment!"
CodeChecker analyzers

- name: Run tests
run: |
cd test
python3 test.py -vvv

# Prepares matrix used to generate jobs in project_test_runner
# This job assumes that every project patch file is in the .github/workflows/patches directory
# and the name of these scripts follows this rule: patch-project_name.sh
# patches must clone their repository into folder: test-proj
prepare_project_matrix:
runs-on: ubuntu-24.04
name: Collecting Projects
outputs:
project_configurations: ${{ steps.generate_matrix.outputs.matrix_json }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Generate dynamic project matrix
id: generate_matrix
run: |
PATCH_DIR="./test/foss/"
TEMP_JSON_FILE=$(mktemp)
find "$PATCH_DIR" -maxdepth 1 -mindepth 1 -type d ! -name "templates" -print0 | while IFS= read -r -d $'\0' PROJECT_FOLDER; do
# Extract project name from folder name
PROJECT_NAME=$(basename "$PROJECT_FOLDER")

jq -n -c \
--arg name "$PROJECT_NAME" \
--arg folder "$PROJECT_FOLDER" \
'{ name: $name, folder: $folder }' >> "$TEMP_JSON_FILE"

echo "Added $PROJECT_NAME to matrix."
done

if [ -s "$TEMP_JSON_FILE" ]; then
FINAL_MATRIX_JSON="[$(paste -s -d ',' "$TEMP_JSON_FILE")]"
else
FINAL_MATRIX_JSON="[]"
fi

echo "Generated matrix: $FINAL_MATRIX_JSON"
echo "matrix_json=$FINAL_MATRIX_JSON" >> "$GITHUB_OUTPUT"
shell: bash

project_test_runner:
# Test the bazel rules introduced by repository on an independent open-source projects.
runs-on: ubuntu-24.04
needs: prepare_project_matrix
strategy:
fail-fast: false
max-parallel: 2 # limit number of concurrent jobs
matrix:
project: ${{ fromJson(needs.prepare_project_matrix.outputs.project_configurations) }}

name: "Test On Project: ${{ matrix.project.name }}"
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup environment
uses: ./.github/actions/env_setup

- name: Initializing test
run: |
cd ${{ matrix.project.folder }}
sh ./init.sh

# Running bazel with test will signal failure because CodeChecker found problems
- name: Run Monolithic Bazel CodeChecker
run: |
cd ${{ matrix.project.folder }}/test-proj
bazel test :codechecker_test || [ $? -eq 3 ] && exit 0 || exit $?

# Running bazel with test will signal failure because CodeChecker found problems
- name: Run Per File Bazel CodeChecker
run: |
cd ${{ matrix.project.folder }}/test-proj
bazel test :code_checker_test || [ $? -eq 3 ] && exit 0 || exit $?
67 changes: 0 additions & 67 deletions .github/workflows/test.yml

This file was deleted.

10 changes: 10 additions & 0 deletions test/foss/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# How to add a new project:

- Create a folder with the name of the project.
- place an init.sh script in the folder, this script should:
- Clone the test project into a folder named `test-proj`.
- To ensure the project doesn't change over time, check out a specific tag or commit instead of a branch!
- Copy the .bazelversion file from the templates directory.
- Append the WORKSPACE.template file to the WORKSPACE file of the project.
- Append the codechecker rules to the BUILD file of the project.
- There can be only two targets, codechecker_test and code_checker_test
1 change: 1 addition & 0 deletions test/foss/templates/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.5.0
20 changes: 20 additions & 0 deletions test/foss/templates/WORKSPACE.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#----------------------------------------------------

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

local_repository(
name = "bazel_codechecker",
path = "../../../../",
)

load(
"@bazel_codechecker//src:tools.bzl",
"register_default_codechecker",
"register_default_python_toolchain",
)

register_default_python_toolchain()

register_default_codechecker()

#----------------------------------------------------
57 changes: 57 additions & 0 deletions test/foss/yaml-cpp/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# Copyright 2023 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

git clone --recurse https://github.com/jbeder/yaml-cpp.git test-proj
cd test-proj
git checkout yaml-cpp-0.7.0

# This file must be in the root of the project to be analyzed for bazelisk to work
cp ../../templates/.bazelversion ./.bazelversion

# Add codechecker to the project
cat <<EOF >> BUILD.bazel
#-------------------------------------------------------

# codechecker rules
load(
"@bazel_codechecker//src:codechecker.bzl",
"codechecker_test",
)
load(
"@bazel_codechecker//src:code_checker.bzl",
"code_checker_test",
)


codechecker_test(
name = "codechecker_test",
targets = [
":yaml-cpp",
],
)

code_checker_test(
name = "code_checker_test",
targets = [
":yaml-cpp",
],
)

#-------------------------------------------------------
EOF

# Add codechecker_bazel repo to WORKSPACE
cat ../../templates/WORKSPACE.template >> WORKSPACE
57 changes: 57 additions & 0 deletions test/foss/zlib/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# Copyright 2023 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

git clone --recurse https://github.com/madler/zlib.git test-proj
cd test-proj
git checkout 5a82f71ed1dfc0bec044d9702463dbdf84ea3b71

# This file must be in the root of the project to be analyzed for bazelisk to work
cp ../../templates/.bazelversion ./.bazelversion

# Add codechecker to the project
cat <<EOF >> BUILD.bazel
#-------------------------------------------------------

# codechecker rules
load(
"@bazel_codechecker//src:codechecker.bzl",
"codechecker_test",
)
load(
"@bazel_codechecker//src:code_checker.bzl",
"code_checker_test",
)


codechecker_test(
name = "codechecker_test",
targets = [
":z",
],
)

code_checker_test(
name = "code_checker_test",
targets = [
":z",
],
)

#-------------------------------------------------------
EOF

# Add codechecker_bazel repo to WORKSPACE
cat ../../templates/WORKSPACE.template >> WORKSPACE
Loading