| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
set -eu |
| 4 |
|
| 5 |
TEST_REFERENCE_SUBDIR="${TEST_REFERENCE_SUBDIR:-test/reference}" |
| 6 |
REPO=pixel-test-reference |
| 7 |
|
| 8 |
GITHUB_BASE="${GITHUB_BASE:-cockpit-project/cockpit}" |
| 9 |
GITHUB_REPOSITORY="${GITHUB_BASE%/*}/${REPO}" |
| 10 |
CLONE_REMOTE="https://github.com/${GITHUB_REPOSITORY}" |
| 11 |
PUSH_REMOTE="[email protected]:${GITHUB_REPOSITORY}" |
| 12 |
|
| 13 |
message() { |
| 14 |
[ "${V-}" != 0 ] || printf " %-8s %s\n" "$1" "$2" |
| 15 |
} |
| 16 |
|
| 17 |
cmd_init() { |
| 18 |
git submodule add -b empty "$CLONE_REMOTE" "$TEST_REFERENCE_SUBDIR" |
| 19 |
} |
| 20 |
|
| 21 |
cmd_update() { |
| 22 |
git submodule update --init -- "$TEST_REFERENCE_SUBDIR" || ( |
| 23 |
echo "" |
| 24 |
echo "Updating test/reference has failed, maybe because of" |
| 25 |
echo "local changes that have been accidentally made while" |
| 26 |
echo "it was out of date." |
| 27 |
echo "" |
| 28 |
echo "If you want to throw away these local changes, run" |
| 29 |
echo "" |
| 30 |
echo " $ ./test/common/pixel-tests reset" |
| 31 |
echo "" |
| 32 |
exit 1 |
| 33 |
) |
| 34 |
} |
| 35 |
|
| 36 |
cmd_pull() { |
| 37 |
cmd_update |
| 38 |
} |
| 39 |
|
| 40 |
cmd_status() { |
| 41 |
cmd_update |
| 42 |
( cd "$TEST_REFERENCE_SUBDIR" |
| 43 |
git rm --force --cached --quiet '*.png' |
| 44 |
git add *.png |
| 45 |
if git diff-index --name-status --cached --exit-code HEAD; then |
| 46 |
echo No changes |
| 47 |
fi |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
cmd_push() { |
| 52 |
cmd_update |
| 53 |
( cd "$TEST_REFERENCE_SUBDIR" |
| 54 |
git rm --force --cached --quiet '*.png' |
| 55 |
git add *.png |
| 56 |
if ! git diff-index --name-status --cached --exit-code HEAD; then |
| 57 |
git fetch origin empty:empty |
| 58 |
git reset --soft empty |
| 59 |
git commit --quiet -m "$(date)" |
| 60 |
else |
| 61 |
echo No changes |
| 62 |
fi |
| 63 |
tag="sha-$(git rev-parse HEAD)" |
| 64 |
[ $(git tag -l "$tag") ] || git tag "$tag" HEAD --no-sign |
| 65 |
git push "$PUSH_REMOTE" "$tag" |
| 66 |
) |
| 67 |
git add "$TEST_REFERENCE_SUBDIR" |
| 68 |
if [ -n "$(git status --porcelain "$TEST_REFERENCE_SUBDIR")" ]; then |
| 69 |
echo "" |
| 70 |
echo "The test/reference link has changed. The next step is to commit and" |
| 71 |
echo "push this change, just like any other change to a file." |
| 72 |
echo "" |
| 73 |
echo "The change has already been added with 'git add', so you could now" |
| 74 |
echo "amend your current HEAD commit with it like this:" |
| 75 |
echo "" |
| 76 |
echo " $ git commit --amend" |
| 77 |
echo "" |
| 78 |
echo "Then the HEAD commit can be pushed like normally. There is nothing" |
| 79 |
echo "special about committing and pushing a change to test/reference." |
| 80 |
fi |
| 81 |
} |
| 82 |
|
| 83 |
cmd_reset() { |
| 84 |
rm -rf "$TEST_REFERENCE_SUBDIR" |
| 85 |
cmd_update |
| 86 |
} |
| 87 |
|
| 88 |
pixel_test_logs_urls() { |
| 89 |
urls=() |
| 90 |
prs=() |
| 91 |
for arg in "${@:-}"; do |
| 92 |
if [[ "$arg" == http* ]]; then |
| 93 |
urls+=("$arg") |
| 94 |
else |
| 95 |
prs+=("$arg") |
| 96 |
fi |
| 97 |
done |
| 98 |
|
| 99 |
if [ ${#urls[@]} -gt 0 ]; then |
| 100 |
echo "${urls[@]}" |
| 101 |
return |
| 102 |
fi |
| 103 |
|
| 104 |
revisions=() |
| 105 |
remote=$(git remote get-url upstream || git remote get-url origin) |
| 106 |
repo=$(echo $remote | sed -re 's,[email protected]:|https://github.com/,,' -e 's,\.git$,,') |
| 107 |
if [ ${#prs[@]} -eq 0 ]; then |
| 108 |
revisions+=("$(git rev-parse "@{upstream}")") |
| 109 |
else |
| 110 |
for pr in "${prs[@]}"; do |
| 111 |
revisions+=("$(curl -s "https://api.github.com/repos/$repo/pulls/$pr" | python3 -c " |
| 112 |
import json |
| 113 |
import sys |
| 114 |
|
| 115 |
print(json.load(sys.stdin)['head']['sha']) |
| 116 |
")") |
| 117 |
done |
| 118 |
fi |
| 119 |
for revision in "${revisions[@]}"; do |
| 120 |
context=$(cat test/reference-image) |
| 121 |
curl -s "https://api.github.com/repos/$repo/statuses/$revision?per_page=100" | python3 -c " |
| 122 |
import json |
| 123 |
import sys |
| 124 |
|
| 125 |
seen = set() |
| 126 |
for s in json.load(sys.stdin): |
| 127 |
c = s['context'] |
| 128 |
if 'firefox' in c or 'devel' in c: |
| 129 |
continue |
| 130 |
if c.split('/')[0] == sys.argv[1] and s['target_url']: |
| 131 |
url=s['target_url'] |
| 132 |
if url not in seen: |
| 133 |
seen.add(url) |
| 134 |
print(url) |
| 135 |
" "$context" |
| 136 |
done |
| 137 |
} |
| 138 |
|
| 139 |
cmd_fetch() { |
| 140 |
read -ra urls <<<"$(pixel_test_logs_urls "${@:-}")" |
| 141 |
if [ "${#urls[@]}" -eq 0 ]; then |
| 142 |
echo >&2 "Can't find test results for $(cat test/reference-image), sorry." |
| 143 |
exit 1 |
| 144 |
elif [ "${#urls[@]}" -ne ${#@} ]; then |
| 145 |
echo "Something went wrong fetching the links, aborting early" |
| 146 |
exit 1 |
| 147 |
fi |
| 148 |
cmd_update |
| 149 |
for url in "${urls[@]}"; do |
| 150 |
url=${url/\/log.html/} |
| 151 |
echo "Fetching new pixel test references from $url" |
| 152 |
pixels=$(curl -s "$url/index.html" | grep '[^=><"]*-pixels.png' -o | uniq) |
| 153 |
for f in ${pixels}; do |
| 154 |
echo "$f" |
| 155 |
curl -s --output-dir test/reference/ -O "$url/$f" |
| 156 |
done |
| 157 |
done |
| 158 |
} |
| 159 |
|
| 160 |
cmd_help() { |
| 161 |
cat <<EOF |
| 162 |
$0 - Maintain the test/reference directory |
| 163 |
|
| 164 |
The following commands are available: |
| 165 |
|
| 166 |
update - Ensure that test/reference matches the checked out branch |
| 167 |
|
| 168 |
The test/reference directory is a git submodule, and it needs to |
| 169 |
be kept in sync with the main worktree (just like any other |
| 170 |
submodule). This is necessary whenever you check out a different |
| 171 |
branch and then start working on pixel tests, for example. |
| 172 |
|
| 173 |
You can use "git checkout --recurse-submodules" when changing |
| 174 |
branches to achieve this, or some variation of "git submodule |
| 175 |
update test/reference", or run "pixel-tests update". |
| 176 |
|
| 177 |
Most other pixel-tests commands will run "pixel-tests update" |
| 178 |
automatically, so you might not actually need to run it explicitly |
| 179 |
very often, but it's good to remember that this step is |
| 180 |
unfortunately necessary. |
| 181 |
|
| 182 |
Like other ways to update submodules, this command will not |
| 183 |
overwrite your local changes, so it is safe to use often and "just |
| 184 |
in case". If your local changes conflict with the changes that |
| 185 |
would be necessary for updating test/reference, the update will |
| 186 |
not be done. |
| 187 |
|
| 188 |
When "pixel-tests update" fails, it is probably easiest to throw |
| 189 |
away local changes with "pixel-tests reset" and re-acquire the new |
| 190 |
reference images that you want to install, maybe with "pixel-tests |
| 191 |
fetch". |
| 192 |
|
| 193 |
pull - Old name for "update". |
| 194 |
|
| 195 |
reset - Throw away local changes |
| 196 |
|
| 197 |
If you want to get rid of unpushed local changes, run "pixel-tests |
| 198 |
reset". This will remove the test/reference directory completely |
| 199 |
and then run "pixel-tests update" to recreate it. |
| 200 |
|
| 201 |
status - Show local changes |
| 202 |
|
| 203 |
After writing new reference images into test/reference, running |
| 204 |
"pixel-test status" will summarize the local changes. It will |
| 205 |
list all images that have been modified (with a "M" prefix), added |
| 206 |
("A"), or deleted ("D"). |
| 207 |
|
| 208 |
The "status" command will run "update" as the first step, so it |
| 209 |
might fail if it detects conflicts. To avoid that risk, run |
| 210 |
"update" explicitly before making any changes in test/reference. |
| 211 |
|
| 212 |
push - Upload local changes and prepare test/reference for committing |
| 213 |
|
| 214 |
Once you have finished writing new reference images into |
| 215 |
test/reference (maybe by running "pixel-tests fetch") and are |
| 216 |
ready to make them part of your pull request, you need to upload |
| 217 |
them to Github and record them in the main source repository. |
| 218 |
|
| 219 |
First, run "pixel-tests push" to do the uploading and to stage a |
| 220 |
changed test/reference in the main source repository. |
| 221 |
|
| 222 |
In the main source reository, test/reference is a special kind of |
| 223 |
object. It's not a file, or directory, and not even a symlink |
| 224 |
(although it is similar to a symlink). It's a "gitlink". But |
| 225 |
whatever it is, changes to it need to be staged, committed, and |
| 226 |
pushed, just like changes to regular files. |
| 227 |
|
| 228 |
When "pixel-tests push" is done, the change to test/reference has |
| 229 |
already been staged with "git add" to remind you that there is |
| 230 |
something to commit. |
| 231 |
|
| 232 |
Committing the change is identical to committing any other change. |
| 233 |
|
| 234 |
"push" will also run "update" as the first step. |
| 235 |
|
| 236 |
fetch - Download fresh reference images from a test run |
| 237 |
|
| 238 |
When code or tests are changed, we often need to install new or |
| 239 |
changed reference images for the pixel tests. A good way to do |
| 240 |
that is to run the tests in our CI machinery, let them fail, and |
| 241 |
grab the new reference images from the test log directory. |
| 242 |
Running "pixel-tests fetch" can automate this. |
| 243 |
|
| 244 |
Without any arguments, "fetch" will figure out all by itself where |
| 245 |
to download the images from. For this to work, your "origin" |
| 246 |
remote needs to point to the repository on Github that the current |
| 247 |
PR will be merged into. For our main Cockpit repository, that |
| 248 |
would be "cockpit-project/cockpit", for example. |
| 249 |
|
| 250 |
You can also pass a PR number to "fetch" if the current branch |
| 251 |
doesn't correspond to the PR with the new pixels. Or you can pass |
| 252 |
the URL of the log results. |
| 253 |
|
| 254 |
"push" will also run "update" as the first step. |
| 255 |
EOF |
| 256 |
} |
| 257 |
|
| 258 |
main() { |
| 259 |
local cmd="${1-}" |
| 260 |
|
| 261 |
if [ -z "${cmd}" ]; then |
| 262 |
echo 'This command requires a subcommand: update status push reset fetch' |
| 263 |
echo "Run '$0 help' for a longer explanation" |
| 264 |
exit 1 |
| 265 |
elif ! type -t "cmd_${cmd}" | grep -q function; then |
| 266 |
echo "Unknown subcommand ${cmd}" |
| 267 |
exit 1 |
| 268 |
fi |
| 269 |
|
| 270 |
shift |
| 271 |
[ "${V-0}" = 0 ] || set -x |
| 272 |
"cmd_$cmd" "$@" |
| 273 |
} |
| 274 |
|
| 275 |
main "$@" |