master
sh 182 lines 4.39 KB
Raw
1 #!/bin/sh
2
3 USAGE="$0 [-h] [-v]"
4
5 usage() {
6 echo "$USAGE"
7 echo " Print sharness test coverage"
8 echo " Options:"
9 echo " -h|--help: print this usage message and exit"
10 echo " -v|--verbose: print logs of what happens"
11 exit 0
12 }
13
14 log() {
15 test -z "$VERBOSE" || echo "->" "$@"
16 }
17
18 die() {
19 printf >&2 "fatal: %s\n" "$@"
20 exit 1
21 }
22
23 # get user options
24 while [ "$#" -gt "0" ]; do
25 # get options
26 arg="$1"
27 shift
28
29 case "$arg" in
30 -h|--help)
31 usage ;;
32 -v|--verbose)
33 VERBOSE=1 ;;
34 -*)
35 die "unrecognised option: '$arg'\n$USAGE" ;;
36 *)
37 die "too many arguments\n$USAGE" ;;
38 esac
39 done
40
41 log "Create temporary directory"
42 DATE=$(date +"%Y-%m-%dT%H:%M:%SZ")
43 TMPDIR=$(mktemp -d "/tmp/coverage_helper.$DATE.XXXXXX") ||
44 die "could not 'mktemp -d /tmp/coverage_helper.$DATE.XXXXXX'"
45
46 log "Grep the sharness tests for ipfs commands"
47 CMD_RAW="$TMPDIR/ipfs_cmd_raw.txt"
48 git grep -n -E '\Wipfs\W' -- sharness/t*-*.sh >"$CMD_RAW" ||
49 die "Could not grep ipfs in the sharness tests"
50
51 grep_out() {
52 pattern="$1"
53 src="$TMPDIR/ipfs_cmd_${2}.txt"
54 dst="$TMPDIR/ipfs_cmd_${3}.txt"
55 desc="$4"
56
57 log "Remove $desc"
58 egrep -v "$pattern" "$src" >"$dst" || die "Could not remove $desc"
59 }
60
61 grep_out 'test_expect_.*ipfs' raw expect "test_expect_{success,failure} lines"
62 grep_out '^[^:]+:[^:]+:\s*#' expect comment "comments"
63 grep_out 'test_description=' comment desc "test_description lines"
64 grep_out '^[^:]+:[^:]+:\s*\w+="[^"]*"\s*(\&\&)?\s*$' desc def "variable definition lines"
65 grep_out '^[^:]+:[^:]+:\s*e?grep\W[^|]*\Wipfs' def grep "grep lines"
66 grep_out '^[^:]+:[^:]+:\s*cat\W[^|]*\Wipfs' grep cat "cat lines"
67 grep_out '^[^:]+:[^:]+:\s*rmdir\W[^|]*\Wipfs' cat rmdir "rmdir lines"
68 grep_out '^[^:]+:[^:]+:\s*echo\W[^|]*\Wipfs' cat echo "echo lines"
69
70 grep_in() {
71 pattern="$1"
72 src="$TMPDIR/ipfs_cmd_${2}.txt"
73 dst="$TMPDIR/ipfs_cmd_${3}.txt"
74 desc="$4"
75
76 log "Keep $desc"
77 egrep "$pattern" "$src" >"$dst"
78 }
79
80 grep_in '\Wipfs\W.*/ipfs/' echo slash_in1 "ipfs.*/ipfs/"
81 grep_in '/ipfs/.*\Wipfs\W' echo slash_in2 "/ipfs/.*ipfs"
82
83 grep_out '/ipfs/' echo slash "/ipfs/"
84
85 grep_in '\Wipfs\W.*\.ipfs' slash dot_in1 "ipfs.*\.ipfs"
86 grep_in '\.ipfs.*\Wipfs\W' slash dot_in2 "\.ipfs.*ipfs"
87
88 grep_out '\.ipfs' slash dot ".ipfs"
89
90 log "Print result"
91 CMD_RES="$TMPDIR/ipfs_cmd_result.txt"
92 for f in dot slash_in1 slash_in2 dot_in1 dot_in2
93 do
94 fname="$TMPDIR/ipfs_cmd_${f}.txt"
95 cat "$fname" || die "Could not cat '$fname'"
96 done | sort | uniq >"$CMD_RES" || die "Could not write '$CMD_RES'"
97
98 log "Get all the ipfs commands from 'ipfs commands'"
99 CMD_CMDS="$TMPDIR/commands.txt"
100 ipfs commands --flags >"$CMD_CMDS" || die "'ipfs commands' failed"
101
102 # Portable function to reverse lines in a file
103 reverse() {
104 if type tac >/dev/null
105 then
106 tac "$@"
107 else
108 tail -r "$@"
109 fi
110 }
111
112 log "Match the test line commands with the commands they use"
113 GLOBAL_REV="$TMPDIR/global_results_reversed.txt"
114
115 process_command() {
116 ipfs="$1"
117 cmd="$2"
118 sub1="$3"
119 sub2="$4"
120 sub3="$5"
121
122 if test -n "$cmd"
123 then
124 CMD_OUT="$TMPDIR/res_${ipfs}_${cmd}"
125 PATTERN="$ipfs(\W.*)*\W$cmd"
126 NAME="$ipfs $cmd"
127
128 if test -n "$sub1"
129 then
130 CMD_OUT="${CMD_OUT}_${sub1}"
131 PATTERN="$PATTERN(\W.*)*\W$sub1"
132 NAME="$NAME $sub1"
133
134 if test -n "$sub2"
135 then
136 CMD_OUT="${CMD_OUT}_${sub2}"
137 PATTERN="$PATTERN(\W.*)*\W$sub2"
138 NAME="$NAME $sub2"
139
140 if test -n "$sub3"
141 then
142 CMD_OUT="${CMD_OUT}_${sub3}"
143 PATTERN="$PATTERN(\W.*)*\W$sub3"
144 NAME="$NAME $sub3"
145 fi
146 fi
147 fi
148
149 egrep "$PATTERN" "$CMD_RES" >"$CMD_OUT.txt"
150 reverse "$CMD_OUT.txt" | sed -e 's/^sharness\///' | cut -d- -f1 | uniq -c >>"$GLOBAL_REV"
151 fi
152 }
153
154 reverse "$CMD_CMDS" | while read -r line
155 do
156 LONG_CMD=$(echo "$line" | cut -d/ -f1)
157 SHORT_CMD=$(expr "$line" : "[^/]*/*\(.*\)")
158
159 log "Processing $LONG_CMD"
160 process_command $LONG_CMD
161 LONG_NAME="$NAME"
162
163 log "Processing $SHORT_CMD"
164 process_command $SHORT_CMD
165 SHORT_NAME="$NAME"
166
167 test -n "$SHORT_CMD" && echo "$SHORT_NAME" >>"$GLOBAL_REV"
168 test "$LONG_CMD" != "ipfs" && echo "$LONG_NAME" >>"$GLOBAL_REV"
169 echo >>"$GLOBAL_REV"
170 done
171
172 # The following will allow us to check that
173 # we are properly excluding enough stuff using:
174 # diff -u ipfs_cmd_result.txt cmd_found.txt
175 log "Get all the line commands that matched"
176 CMD_FOUND="$TMPDIR/cmd_found.txt"
177 cat $TMPDIR/res_*.txt | sort -n | uniq >"$CMD_FOUND"
178
179 log "Print results"
180 reverse "$GLOBAL_REV"
181
182 # Remove temp directory...