From b22ee2d88ea5fb8e1adba30d155ace76dc737a45 Mon Sep 17 00:00:00 2001 From: yuxuan-ms <43603130+yuxuan-ms@users.noreply.github.com> Date: Wed, 26 Aug 2020 15:28:22 +0800 Subject: [PATCH] fix test --- .travis.yml | 2 +- testplan/testing/multitest/driver/kafka.py | 6 +- testplan/web_ui/testing/.env | 60 +++++++++++---- .../state/reportActions/fetchReport.js | 76 +++++++++---------- 4 files changed, 87 insertions(+), 57 deletions(-) diff --git a/.travis.yml b/.travis.yml index 376edbb72..336cdbf9c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ language: python env: - BLACK_VERSION: "19.10b0" global: - - REACT_APP_API_BASE_URL: "/api" + - REACT_APP_API_BASE_URL: "/fake/api/endpoint" python: - "2.7" diff --git a/testplan/testing/multitest/driver/kafka.py b/testplan/testing/multitest/driver/kafka.py index ff86ef1c3..7bb03e3d0 100644 --- a/testplan/testing/multitest/driver/kafka.py +++ b/testplan/testing/multitest/driver/kafka.py @@ -34,11 +34,11 @@ class KafkaStandalone(app.App): """ Driver for starting a Kafka instance in standalone mode. - :param cfg_template: Zookeeper config file template. + :param cfg_template: Kafka config file template. :type cfg_template: ``str`` - :param binary: zkServer.sh file path. + :param binary: kafka-server-start.sh file path. :type binary: ``str`` - :param port: Zookeeper listen port. Zookeeper doesn't support random port + :param port: Kafka listen port. :type port: ``int`` :param env: Environmental variables to be made available to Zookeeper process. :type env: ``dict`` diff --git a/testplan/web_ui/testing/.env b/testplan/web_ui/testing/.env index 3ae99d392..d0032cefe 100644 --- a/testplan/web_ui/testing/.env +++ b/testplan/web_ui/testing/.env @@ -1,16 +1,50 @@ -# when debugging locally, create a file ".env.local" next to this file -# and set this variable to the full base URL of your API endpoint. -# You can set this variable to just the API path base if your API uses -# he same hostname and scheme as the webserver serving this app (which is -# likely "http://localhost:3000" when debugging locally). The app will set -# basic CORS headers when NODE_ENV == "development" or "test". +# When debugging locally, create a file ".env.local" next to this file +# containing: + +REACT_APP_API_BASE_URL=/path/to/api/endpoint + +# ... if your API is served from the same origin as this webapp. For example, +# if this web app is available at "https://www.myapp.mil/" and the API is +# available at "https://www.myapp.mil/api" then you'd set: + +REACT_APP_API_BASE_URL=/api + +# If your API is served from a different origin (e.g. if you're debugging +# locally at http://localhost:3000) than your API (say it's still +# "https://www.myapp.mil/api") then set: + +REACT_APP_API_BASE_URL=https://www.myapp.mil/api + +# This will also work if the web app and API are served from the same origin. +# The app will set basic CORS headers when NODE_ENV == "development" or "test". +# More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS +# +#=============================================================================# +# Note that NOT setting REACT_APP_API_BASE_URL - either in the environment or # +# in another .env.* file - will result in a build error. # +#=============================================================================# # -# More info: -# - https://create-react-app.dev/docs/advanced-configuration/ -# - https://create-react-app.dev/docs/adding-custom-environment-variables/ -# - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS +# The .env.* files override one another like so (highest > lowest priority): +# > when NODE_ENV === "development": +# - (variables from environment) +# - .env.development.local +# - .env.development +# - .env.local +# - .env +# > when NODE_ENV === "production": +# - (variables from environment) +# - .env.production.local +# - .env.production +# - .env.local +# - .env +# > when NODE_ENV === "test": (omits .env.local) +# - (variables from environment) +# - .env.test.local +# - .env.test +# - .env # -# The following configuration simply picks up the base url from an environment -# variable. +# More info: +# - https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used +# - https://create-react-app.dev/docs/advanced-configuration # -REACT_APP_API_BASE_URL=${REACT_APP_API_BASE_URL} +REACT_APP_API_BASE_URL=OverrideMeOrThereWillBeABuildError diff --git a/testplan/web_ui/testing/src/Report/BatchReportBeta/state/reportActions/fetchReport.js b/testplan/web_ui/testing/src/Report/BatchReportBeta/state/reportActions/fetchReport.js index a8425ec1c..0adc1e44f 100644 --- a/testplan/web_ui/testing/src/Report/BatchReportBeta/state/reportActions/fetchReport.js +++ b/testplan/web_ui/testing/src/Report/BatchReportBeta/state/reportActions/fetchReport.js @@ -13,6 +13,37 @@ import { PropagateIndices } from '../../../reportUtils'; import { toPlainObjectIn, flattened } from '../../../../Common/utils'; const __DEV__ = process.env.NODE_ENV !== 'production'; +const API_BASE_URL = process.env.REACT_APP_API_BASE_URL; +const UNSET_API_BASE_URL_VAL = 'OverrideMeOrThereWillBeABuildError'; +/** @type {URL} */ +const API_BASE_URL_OBJ = (() => { + // these conditionals + try-catches are top-level so errors will be caught + // during the build. + // eslint-disable-next-line max-len + if(!_.isString(API_BASE_URL) || API_BASE_URL === UNSET_API_BASE_URL_VAL) { + throw new Error( + "The environment variable REACT_APP_API_BASE_URL must be set to your " + + "API's base URL. See this project's .env file for more information." + ); + } + let apiBaseUrlObj; + try { + // this will not error when API_BASE_URL is a full URI + apiBaseUrlObj = new URL(API_BASE_URL); + } catch(err1) { + try { + // this will not error when API_BASE_URL only a path + apiBaseUrlObj = new URL(API_BASE_URL, window.location.origin); + } catch(err2) { + throw new Error( + `The environment variable REACT_APP_API_BASE_URL is not set to a ` + + `valid URL or a URL path - received ` + + `REACT_APP_API_BASE_URL="${API_BASE_URL}".` + ); + } + } + return apiBaseUrlObj; +})(); let TEST_REPORTS = {}; // eslint-disable-line no-unused-vars if(__DEV__) { @@ -22,49 +53,14 @@ if(__DEV__) { }; } -/** @returns {URL} */ -function apiBaseUrlFromEnv() { - const prodErrMsg = 'API error: Please notify support with your logs.'; - const baseURL = process.env.REACT_APP_API_BASE_URL; - - if(!_.isString(baseURL)) { - let err; - if(__DEV__) { - err = new TypeError( - "The environment variable REACT_APP_API_BASE_URL must be set to your " + - "API's base URL. See this project's .env file for more information." - ); - } else { - err = new Error(prodErrMsg); - } - throw err; - } - - let baseURLObj; - try { - baseURLObj = new URL(baseURL); - } catch(originalError) { - let underlyingError; - if(__DEV__) throw { - originalError, - underlyingError: 'Environment variable REACT_APP_API_BASE_URL is not ' + - `a valid URL. Got "${REACT_APP_API_BASE_URL}".`, - } - throw new Error(prodErrMsg); - } - - return baseURLObj; -} - const axiosDefaultConfig = (() => { - const baseUrlObj = apiBaseUrlFromEnv(); - const baseURLOrigin = baseUrlObj.origin; - const baseURL = baseUrlObj.href; + const apiBaseURLOrigin = API_BASE_URL_OBJ.origin; + const apiBaseURL = API_BASE_URL_OBJ.href; const headers = { ...defaultHeaders.common, Accept: 'application/json' }; - if(__DEV__ && window.location.origin !== baseURLOrigin) { - headers['Access-Control-Allow-Origin'] = baseURLOrigin; + if(__DEV__ && window.location.origin !== apiBaseURLOrigin) { + headers['Access-Control-Allow-Origin'] = apiBaseURLOrigin; } - return { baseURL, headers, timeout: 60_000 }; + return { baseURL: apiBaseURL, headers, timeout: 60_000 }; })(); const fetchFakeReport = async ({ testReport }, { dispatch, requestId }) => {