-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_githook_pre_push.sh
executable file
·173 lines (142 loc) · 4.39 KB
/
test_githook_pre_push.sh
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
#!/usr/bin/env bash
set -e
# This is a script to test the pre-push lint hook in githook.py. It creates a
# test repository, installs the lint git hook, then runs a series of tests to
# confirm that the right files are linted on push, and that lint failure aborts
# the push. If the test passed, it will output "Test passed!".
#
# Unfortuantely, this script doesn't *automatically* assert that the correct
# files are linted. To verify this, add a temporary line of code to githook.py
# that prints the set of linted files, then manually check the output against
# the "Confirm:" lines outputted by this script.
#
# TODO(mdr): It'd be nice if this script *automatically* asserted that the
# right files are linted. I'm not sure it's worth building the necessary
# infra right now, though, since this probably won't be a high-traffic area
# of the KA codebase. But, if that changes, let's upgrade this script!
remote_repo_path=$(mktemp -d -t lint-hook-test-remote.XXXXXX)
local_repo_path=$(mktemp -d -t lint-hook-test-local.XXXXXX)
function finish {
rm -rf $remote_repo_path
rm -rf $local_repo_path
}
trap finish EXIT
# Create a local git repository to serve as the "remote" repository.
echo '>> Initialize "remote" repo.'
git init --bare $remote_repo_path
echo ''
# Create a local git repository to serve as the "local" repository.
# Initialize it, set up the remote, and add the khan-linter git hook.
echo '>> Initialize "local" repo.'
cd $local_repo_path
git init
git remote add origin $remote_repo_path
git commit --allow-empty -m 'initial commit'
git push -u origin master
mkdir -p .git/hooks
echo '~/khan/devtools/khan-linter/githook.py --hook=pre-push "$@"' > .git/hooks/pre-push
chmod +x .git/hooks/pre-push
echo ''
echo ''
echo '>> Push. (Changes: <none>.)'
echo '>> Confirm: No files are linted, push succeeds.'
git push
echo ''
echo '>> Commit. (Changes: Add A and B.)'
touch A.js B.js
git add A.js B.js
git commit -m 'Add A and B.'
git tag add-A-and-B
echo ''
echo '>> Push. (Changes: Add A and B.)'
echo '>> Confirm: A and B are linted, push succeeds.'
git push
echo ''
echo '>> Commit. (Changes: Modify A to fail linting.)'
echo 'foo' > A.js
git add A.js
git commit -m 'Modify A to fail linting.'
echo ''
echo '>> Push. (Changes: Modify A to fail linting.)'
echo '>> Confirm: A is linted, push fails.'
! git push
echo ''
echo '>> Push with no-lint envvar set.'
echo '>> Confirm: A is not linted, push succeeds.'
env GIT_LINT=no git push
echo ''
echo '>> Revert changes to A.'
git reset --hard add-A-and-B
git push -f
echo ''
echo '>> Commit. (Changes: Add C, which fails linting.)'
echo 'foo' > C.js
git add C.js
git commit -m 'Add C, which fails linting.'
echo ''
echo '>> Push. (Changes: Add C, which fails linting.)'
echo '>> Confirm: C is linted, push fails.'
! git push
echo ''
echo '>> Revert creation of C.'
git reset --hard add-A-and-B
echo ''
echo '>> Create feature branch.'
git checkout -b feature-branch
echo ''
echo '>> Commit. (Changes: Add D on feature branch.)'
touch D.js
git add D.js
git commit -m 'Add D on feature branch.'
echo ''
echo '>> Checkout master.'
git checkout master
echo ''
echo '>> Commit. (Changes: Add E.)'
touch E.js
git add E.js
git commit -m 'Add E.'
echo ''
echo '>> Push. (Changes: Add E.)'
echo '>> Confirm: E is linted, push succeeds.'
git push
echo ''
echo '>> Checkout feature branch.'
git checkout feature-branch
echo ''
echo '>> Merge master.'
git merge master --no-edit
echo ''
echo '>> Push feature branch. (Changes: Add E on feature branch, merge D from master.)'
echo '>> Confirm: D is linted, E is NOT linted (not necessary, since master was pushed earlier), push succeeds.'
git push origin feature-branch
echo ''
echo '>> Commit. (Changes: Add F to feature branch.)'
touch F.js
git add F.js
git commit -m 'Add F to feature branch.'
echo ''
echo '>> Checkout master.'
git checkout master
echo ''
echo '>> Commit. (Changes: Add G.)'
touch G.js
git add G.js
git commit -m 'Add G.'
echo ''
echo '>> (Unlike last time, do NOT push to master.)'
echo ''
echo '>> Checkout feature branch.'
git checkout feature-branch
echo ''
echo '>> Merge master.'
git merge master --no-edit
echo ''
echo '>> Push. (Changes: Add F to feature branch, merge G from master.)'
echo '>> Confirm: F and G are linted, push succeeds.'
git push origin feature-branch
echo ''
echo '>> Delete the branch.'
echo '>> Confirm: no lint runs, push succeeds.'
git push origin --delete feature-branch
echo 'Test passed!'