test 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. set -e
  3. cd "$(dirname "$0")/.."
  4. RED='\033[0;31m'
  5. GREEN='\033[0;32m'
  6. YELLOW='\033[0;33m'
  7. NC='\033[0m' # No Color
  8. function prism_is_running() {
  9. curl --silent "http://localhost:4010" >/dev/null 2>&1
  10. }
  11. kill_server_on_port() {
  12. pids=$(lsof -t -i tcp:"$1" || echo "")
  13. if [ "$pids" != "" ]; then
  14. kill "$pids"
  15. echo "Stopped $pids."
  16. fi
  17. }
  18. function is_overriding_api_base_url() {
  19. [ -n "$TEST_API_BASE_URL" ]
  20. }
  21. if ! is_overriding_api_base_url && ! prism_is_running ; then
  22. # When we exit this script, make sure to kill the background mock server process
  23. trap 'kill_server_on_port 4010' EXIT
  24. # Start the dev server
  25. ./scripts/mock --daemon
  26. fi
  27. if is_overriding_api_base_url ; then
  28. echo -e "${GREEN}✔ Running tests against ${TEST_API_BASE_URL}${NC}"
  29. echo
  30. elif ! prism_is_running ; then
  31. echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Prism server"
  32. echo -e "running against your OpenAPI spec."
  33. echo
  34. echo -e "To run the server, pass in the path or url of your OpenAPI"
  35. echo -e "spec to the prism command:"
  36. echo
  37. echo -e " \$ ${YELLOW}npm exec --package=@stoplight/prism-cli@~5.3.2 -- prism mock path/to/your.openapi.yml${NC}"
  38. echo
  39. exit 1
  40. else
  41. echo -e "${GREEN}✔ Mock prism server is running with your OpenAPI spec${NC}"
  42. echo
  43. fi
  44. echo "==> Running tests"
  45. go test ./... "$@"