sharness_test_coverage_helper.sh 4.39 KB
Newer Older
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
#!/bin/sh

USAGE="$0 [-h] [-v]"

usage() {
    echo "$USAGE"
    echo "	Print sharness test coverage"
    echo "	Options:"
    echo "		-h|--help: print this usage message and exit"
    echo "		-v|--verbose: print logs of what happens"
    exit 0
}

log() {
    test -z "$VERBOSE" || echo "->" "$@"
}

die() {
    printf >&2 "fatal: %s\n" "$@"
    exit 1
}

# get user options
while [ "$#" -gt "0" ]; do
    # get options
    arg="$1"
    shift

    case "$arg" in
	-h|--help)
	    usage ;;
	-v|--verbose)
	    VERBOSE=1 ;;
	-*)
	    die "unrecognised option: '$arg'\n$USAGE" ;;
	*)
	    die "too many arguments\n$USAGE" ;;
    esac
done

log "Create temporary directory"
DATE=$(date +"%Y-%m-%dT%H:%M:%SZ")
TMPDIR=$(mktemp -d "/tmp/coverage_helper.$DATE.XXXXXX") ||
die "could not 'mktemp -d /tmp/coverage_helper.$DATE.XXXXXX'"

46
log "Grep the sharness tests for ipfs commands"
47 48
CMD_RAW="$TMPDIR/ipfs_cmd_raw.txt"
git grep -n -E '\Wipfs\W' -- sharness/t*-*.sh >"$CMD_RAW" ||
49 50
die "Could not grep ipfs in the sharness tests"

51 52 53 54 55 56 57 58 59
grep_out() {
    pattern="$1"
    src="$TMPDIR/ipfs_cmd_${2}.txt"
    dst="$TMPDIR/ipfs_cmd_${3}.txt"
    desc="$4"

    log "Remove $desc"
    egrep -v "$pattern" "$src" >"$dst" || die "Could not remove $desc"
}
60

61 62 63
grep_out 'test_expect_.*ipfs' raw expect "test_expect_{success,failure} lines"
grep_out '^[^:]+:[^:]+:\s*#' expect comment "comments"
grep_out 'test_description=' comment desc "test_description lines"
64 65
grep_out '^[^:]+:[^:]+:\s*\w+="[^"]*"\s*(\&\&)?\s*$' desc def "variable definition lines"
grep_out '^[^:]+:[^:]+:\s*e?grep\W[^|]*\Wipfs' def grep "grep lines"
66 67 68
grep_out '^[^:]+:[^:]+:\s*cat\W[^|]*\Wipfs' grep cat "cat lines"
grep_out '^[^:]+:[^:]+:\s*rmdir\W[^|]*\Wipfs' cat rmdir "rmdir lines"
grep_out '^[^:]+:[^:]+:\s*echo\W[^|]*\Wipfs' cat echo "echo lines"
69

70 71 72 73 74
grep_in() {
    pattern="$1"
    src="$TMPDIR/ipfs_cmd_${2}.txt"
    dst="$TMPDIR/ipfs_cmd_${3}.txt"
    desc="$4"
75

76 77 78
    log "Keep $desc"
    egrep "$pattern" "$src" >"$dst"
}
79

80 81
grep_in '\Wipfs\W.*/ipfs/' echo slash_in1 "ipfs.*/ipfs/"
grep_in '/ipfs/.*\Wipfs\W' echo slash_in2 "/ipfs/.*ipfs"
82

83
grep_out '/ipfs/' echo slash "/ipfs/"
84

85 86
grep_in '\Wipfs\W.*\.ipfs' slash dot_in1 "ipfs.*\.ipfs"
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 93
for f in dot slash_in1 slash_in2 dot_in1 dot_in2
do
94 95 96 97 98 99
    fname="$TMPDIR/ipfs_cmd_${f}.txt"
    cat "$fname" || die "Could not cat '$fname'"
done | sort | uniq >"$CMD_RES" || die "Could not write '$CMD_RES'"

log "Get all the ipfs commands from 'ipfs commands'"
CMD_CMDS="$TMPDIR/commands.txt"
100
ipfs commands --flags >"$CMD_CMDS" || die "'ipfs commands' failed"
101

102 103 104 105 106 107 108 109 110 111
# Portable function to reverse lines in a file
reverse() {
    if type tac >/dev/null
    then
	tac "$@"
    else
	tail -r "$@"
    fi
}

112 113
log "Match the test line commands with the commands they use"
GLOBAL_REV="$TMPDIR/global_results_reversed.txt"
114 115 116 117 118 119 120 121

process_command() {
    ipfs="$1"
    cmd="$2"
    sub1="$3"
    sub2="$4"
    sub3="$5"

122
    if test -n "$cmd"
123
    then
124 125 126 127
	CMD_OUT="$TMPDIR/res_${ipfs}_${cmd}"
	PATTERN="$ipfs(\W.*)*\W$cmd"
	NAME="$ipfs $cmd"

128 129
	if test -n "$sub1"
	then
130 131 132 133 134
	    CMD_OUT="${CMD_OUT}_${sub1}"
	    PATTERN="$PATTERN(\W.*)*\W$sub1"
	    NAME="$NAME $sub1"

	    if test -n "$sub2"
135
	    then
136 137 138
		CMD_OUT="${CMD_OUT}_${sub2}"
		PATTERN="$PATTERN(\W.*)*\W$sub2"
		NAME="$NAME $sub2"
139 140 141 142 143 144 145

		if test -n "$sub3"
		then
		    CMD_OUT="${CMD_OUT}_${sub3}"
		    PATTERN="$PATTERN(\W.*)*\W$sub3"
		    NAME="$NAME $sub3"
		fi
146 147
	    fi
	fi
148

149 150
	egrep "$PATTERN" "$CMD_RES" >"$CMD_OUT.txt"
	reverse "$CMD_OUT.txt" | sed -e 's/^sharness\///' | cut -d- -f1 | uniq -c >>"$GLOBAL_REV"
151
    fi
152 153 154 155 156 157 158 159 160
}

reverse "$CMD_CMDS" | while read -r line
do
    LONG_CMD=$(echo "$line" | cut -d/ -f1)
    SHORT_CMD=$(expr "$line" : "[^/]*/*\(.*\)")

    log "Processing $LONG_CMD"
    process_command $LONG_CMD
161
    LONG_NAME="$NAME"
162 163 164

    log "Processing $SHORT_CMD"
    process_command $SHORT_CMD
165
    SHORT_NAME="$NAME"
166

167 168
    test -n "$SHORT_CMD" && echo "$SHORT_NAME" >>"$GLOBAL_REV"
    test "$LONG_CMD" != "ipfs" && echo "$LONG_NAME" >>"$GLOBAL_REV"
169
    echo >>"$GLOBAL_REV"
170 171
done

172 173 174 175 176 177 178
# The following will allow us to check that
# we are properly excuding enough stuff using:
# diff -u ipfs_cmd_result.txt cmd_found.txt
log "Get all the line commands that matched"
CMD_FOUND="$TMPDIR/cmd_found.txt"
cat $TMPDIR/res_*.txt | sort -n | uniq >"$CMD_FOUND"

179 180 181
log "Print results"
reverse "$GLOBAL_REV"

182
# Remove temp directory...