b0e065d09f837ad6ffb839f0e191e6b4a6176fb8
[platform/core/csapi/tizenfx.git] / coverage.sh
1 #!/bin/bash -e
2
3 SCRIPT_DIR=$(dirname $(readlink -f $0))
4 ROOTDIR=$SCRIPT_DIR
5
6 BUILD_CONF=Debug
7 SLN_NAME=_TizenFX_Cov
8 SLN_FILE=$ROOTDIR/$SLN_NAME.sln
9
10 COV_TOOL_PATH=$ROOTDIR/tools/coverage
11 COV_REPORT_PATH=$ROOTDIR/Artifacts/coverage
12 MINICOVER_PATH=$COV_TOOL_PATH/coverage/minicover_linux
13 TARGET_ASSEMBLY_PATH=/usr/share/dotnet.tizen/framework
14 TARGET_COV_PATH=/home/owner/share/cov
15
16
17 usage() {
18   echo "Usage: $0 [command] [module] [exclude-sources]"
19   echo "Commands:"
20   echo "    instrument|--instrument|-i : Instrument assemblies to record code coverage."
21   echo "    replace |--replace |-c     : Replace runtime assemblies with intrumented."
22   echo "    report|--report|-r         : Generate report files."
23 }
24
25 check_module() {
26   if [ -z "$1" ]; then
27     echo "No module specified." 1>&2
28     exit 1
29   fi
30
31   local PROJECT_DIR=$ROOTDIR/src/$1
32
33   if [ ! -f $PROJECT_DIR/$1.csproj ]; then
34     echo "No such found $1 module." 1>&2
35     exit 1
36   fi
37
38   echo $PROJECT_DIR
39 }
40
41 update_coverage_tools() {
42   echo "## Update Coverage Tools ##"
43   if [ -d $COV_TOOL_PATH ]; then
44     pushd $COV_TOOL_PATH > /dev/null
45     git fetch origin
46     git reset --hard origin/master
47     popd > /dev/null
48   else
49     git clone https://github.sec.samsung.net/RS8-DotNetTctTools/CSharpCoverageTool $COV_TOOL_PATH
50   fi
51 }
52
53 public_modules() {
54   find $ROOTDIR/src -maxdepth 1 \
55       -type d ! -name "*.Design" \
56       -not -path $ROOTDIR/src | sort | sed "s:$ROOTDIR/src/::"
57 }
58
59 build() {
60   PROJECT_DIR=$(check_module $1)
61   dotnet build -c $BUILD_CONF $PROJECT_DIR/$1.csproj
62 }
63
64 instrument() {
65   PROJECT_DIR=$(check_module $1)
66   dotnet $MINICOVER_PATH/dotnet-minicover.dll instrument \
67       --workdir $ROOTDIR \
68       --sources "/src/$1/**/*.cs" \
69       --exclude-sources $2 \
70       --assemblies "/src/$1/bin/$BUILD_CONF/*/$1.dll" \
71       --hits-file $1
72 }
73
74 prepare_replace() {
75   sdb root on
76   sdb shell mount -o remount,rw /
77   sdb shell "rm -f $TARGET_ASSEMBLY_PATH/*.ni.dll"
78   sdb shell "rm -fr $TARGET_COV_PATH"
79   sdb shell "mkdir -p $TARGET_COV_PATH"
80   sdb push $MINICOVER_PATH/MiniCover.HitServices.dll $TARGET_ASSEMBLY_PATH
81   sdb shell chsmack -a '_' $TARGET_ASSEMBLY_PATH/MiniCover.HitServices.dll
82 }
83
84 replace() {
85   PROJECT_DIR=$(check_module $1)
86   sdb push $PROJECT_DIR/bin/$BUILD_CONF/*/$1.dll $TARGET_ASSEMBLY_PATH
87   sdb shell chsmack -a '_' $TARGET_ASSEMBLY_PATH/$1.dll
88 }
89
90 prepare_report() {
91   rm -fr $COV_REPORT_PATH
92   mkdir -p $COV_REPORT_PATH
93   sdb pull $TARGET_COV_PATH
94 }
95
96 report() {
97   set +e
98   PROJECT_DIR=$(check_module $1)
99   dotnet $MINICOVER_PATH/dotnet-minicover.dll xmlreport \
100       --threshold 85 --output $1.xml
101   dotnet $COV_TOOL_PATH/coverage/ReportGenerator_4.8.0/netcoreapp2.0/ReportGenerator.dll \
102       -reporttypes:lcov -reports:$1.xml -targetdir:$COV_REPORT_PATH/$1
103   java -jar $COV_TOOL_PATH/coverage/jgenhtml-1.5.jar \
104       $COV_REPORT_PATH/$1/lcov.info -o $COV_REPORT_PATH/$1 --no-branch-coverage
105
106   rm -f $1.xml
107   rm -f $1*.hits
108 }
109
110 cmd_instrument() {
111   build $1
112   instrument $1 $2
113 }
114
115 cmd_replace() {
116   prepare_replace
117   replace $1
118 }
119
120 cmd_report() {
121   prepare_report
122   report $1
123 }
124
125 update_coverage_tools
126
127 cmd=$1; [ $# -gt 0 ] && shift;
128 case "$cmd" in
129   instrument|--instrument|-i) cmd_instrument $@ ;;
130   replace |--replace |-c) cmd_replace $@ ;;
131   report|--report| -r)  cmd_report $@ ;;
132   *) usage ;;
133 esac