Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Recorder] Coupling RecorderEnvironmentSetup with the record call #7083

Merged
8 changes: 6 additions & 2 deletions sdk/test-utils/recorder/src/baseRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
parseUrl,
isPlaybackMode,
isRecordMode,
findRecordingsFolderPath
findRecordingsFolderPath,
RecorderEnvironmentSetup
} from "./utils";
import { customConsoleLog } from "./customConsoleLog";

Expand Down Expand Up @@ -44,14 +45,17 @@ export function skipQueryParams(params: string[]): void {
queryParameters = params;
}

export function setEnvironmentOnLoad() {
export function setEnvironmentOnLoad(environmentSetup: RecorderEnvironmentSetup) {
if (!isBrowser() && (isRecordMode() || isPlaybackMode())) {
nock = require("nock");
}

if (isBrowser() && isRecordMode()) {
customConsoleLog();
}
setReplaceableVariables(environmentSetup.replaceableVariables);
setReplacements(environmentSetup.replaceInRecordings);
skipQueryParams(environmentSetup.queryParametersToSkip);
}

export abstract class BaseRecorder {
Expand Down
13 changes: 7 additions & 6 deletions sdk/test-utils/recorder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

export { record, Recorder } from "./recorder";
export { env, delay, isPlaybackMode, isRecordMode, isLiveMode } from "./utils";
export {
setReplaceableVariables,
setReplacements,
setEnvironmentOnLoad,
skipQueryParams
} from "./baseRecorder";
env,
delay,
isPlaybackMode,
isRecordMode,
isLiveMode,
RecorderEnvironmentSetup
} from "./utils";
export { jsonRecordingFilterFunction } from "./basekarma.conf";
15 changes: 12 additions & 3 deletions sdk/test-utils/recorder/src/recorder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import { getUniqueName, isBrowser, isRecordMode, isPlaybackMode } from "./utils";
import {
getUniqueName,
isBrowser,
isRecordMode,
isPlaybackMode,
RecorderEnvironmentSetup
} from "./utils";
import { NiseRecorder, NockRecorder, BaseRecorder, setEnvironmentOnLoad } from "./baseRecorder";

/**
Expand Down Expand Up @@ -52,7 +58,10 @@ export interface Recorder {
* @param {Mocha.Context} [testContext]
* @returns {Recorder}
*/
export function record(testContext: Mocha.Context): Recorder {
export function record(
testContext: Mocha.Context,
environmentSetup: RecorderEnvironmentSetup
): Recorder {
let recorder: BaseRecorder;
let testHierarchy: string;
let testTitle: string;
Expand All @@ -72,7 +81,7 @@ export function record(testContext: Mocha.Context): Recorder {
testTitle = testContext.test!.title;
}

setEnvironmentOnLoad();
setEnvironmentOnLoad(environmentSetup);

if (isBrowser()) {
recorder = new NiseRecorder(testHierarchy, testTitle);
Expand Down
37 changes: 37 additions & 0 deletions sdk/test-utils/recorder/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ export interface TestInfo {
newDate: { [x: string]: string };
}

/**
* Interface to setup environment necessary for the test run.
*
* @export
* @interface RecorderEnvironmentSetup
*/
export interface RecorderEnvironmentSetup {
/**
* Used in record and playback modes
* 1. The key-value pairs will be used as the environment variables in playback mode.
* 2. If the env variables are present in the recordings as plain strings, they will be replaced with the provided values in record mode
*
* @type {{ [ENV_VAR: string]: string }}
* @memberof RecorderEnvironmentSetup
*/
replaceableVariables: { [ENV_VAR: string]: string };
/**
* Used in record mode
* Array of callback functions provided to customize the generated recordings in record mode
*
* Example with one callback function -
* `sig` param of SAS Token is being filtered here from the recordings..
* [ (recording: string): string => recording.replace(new RegExp(env.ACCOUNT_SAS.match("(.*)&sig=(.*)")[2], "g"), "aaaaa") ]
*
* @memberof RecorderEnvironmentSetup
*/
replaceInRecordings: Array<(recording: string) => string>;
/**
* Used in record and playback modes
* Array of query parameters provided will be filtered from the requests
*
* @type {Array<string>}
* @memberof RecorderEnvironmentSetup
*/
queryParametersToSkip: Array<string>;
}

export const env = isBrowser() ? (window as any).__env__ : process.env;

export function isRecordMode() {
Expand Down