build_static_site.sh: create target if nonexistent (#695)

Summary:
The current version of the build script has the safe but annoying
property that the target directory must be an existing, empty directory.
It seems reasonable and convenient to allow the build script to create
the directory with `mkdir -p`. It still fails if the directory is not
empty or is a file.

Test Plan:
Unit tests updated; run `yarn sharness-full`.

wchargin-branch: build-mkdir-p
This commit is contained in:
William Chargin 2018-08-21 15:35:13 -07:00 committed by GitHub
parent 3216f5596e
commit d839fcae95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 18 deletions

View File

@ -45,9 +45,7 @@ parse_args() {
fi fi
shift shift
if [ $# -eq 0 ]; then die 'missing value for --target'; fi if [ $# -eq 0 ]; then die 'missing value for --target'; fi
if ! target="$(readlink -e "$1")"; then target="$1"
die "target does not exist: $1"
fi
;; ;;
--repo) --repo)
shift shift
@ -79,12 +77,16 @@ parse_args() {
if [ -z "${target}" ]; then if [ -z "${target}" ]; then
die 'target directory not specified' die 'target directory not specified'
fi fi
if ! [ -e "${target}" ]; then
mkdir -p -- "${target}"
fi
if ! [ -d "${target}" ]; then if ! [ -d "${target}" ]; then
die "target is not a directory: ${target}" die "target is not a directory: ${target}"
fi fi
if [ "$(command ls -A "${target}" | wc -l)" != 0 ]; then if [ "$(command ls -A "${target}" | wc -l)" != 0 ]; then
die "target directory is nonempty: ${target}" die "target directory is nonempty: ${target}"
fi fi
target="$(readlink -e "${target}")"
} }
build() { build() {

View File

@ -48,21 +48,17 @@ test_expect_success "should fail with multiple targets" '
grep -qF -- "--target specified multiple times" err grep -qF -- "--target specified multiple times" err
' '
test_expect_success "should fail with nonexistent targets" '
test_must_fail run --target wat 2>err &&
grep -qF -- "target does not exist: wat" err
'
test_expect_success "should fail with nonexistent targets with subcomponents" '
# using "readlink -f", this behavior can be different.
test_must_fail run --target wat/wat 2>err &&
grep -qF -- "target does not exist: wat/wat" err
'
test_expect_success "should fail with a file as target" ' test_expect_success "should fail with a file as target" '
printf "important\nstuff" >important_data && printf "important\nstuff" >important_data &&
test_must_fail run --target important_data 2>err && test_must_fail run --target important_data 2>err &&
grep -qF -- "target is not a directory: ${PWD}/important_data" err && grep -qF -- "target is not a directory" err &&
printf "important\nstuff" | test_cmp - important_data
'
test_expect_success "should fail with a target under a file" '
printf "important\nstuff" >important_data &&
test_must_fail run --target important_data/something 2>err &&
grep -q -- "cannot create directory.*Not a directory" err &&
printf "important\nstuff" | test_cmp - important_data printf "important\nstuff" | test_cmp - important_data
' '
@ -70,7 +66,7 @@ test_expect_success "should fail with a nonempty directory as target" '
mkdir important_dir && mkdir important_dir &&
printf "redacted\n" >important_dir/.wallet.dat && printf "redacted\n" >important_dir/.wallet.dat &&
test_must_fail run --target important_dir 2>err && test_must_fail run --target important_dir 2>err &&
grep -qF -- "target directory is nonempty: ${PWD}/important_dir" err && grep -qF -- "target directory is nonempty: important_dir" err &&
printf "redacted\n" | test_cmp - important_dir/.wallet.dat printf "redacted\n" | test_cmp - important_dir/.wallet.dat
' '
@ -116,7 +112,7 @@ fi
run_build() { run_build() {
prereq_name="$1"; shift prereq_name="$1"; shift
description="$1"; shift description="$1"; shift
output_dir="output_${prereq_name}" output_dir="build_output/output_${prereq_name}"
api_dir="${output_dir}/api/v1/data" api_dir="${output_dir}/api/v1/data"
data_dir="${api_dir}/data" data_dir="${api_dir}/data"
for arg in "${output_dir}" "$@"; do for arg in "${output_dir}" "$@"; do
@ -129,7 +125,6 @@ run_build() {
flags="--target $output_dir $*" # checked for sanity above flags="--target $output_dir $*" # checked for sanity above
test_expect_success EXPENSIVE,HAVE_GITHUB_TOKEN \ test_expect_success EXPENSIVE,HAVE_GITHUB_TOKEN \
"${prereq_name}: ${description}" ' "${prereq_name}: ${description}" '
mkdir "${output_dir}" &&
run '"${flags}"' 2>err && run '"${flags}"' 2>err &&
test_must_fail grep -vF \ test_must_fail grep -vF \
-e "Removing build directory: " \ -e "Removing build directory: " \