summaryrefslogtreecommitdiff
path: root/src/cmd/gofmt/test.sh
blob: 063a0727f50acb73f0cbdde9b9e3dcdd209b2ca1 (plain)
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
#!/usr/bin/env bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

eval $(gomake --no-print-directory -f ../../Make.inc go-env)
if [ -z "$O" ]; then
	echo 'missing $O - maybe no Make.$GOARCH?' 1>&2
	exit 1
fi

CMD="./gofmt"
TMP1=test_tmp1.go
TMP2=test_tmp2.go
TMP3=test_tmp3.go
COUNT=0

count() {
	#echo $1
	let COUNT=$COUNT+1
	let M=$COUNT%10
	if [ $M == 0 ]; then
		echo -n "."
	fi
}


error() {
	echo $1
	exit 1
}


# apply to one file
apply1() {
	# the following files are skipped because they are test cases
	# for syntax errors and thus won't parse in the first place:
	case `basename "$F"` in
	func3.go | const2.go | char_lit1.go | blank1.go | ddd1.go | \
	bug014.go | bug050.go |  bug068.go |  bug083.go | bug088.go | \
	bug106.go | bug121.go | bug125.go | bug133.go | bug160.go | \
	bug163.go | bug166.go | bug169.go | bug217.go | bug222.go | \
	bug226.go | bug228.go | bug248.go | bug274.go | bug280.go | \
	bug282.go | bug287.go | bug298.go | bug299.go | bug300.go | \
	bug302.go | bug306.go | bug322.go | bug324.go | bug335.go | \
	bug340.go | bug349.go | bug351.go | bug358.go ) return ;;
	esac
	# the following directories are skipped because they contain test
	# cases for syntax errors and thus won't parse in the first place:
	case `dirname "$F"` in
	$GOROOT/test/syntax ) return ;;
	esac
	#echo $1 $2
	"$1" "$2"; count "$F"
}


# apply to local files
applydot() {
	for F in `find . -name "*.go" | grep -v "._"`; do
		apply1 "$1" $F
	done
}


# apply to all .go files we can find
apply() {
	for F in `find "$GOROOT" -name "*.go" | grep -v "._"`; do
		apply1 "$1" $F
	done
}


cleanup() {
	rm -f $TMP1 $TMP2 $TMP3
}


silent() {
	cleanup
	$CMD "$1" > /dev/null 2> $TMP1
	if [ $? != 0 ]; then
		cat $TMP1
		error "Error (silent mode test): test.sh $1"
	fi
}


idempotent() {
	cleanup
	$CMD "$1" > $TMP1
	if [ $? != 0 ]; then
		error "Error (step 1 of idempotency test): test.sh $1"
	fi

	$CMD $TMP1 > $TMP2
	if [ $? != 0 ]; then
		error "Error (step 2 of idempotency test): test.sh $1"
	fi

	$CMD $TMP2 > $TMP3
	if [ $? != 0 ]; then
		error "Error (step 3 of idempotency test): test.sh $1"
	fi

	cmp -s $TMP2 $TMP3
	if [ $? != 0 ]; then
		diff $TMP2 $TMP3
		error "Error (step 4 of idempotency test): test.sh $1"
	fi
}


valid() {
	cleanup
	$CMD "$1" > $TMP1
	if [ $? != 0 ]; then
		error "Error (step 1 of validity test): test.sh $1"
	fi

	$GC -o /dev/null $TMP1
	if [ $? != 0 ]; then
		error "Error (step 2 of validity test): test.sh $1"
	fi
}


runtest() {
	#echo "Testing silent mode"
	cleanup
	"$1" silent "$2"

	#echo "Testing idempotency"
	cleanup
	"$1" idempotent "$2"
}


runtests() {
	if [ $# = 0 ]; then
		runtest apply
		# verify the pretty-printed files can be compiled with $GC again
		# do it in local directory only because of the prerequisites required
		#echo "Testing validity"
		# Disabled for now due to dependency problems
		# cleanup
		# applydot valid
	else
		for F in "$@"; do
			runtest apply1 "$F"
		done
	fi
}


# run over all .go files
runtests "$@"
cleanup

# done
echo
echo "PASSED ($COUNT tests)"