forked from AimRT/AimRT
-
Notifications
You must be signed in to change notification settings - Fork 0
203 lines (162 loc) · 6.32 KB
/
test-workflow.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
name: test Workflow
on:
workflow_call:
inputs:
image_name:
required: true
type: string
default: ubuntu
image_tag:
required: false
type: string
default: latest
run_platform:
required: false
type: string
default: amd64
test_report:
required: false
type: boolean
default: false
secrets:
TEST_CMD:
required: true
jobs:
test:
runs-on: ${{ inputs.run_platform }}
container:
image: ${{ inputs.image_name }}:${{ inputs.image_tag }}
steps:
- name: Checkout code
uses: actions/checkout@v4
env:
https_proxy: ${{ secrets.https_proxy }}
http_proxy: ${{ secrets.http_proxy }}
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Get short sha
id: slug
run: echo "sha8=$(echo ${GITHUB_SHA} | cut -c1-8)" >> $GITHUB_OUTPUT
- name: generate test_coverage.sh
if: inputs.test_report == true
run: |
echo "generate /tmp/test_coverage.sh ..."
cat << EOF >/tmp/test_coverage.sh
#!/bin/bash
# 生成报告并提交到指定http文件服务器上
if [ -z "${{ secrets.TEST_COVERAGE_REPORT_POST_URL }}" ]; then
echo "TEST_COVERAGE_REPORT_POST_URL is empty, not generate test coverage report"
exit 0
fi
ret=0
# 创建一个\$workdir文件夹,用于存放所有的.gcda文件
workdir='/tmp/gtest_report'
rm \$workdir -rf
mkdir -p \$workdir
default_ignore=(
"_test.cc"
"_test.c"
"_test.cpp"
"*/build/_deps/*"
)
default_test_cov_ignore=(
"*/include/*"
"*/build/_deps/*"
\${TEST_COVERAGE_IGNORE[@]}
)
# 查找同时存在 *.gcda 和 *.gcno 文件的文件
gcda_files=\$(find ./build -type f -name "*.gcda")
for gcda_file in \$gcda_files; do
# 如果匹配了 \$default_ignore 中的文件,则跳过
for ignore in \${default_ignore[@]}; do
# 使用正则表达式匹配
if [[ \$gcda_file =~ \$ignore ]]; then
echo "ignore \$gcda_file"
continue 2
fi
done
gcno_file="\${gcda_file%.gcda}.gcno"
if [ -f "\$gcno_file" ]; then
cp "\$gcda_file" "\$workdir"
cp "\$gcno_file" "\$workdir"
fi
done
cd \$workdir
# 如果当前目录下没有gcda文件,则直接退出,不需要进行测试覆盖率检查
if [ -z "\$(find . -name "*.gcda")" ]; then
echo "no gcda files, not need to test coverage"
exit 0
fi
# 使用 gcov 来生成覆盖率数据
gcov *.gcno >/dev/null 2>&1
# 生成报告
lcov -c -d . -o coverage.info >/dev/null 2>&1
# 过滤掉不需要的信息
for ignore in \${default_test_cov_ignore[@]}; do
lcov --remove coverage.info "\$ignore" -o coverage.info >/dev/null 2>&1
done
# 列出测试覆盖率
lcov -l coverage.info
# 判断整体覆盖率是否达标
test_coverage=\$(lcov -l coverage.info | grep "Total:" | sed 's/|/| /g' | awk '{print \$2}' | awk -F '%' '{print \$1}')
# if error to get test_coverage
if [ -z "\$test_coverage" ];
then
echo "no test coverage, skip"
exit 0
fi
# 生成html报告
test_coverage_dir=${{ github.repository_owner }}/${{ github.event.repository.name }}/${{ github.ref_name }}/${{ steps.slug.outputs.sha8 }}
mkdir -p \$test_coverage_dir
genhtml coverage.info -o \$test_coverage_dir >/dev/null 2>&1
# zip 压缩 \$test_coverage_dir
zip -r ${{ steps.slug.outputs.sha8 }}.zip \$test_coverage_dir
# 生成报告并提交到指定http文件服务器上
curl -F file=@\${{ steps.slug.outputs.sha8 }}.zip -F unzip=true \${{secrets.TEST_COVERAGE_REPORT_POST_URL}}
# 判断推送是否成功
if [ \$? -ne 0 ]; then
echo "test report push failed ..."
ret=1
fi
# 如果 TEST_COVERAGE_THRESHOLD 变量为空,则默认为 0
if [ -z "\$TEST_COVERAGE_THRESHOLD" ]; then
TEST_COVERAGE_THRESHOLD=0
fi
echo "test coverage: \$test_coverage"
# 将TEST_COVERAGE_THRESHOLD转换为数字,防止出现字符串比较的情况
test_coverage_threshold=\$(echo \$TEST_COVERAGE_THRESHOLD | tr -d '"')
#比较两个数的大小,一个是小数,一个是整数
if [ \$(echo "\$test_coverage >= \$test_coverage_threshold" | bc) -eq 1 ]; then
echo "test coverage is greater than \$test_coverage_threshold"
else
echo "test coverage is less than \$test_coverage_threshold"
ret=1
fi
# 删除临时文件
rm -rf \$test_coverage_dir
rm -rf ${{ steps.slug.outputs.sha8 }}.zip
# 打印报告地址
echo -e "\\033[1;35m test coverage report url: ${{secrets.TEST_COVERAGE_REPORT_URL}}/\$test_coverage_dir/index.html \\033[0m"
exit \$ret
EOF
- name: Run test stage
shell: bash
env:
https_proxy: ""
http_proxy: ""
no_proxy: "*"
run: |
echo "runnint test in workflow_call"
source /opt/ros/humble/setup.bash
echo "${{ secrets.TEST_CMD}}"
eval "${{ secrets.TEST_CMD}}"
- name: authorize test_coverage.sh
if: inputs.test_report == true
env:
https_proxy: ""
http_proxy: ""
no_proxy: "*"
run: |
chmod +x /tmp/test_coverage.sh
echo "test_coverage.sh is authorized"
bash /tmp/test_coverage.sh