test: invoke `yarn backend` only once (#784)

Summary:
Lots of tests need the output of `yarn backend`. Before this commit,
they tended to create it themselves. This was slow and wasteful, and
also could in principle have race conditions (though in practice usually
tended not to).

This commit updates tests to respect a `SOURCECRED_BIN` environment
variable indicating the path to an existing directory of backend
applications.

Closes #765.

Test Plan:
Running `yarn test --full` passes.

Prepending `echo run >>/tmp/log &&` to the `backend` script in
`package.json` and running `yarn test --full` results in a log file
containing only one line, indicating that the script really is run only
once.

wchargin-branch: deduplicate-backend
This commit is contained in:
William Chargin 2018-09-05 12:47:54 -07:00 committed by GitHub
parent bd7b7dec82
commit 3dda4ab35c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 40 deletions

View File

@ -18,6 +18,18 @@ function main() {
} }
function makeTasks(mode /*: "BASIC" | "FULL" */) { function makeTasks(mode /*: "BASIC" | "FULL" */) {
const backendOutput = tmp.dirSync({
unsafeCleanup: true,
prefix: "sourcecred-test-",
}).name;
console.log("tmpdir for backend output: " + backendOutput);
function withSourcecredBinEnv(
invocation /*: $ReadOnlyArray<string> */
) /*: string[] */ {
return ["env", "SOURCECRED_BIN=" + backendOutput, ...invocation];
}
const basicTasks = [ const basicTasks = [
{ {
id: "ensure-flow-typing", id: "ensure-flow-typing",
@ -59,16 +71,6 @@ function makeTasks(mode /*: "BASIC" | "FULL" */) {
cmd: ["npm", "run", "--silent", "unit"], cmd: ["npm", "run", "--silent", "unit"],
deps: [], deps: [],
}, },
{
id: {BASIC: "sharness", FULL: "sharness-full"}[mode],
cmd: [
"npm",
"run",
"--silent",
{BASIC: "sharness", FULL: "sharness-full"}[mode],
],
deps: [],
},
{ {
id: "backend", id: "backend",
cmd: [ cmd: [
@ -78,32 +80,37 @@ function makeTasks(mode /*: "BASIC" | "FULL" */) {
"backend", "backend",
"--", "--",
"--output-path", "--output-path",
tmp.dirSync({ backendOutput,
unsafeCleanup: true,
prefix: "sourcecred-backend-dry-run-",
}).name,
], ],
deps: [], deps: [],
}, },
{
id: {BASIC: "sharness", FULL: "sharness-full"}[mode],
cmd: withSourcecredBinEnv([
"npm",
"run",
"--silent",
{BASIC: "sharness", FULL: "sharness-full"}[mode],
]),
deps: ["backend"],
},
]; ];
const extraTasks = [ const extraTasks = [
{
id: "backend-in-place",
cmd: ["npm", "run", "--silent", "backend"],
// This task depends on `check-pretty` in order to work around a
// race condition in Prettier:
// https://github.com/prettier/prettier/issues/4468
deps: ["check-pretty"],
},
{ {
id: "fetchGithubRepoTest", id: "fetchGithubRepoTest",
cmd: ["./src/plugins/github/fetchGithubRepoTest.sh", "--no-build"], cmd: withSourcecredBinEnv([
deps: ["backend-in-place"], "./src/plugins/github/fetchGithubRepoTest.sh",
"--no-build",
]),
deps: ["backend"],
}, },
{ {
id: "loadRepositoryTest", id: "loadRepositoryTest",
cmd: ["./src/plugins/git/loadRepositoryTest.sh", "--no-build"], cmd: withSourcecredBinEnv([
deps: ["backend-in-place"], "./src/plugins/git/loadRepositoryTest.sh",
"--no-build",
]),
deps: ["backend"],
}, },
]; ];
switch (mode) { switch (mode) {

View File

@ -6,6 +6,7 @@ usage() {
printf ' [--repo OWNER/NAME [...]]\n' printf ' [--repo OWNER/NAME [...]]\n'
printf ' [--feedback-url URL]\n' printf ' [--feedback-url URL]\n'
printf ' [--cname DOMAIN]\n' printf ' [--cname DOMAIN]\n'
printf ' [--no-backend]\n'
printf ' [-h|--help]\n' printf ' [-h|--help]\n'
printf '\n' printf '\n'
printf 'Build the static SourceCred website, including example data.\n' printf 'Build the static SourceCred website, including example data.\n'
@ -20,8 +21,18 @@ usage() {
printf '%s\n' '--cname DOMAIN' printf '%s\n' '--cname DOMAIN'
printf '\t%s\n' 'configure DNS for a GitHub Pages site to point to' printf '\t%s\n' 'configure DNS for a GitHub Pages site to point to'
printf '\t%s\n' 'the provided custom domain' printf '\t%s\n' 'the provided custom domain'
printf '%s\n' '--no-backend'
printf '\t%s\n' 'do not run "yarn backend"; see also the SOURCECRED_BIN'
printf '\t%s\n' 'environment variable'
printf '%s\n' '-h|--help' printf '%s\n' '-h|--help'
printf '\t%s\n' 'show this message' printf '\t%s\n' 'show this message'
printf '\n'
printf 'Environment variables:\n'
printf '\n'
printf '%s\n' 'SOURCECRED_BIN'
printf '\t%s\n' 'When using --no-backend, directory containing the'
printf '\t%s\n' 'SourceCred executables (output of "yarn backend").'
printf '\t%s\n' 'Default is ./bin. Ignored without --no-backend.'
} }
main() { main() {
@ -31,6 +42,7 @@ main() {
cd "${toplevel}" cd "${toplevel}"
sourcecred_data= sourcecred_data=
sourcecred_bin=
trap cleanup EXIT trap cleanup EXIT
build build
@ -38,6 +50,7 @@ main() {
parse_args() { parse_args() {
unset SOURCECRED_FEEDBACK_URL unset SOURCECRED_FEEDBACK_URL
BACKEND=1
target= target=
cname= cname=
repos=( ) repos=( )
@ -78,6 +91,9 @@ parse_args() {
die 'empty value for --cname' die 'empty value for --cname'
fi fi
;; ;;
--no-backend)
BACKEND=0
;;
-h|--help) -h|--help)
usage usage
exit 0 exit 0
@ -102,24 +118,26 @@ parse_args() {
die "target directory is nonempty: ${target}" die "target directory is nonempty: ${target}"
fi fi
target="$(readlink -e "${target}")" target="$(readlink -e "${target}")"
: "${SOURCECRED_BIN:=./bin}"
} }
build() { build() {
sourcecred_data="$(mktemp -d --suffix ".sourcecred-data")" sourcecred_data="$(mktemp -d --suffix ".sourcecred-data")"
export SOURCECRED_DIRECTORY="${sourcecred_data}" export SOURCECRED_DIRECTORY="${sourcecred_data}"
yarn if [ "${BACKEND}" -ne 0 ]; then
# shellcheck disable=SC2016 sourcecred_bin="$(mktemp -d --suffix ".sourcecred-bin")"
printf >&2 'warn: running `yarn backend`, overwriting `bin/` in your repo\n' export SOURCECRED_BIN="${sourcecred_bin}"
printf >&2 'warn: if this offends you, please see: %s\n' \ yarn
'https://github.com/sourcecred/sourcecred/issues/580' yarn -s backend --output-path "${SOURCECRED_BIN}"
yarn backend fi
yarn build --output-path "${target}" yarn -s build --output-path "${target}"
if [ "${#repos[@]}" -ne 0 ]; then if [ "${#repos[@]}" -ne 0 ]; then
for repo in "${repos[@]}"; do for repo in "${repos[@]}"; do
printf >&2 'info: loading repository: %s\n' "${repo}" printf >&2 'info: loading repository: %s\n' "${repo}"
node ./bin/sourcecred.js load "${repo}" NODE_PATH="./node_modules${NODE_PATH:+:${NODE_PATH}}" \
node "${SOURCECRED_BIN:-./bin}/sourcecred.js" load "${repo}"
done done
fi fi
@ -147,7 +165,8 @@ build() {
} }
cleanup() { cleanup() {
if [ -d "${sourcecred_data}" ]; then rm -rf "${sourcecred_data}"; fi if [ -d "${sourcecred_data:-}" ]; then rm -rf "${sourcecred_data}"; fi
if [ -d "${sourcecred_bin:-}" ]; then rm -rf "${sourcecred_bin}"; fi
} }
die() { die() {

View File

@ -152,8 +152,6 @@ run_build() {
run '"${flags}"' 2>err && run '"${flags}"' 2>err &&
test_must_fail grep -vF \ test_must_fail grep -vF \
-e "Removing contents of build directory: " \ -e "Removing contents of build directory: " \
-e "warn: running \`yarn backend\`" \
-e "warn: if this offends you" \
-e "info: loading repository" \ -e "info: loading repository" \
err && err &&
test_path_is_dir "${output_dir}" && test_path_is_dir "${output_dir}" &&
@ -215,6 +213,7 @@ test_pages() {
run_build TWO_REPOS \ run_build TWO_REPOS \
"should build the site with two repositories and a CNAME" \ "should build the site with two repositories and a CNAME" \
--no-backend \
--cname sourcecred.example.com \ --cname sourcecred.example.com \
--feedback-url http://discuss.example.com/feedback/ \ --feedback-url http://discuss.example.com/feedback/ \
--repo sourcecred/example-git \ --repo sourcecred/example-git \
@ -255,6 +254,7 @@ test_expect_success TWO_REPOS "TWO_REPOS: should have a correct CNAME record" '
SOURCECRED_FEEDBACK_URL=http://wat.com/wat \ SOURCECRED_FEEDBACK_URL=http://wat.com/wat \
run_build NO_REPOS \ run_build NO_REPOS \
"should build the site with no repositories and no CNAME" \ "should build the site with no repositories and no CNAME" \
--no-backend \
# no arguments here # no arguments here
test_pages NO_REPOS test_pages NO_REPOS

View File

@ -14,12 +14,17 @@ usage() {
printf ' Default is --build.\n' printf ' Default is --build.\n'
printf ' --help\n' printf ' --help\n'
printf ' Show this message\n' printf ' Show this message\n'
printf '\n'
printf 'Environment variables:'
printf ' SOURCECRED_BIN\n'
printf ' When using --no-build, directory containing the SourceCred\n'
printf ' executables (output of "yarn backend"). Default is ./bin.\n'
} }
fetch() { fetch() {
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
node bin/createExampleRepo.js "${tmpdir}" node "${SOURCECRED_BIN:-./bin}/createExampleRepo.js" "${tmpdir}"
node bin/loadAndPrintGitRepository.js "${tmpdir}" node "${SOURCECRED_BIN:-./bin}/loadAndPrintGitRepository.js" "${tmpdir}"
rm -rf "${tmpdir}" rm -rf "${tmpdir}"
} }
@ -35,6 +40,10 @@ update() {
} }
main() { main() {
if [ -n "${SOURCECRED_BIN:-}" ]; then
SOURCECRED_BIN="$(readlink -f "${SOURCECRED_BIN}")"
fi
cd "$(git rev-parse --show-toplevel)"
UPDATE= UPDATE=
BUILD=1 BUILD=1
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
@ -54,7 +63,10 @@ main() {
shift shift
done done
if [ -n "${BUILD}" ]; then if [ -n "${BUILD}" ]; then
unset SOURCECRED_BIN
yarn backend yarn backend
else
export NODE_PATH=./node_modules"${NODE_PATH:+:${NODE_PATH}}"
fi fi
if [ -n "${UPDATE}" ]; then if [ -n "${UPDATE}" ]; then
update update

View File

@ -16,6 +16,11 @@ usage() {
printf ' Default is --build.\n' printf ' Default is --build.\n'
printf ' --help\n' printf ' --help\n'
printf ' Show this message\n' printf ' Show this message\n'
printf '\n'
printf 'Environment variables:'
printf ' SOURCECRED_BIN\n'
printf ' When using --no-build, directory containing the SourceCred\n'
printf ' executables (output of "yarn backend"). Default is ./bin.\n'
} }
fetch() { fetch() {
@ -24,7 +29,7 @@ fetch() {
printf >&2 'to a 40-character hex string API token from GitHub.\n' printf >&2 'to a 40-character hex string API token from GitHub.\n'
return 1 return 1
fi fi
node bin/fetchAndPrintGithubRepo.js \ node "${SOURCECRED_BIN:-./bin}/fetchAndPrintGithubRepo.js" \
sourcecred example-github "${GITHUB_TOKEN}" sourcecred example-github "${GITHUB_TOKEN}"
} }
@ -40,6 +45,9 @@ update() {
} }
main() { main() {
if [ -n "${SOURCECRED_BIN:-}" ]; then
SOURCECRED_BIN="$(readlink -f "${SOURCECRED_BIN}")"
fi
cd "$(git rev-parse --show-toplevel)" cd "$(git rev-parse --show-toplevel)"
UPDATE= UPDATE=
BUILD=1 BUILD=1
@ -60,7 +68,10 @@ main() {
shift shift
done done
if [ -n "${BUILD}" ]; then if [ -n "${BUILD}" ]; then
unset SOURCECRED_BIN
yarn backend yarn backend
else
export NODE_PATH="./node_modules${NODE_PATH:+:${NODE_PATH}}"
fi fi
if [ -n "${UPDATE}" ]; then if [ -n "${UPDATE}" ]; then
update update