sourcecred/sharness/test_cli_scores.t
Dandelion Mané 88f736d180
add sourcecred/scores (#1223)
The scores are lightly processed from their internal representation.
Example usage:

```
$ yarn backend;
$ node bin/sourcecred.js load sourcecred/sourcecred
$ node bin/sourcecred.js scores sourcecred/sourcecred > scores.json
```

The data structure is as follows:

```js
export type NodeOutput = {|
  +id: string,
  +totalCred: number,
  +intervalCred: $ReadOnlyArray<number>,
|};

export type ScoreOutput = Compatible<{|
  +users: $ReadOnlyArray<NodeOutput>,
  +intervals: $ReadOnlyArray<Interval>,
|}>;
```

Test plan: I added sharness tests at `sharness/test_cli_scores.t`.
In the past, we've used javascript tests for CLI commands. However,
those are pretty time-consuming to write, and are less robust than
simply running the command from bash. Check the snapshot for a sense of
what the new data format looks like. Also, the snapshot updater now
updates this snapshot too.

Relevant for #1047.
Thanks to @Beanow for feedback on the output format and design.
Thanks to @wchargin for help in code review.
2019-07-14 17:05:13 +01:00

97 lines
2.9 KiB
Bash
Executable File

#!/bin/sh
# Disable these lint rules globally:
# 2034 = unused variable (used by sharness)
# 2016 = parameter expansion in single quotes
# 1004 = backslash-newline in single quotes
# shellcheck disable=SC2034,SC2016,SC1004
:
test_description='tests for cli/scores.js'
export GIT_CONFIG_NOSYSTEM=1
export GIT_ATTR_NOSYSTEM=1
# shellcheck disable=SC1091
. ./sharness.sh
test_expect_success "environment and Node linking setup" '
toplevel="$(git -C "$(dirname "$0")" rev-parse --show-toplevel)" &&
snapshot_directory="${toplevel}/sharness/__snapshots__/" &&
SOURCECRED_DIRECTORY="${snapshot_directory}/example-github-load" &&
export SOURCECRED_DIRECTORY &&
snapshot_file="${snapshot_directory}/example-github-scores.json" &&
if [ -z "${SOURCECRED_BIN}" ]; then
printf >&2 "warn: missing environment variable SOURCECRED_BIN\n" &&
printf >&2 "warn: using repository bin directory as fallback\n" &&
export SOURCECRED_BIN="${toplevel}/bin"
fi &&
export NODE_PATH="${toplevel}/node_modules${NODE_PATH:+:${NODE_PATH}}" &&
test_set_prereq SETUP
'
run() (
set -eu
rm -f out err
code=0
node "${SOURCECRED_BIN}"/sourcecred.js "$@" >out 2>err || code=$?
if [ "${code}" -ne 0 ]; then
printf '%s failed with %d\n' "sourcecred $*"
printf 'stdout:\n'
cat out
printf 'stderr:\n'
cat err
fi
)
# Use this instead of `run` when we are expecting sourcecred to return a
# non-zero exit code
run_without_validation() (
set -eu
rm -f out err
node "${SOURCECRED_BIN}"/sourcecred.js "$@" >out 2>err
)
test_expect_success SETUP "should print help message when called without args" '
test_must_fail run_without_validation scores &&
grep -q "no repository ID provided" err &&
grep -q "sourcecred help scores" err
'
test_expect_success SETUP "help should print usage info" '
run help scores &&
grep -q "usage: sourcecred scores REPO_ID" out
'
test_expect_success SETUP "--help should print usage info" '
run scores --help &&
grep -q "usage: sourcecred scores REPO_ID" out
'
test_expect_success SETUP "should fail for multiple repos" '
test_must_fail run_without_validation scores sourcecred/sourcecred torvalds/linux &&
grep -q "fatal: multiple repository IDs provided" err
'
test_expect_success SETUP "should fail for unloaded repo" '
test_must_fail run_without_validation scores torvalds/linux &&
grep -q "fatal: repository ID torvalds/linux not loaded" err
'
if [ -n "${UPDATE_SNAPSHOT}" ]; then
test_set_prereq UPDATE_SNAPSHOT
fi
test_expect_success SETUP,UPDATE_SNAPSHOT "should update the snapshot" '
run scores sourcecred/example-github &&
mv out "${snapshot_file}"
'
test_expect_success SETUP "should be identical to the snapshot" '
run scores sourcecred/example-github &&
diff -u out ${snapshot_file}
'
test_done
# vim: ft=sh