Skip to content

Instantly share code, notes, and snippets.

@dorner
Created November 28, 2021 01:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dorner/edf8ffe9b7285b06d2ed0f107c1ad0b2 to your computer and use it in GitHub Desktop.
Save dorner/edf8ffe9b7285b06d2ed0f107c1ad0b2 to your computer and use it in GitHub Desktop.
const fs = require("fs")
const path = require("path")
const files = fs.readdirSync(process.env.CALL_DIR)
const outputs = `${process.env.CALL_DIR}/../outputs`
if (!fs.existsSync(outputs)) {
console.log(process.cwd())
console.log("outputs directory does not exist - use UPDATE_SNAPSHOTS=1 to create")
process.exit(1)
}
const expectedFiles = fs.readdirSync(outputs)
let foundError = false;
const extraFiles = files.filter(x => x.endsWith(".calls") && !expectedFiles.includes(x));
if (extraFiles.length > 0) {
foundError = true;
console.log(`The following files were output but there are no expected outputs for them! ${extraFiles.join(", ")}`)
}
const missingFiles = expectedFiles.filter(x => x.endsWith(".calls") && !files.includes(x))
if (missingFiles.length > 0) {
foundError = true;
console.log(`The following files were expected to be output but weren't! ${missingFiles.join(", ")}`)
}
files.forEach((file) => {
if (file.endsWith('.calls') || file === 'full-output.txt') {
const firstFile = fs.readFileSync(`${process.env.CALL_DIR}/${file}`);
if (!fs.existsSync(`${outputs}/${path.basename(file)}`)) {
console.log(`${outputs}/${path.basename(file)} does not exist!`)
foundError = true
return;
}
const secondFile = fs.readFileSync(`${outputs}/${path.basename(file)}`)
if (!firstFile.equals(secondFile)) {
console.log(`${file} contents are not equal!`)
foundError = true
}
}
})
process.exit(foundError ? 1 : 0)
#!/usr/bin/env bats
load ./shared
@test "default env variables" {
performTest "defaults"
}
# https://stackoverflow.com/questions/370047/what-is-the-most-elegant-way-to-remove-a-path-from-the-path-variable-in-bash
path_append () { path_remove "$1"; export PATH="$PATH:$1"; }
path_prepend () { path_remove "$1"; export PATH="$1:$PATH"; }
path_remove () { export PATH; PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: '$0 != "'"$1"'"' | sed 's/:$//'); }
# Set variable to the given default ONLY if it's not set at all
# Call: exportVariable VAR_NAME DEFAULT_VALUE
function exportVariable {
VARIABLE=$1
DEFAULT=$2
if [[ -z ${!VARIABLE+x} ]]; then # is the variable completely unset, even to a blank value
eval $VARIABLE=$DEFAULT
fi
export $VARIABLE
}
function setupTest {
mkdir current_calls
CALL_DIR="$(pwd)/current_calls"
export CALL_DIR
BASE_TEST_DIR=$(pwd)
export BASE_TEST_DIR
cp ../../../../platform .
# ... more variables here
exportVariable GITHUB_REPOSITORY "my-service"
exportVariable GITHUB_SHA "github123"
export GITHUB_RUN_NUMBER="123"
export PLATFORM_TEST="true"
if [[ -z $USE_CONFIG_FILE ]]; then
exportVariable POLL_DEPLOYMENT_STATUS "false"
exportVariable SERVICE_NAME "my-service"
exportVariable SERVICE_PROFILE "INTERNET-FACING"
exportVariable SERVICE_TOKEN "service-token"
exportVariable SERVICE_CONTAINER_NAME "my-service-stg"
exportVariable CONTAINER_TAG 'branch-123'
fi
}
function cleanup {
rm -f ./platform
rm -Rrf ./current_calls
}
function fullTest {
performTest "$@"
}
function setup_file {
pushd ..
go build -o platform go/cmd/platform_deploy/main.go
popd
}
# $1 is the directory to perform against
function performTest {
TEST_FILE=$(basename $BATS_TEST_FILENAME)
SUITE=${TEST_FILE%.*}
pushd "suites/$SUITE/$1" || exit
cleanup
setupTest
set +e
./platform service "${@:2}" > $CALL_DIR/full-output.txt # pass all but the first parameter to the script
MAIN_EXIT_CODE=$?
perl -pi -e "s^${BASE_TEST_DIR}^%%BASE_TEST_DIR%%^g" $CALL_DIR/*
perl -pi -e 's^\\e^%%ESC%%^g' $CALL_DIR/* # escape byte printed differently in Linux vs Mac
perl -pi -e "s^\\e^%%ESC%%^g" $CALL_DIR/* # need to do it both ways
if [[ -n $UPDATE_SNAPSHOTS ]]; then
rm -rf outputs
mkdir outputs
cp -R $CALL_DIR/* outputs
fi
node ../../../scripts/compare.js
COMPARE_EXIT_CODE=$?
if [[ "$COMPARE_EXIT_CODE" -eq "0" && "$MAIN_EXIT_CODE" -eq "0" && -z $EXPECT_FAILURE ]]; then # as expected, no failure
cleanup
fi
if [[ "$COMPARE_EXIT_CODE" -eq "0" && "$MAIN_EXIT_CODE" -ne "0" && -n $EXPECT_FAILURE ]]; then # as expected, failure
cleanup
fi
popd || exit
if [[ "$COMPARE_EXIT_CODE" -ne 0 ]]; then
echo "File comparison failed!"
exit 1
fi
if [[ "$MAIN_EXIT_CODE" -ne 0 && -z $EXPECT_FAILURE ]]; then # did not expect failure but it failed
echo "Expected success code but got error code $MAIN_EXIT_CODE"
exit 1
fi
if [[ "$MAIN_EXIT_CODE" -eq "0" && -n $EXPECT_FAILURE ]]; then # expected failure but it didn't fail
echo "Expected error code but got exit code 0 (success)"
exit 1
fi
}
function setup {
export EXPECT_FAILURE=""
path_prepend "$(pwd)/scripts"
unset SERVICE_NAME
unset SERVICE_PROFILE
unset SERVICE_TOKEN
unset SERVICE_CONTAINER_NAME
unset POLL_DEPLOYMENT_STATUS
unset CONTAINER_IMAGE
unset GITHUB_REF
unset GITHUB_REPOSITORY
unset GITHUB_SHA
}
function teardown {
path_remove "$(pwd)/scripts"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment