feat: added support for passing env.json file to test cmd (#2373)

This commit is contained in:
Deepanshu Dhruw
2022-06-15 23:53:24 +05:30
committed by GitHub
parent 2d0bd48e00
commit 0244b941b3
20 changed files with 309 additions and 96 deletions

View File

@@ -9,8 +9,12 @@ import {
import * as A from "fp-ts/Array";
import * as S from "fp-ts/string";
import * as TE from "fp-ts/TaskEither";
import { error, HoppCLIError, HoppErrnoException } from "../types/errors";
import * as E from "fp-ts/Either";
import curryRight from "lodash/curryRight";
import { CommanderError } from "commander";
import { error, HoppCLIError, HoppErrnoException } from "../types/errors";
import { HoppCollectionFileExt } from "../types/collections";
import { HoppEnvFileExt } from "../types/commands";
/**
* Determines whether an object has a property with given name.
@@ -68,42 +72,56 @@ export const isRESTCollection = (
};
/**
* Checks if the given file path exists and is of JSON type.
* Checks if the file path matches the requried file type with of required extension.
* @param path The input file path to check.
* @param extension The required extension for input file path.
* @returns Absolute path for valid file extension OR HoppCLIError in case of error.
*/
export const checkFileExt = curryRight(
(
path: unknown,
extension: HoppCollectionFileExt | HoppEnvFileExt
): E.Either<HoppCLIError, string> =>
pipe(
path,
E.fromPredicate(S.isString, (_) => error({ code: "NO_FILE_PATH" })),
E.chainW(
E.fromPredicate(S.endsWith(`.${extension}`), (_) =>
error({ code: "INVALID_FILE_TYPE", data: extension })
)
)
)
);
/**
* Checks if the given file path exists and is of given type.
* @param path The input file path to check.
* @returns Absolute path for valid file path OR HoppCLIError in case of error.
*/
export const checkFilePath = (
path: string
): TE.TaskEither<HoppCLIError, string> =>
export const checkFile = (path: unknown): TE.TaskEither<HoppCLIError, string> =>
pipe(
path,
/**
* Check the path type and returns string if passes else HoppCLIError.
*/
// Checking if path is string.
TE.fromPredicate(S.isString, () => error({ code: "NO_FILE_PATH" })),
/**
* After checking file path, we map file path to absolute path and check
* if file is of given extension type.
*/
TE.map(join),
TE.chainEitherK(checkFileExt("json")),
/**
* Trying to access given file path.
* If successfully accessed, we return the path from predicate step.
* Else return HoppCLIError with code FILE_NOT_FOUND.
*/
TE.chainFirstW(
TE.chainFirstW((checkedPath) =>
TE.tryCatchK(
() => pipe(path, join, fs.access),
() => error({ code: "FILE_NOT_FOUND", path: path })
)
),
/**
* On successfully accessing given file path, we map file path to
* absolute path and return abs file path if file is JSON type.
*/
TE.map(join),
TE.chainW(
TE.fromPredicate(S.endsWith(".json"), (absPath) =>
error({ code: "FILE_NOT_JSON", path: absPath })
)
() => fs.access(checkedPath),
() => error({ code: "FILE_NOT_FOUND", path: checkedPath })
)()
)
);

View File

@@ -27,21 +27,24 @@ import {
import { getTestMetrics } from "./test";
import { DEFAULT_DURATION_PRECISION } from "./constants";
import { getPreRequestMetrics } from "./pre-request";
import { CollectionRunnerParam } from "../types/collections";
const { WARN, FAIL } = exceptionColors;
/**
* Processes each requests within collections to prints details of subsequent requests,
* tests and to display complete errors-report, failed-tests-report and test-metrics.
* @param collections Array of hopp-collection with hopp-requests to be processed.
* @param param Data of hopp-collection with hopp-requests, envs to be processed.
* @returns List of report for each processed request.
*/
export const collectionsRunner =
(collections: HoppCollection<HoppRESTRequest>[]): T.Task<RequestReport[]> =>
(param: CollectionRunnerParam): T.Task<RequestReport[]> =>
async () => {
const envs: HoppEnvs = { global: [], selected: [] };
const envs: HoppEnvs = param.envs;
const requestsReport: RequestReport[] = [];
const collectionStack: CollectionStack[] = getCollectionStack(collections);
const collectionStack: CollectionStack[] = getCollectionStack(
param.collections
);
while (collectionStack.length) {
// Pop out top-most collection from stack to be processed.

View File

@@ -6,7 +6,7 @@ import * as J from "fp-ts/Json";
import { pipe } from "fp-ts/function";
import { FormDataEntry } from "../types/request";
import { error, HoppCLIError } from "../types/errors";
import { isRESTCollection, isHoppErrnoException } from "./checks";
import { isRESTCollection, isHoppErrnoException, checkFile } from "./checks";
import { HoppCollection, HoppRESTRequest } from "@hoppscotch/data";
/**
@@ -49,10 +49,17 @@ export const parseCollectionData = (
path: string
): TE.TaskEither<HoppCLIError, HoppCollection<HoppRESTRequest>[]> =>
pipe(
TE.of(path),
// Checking if given file path exists or not.
TE.chain(checkFile),
// Trying to read give collection json path.
TE.tryCatch(
() => pipe(path, fs.readFile),
(reason) => error({ code: "UNKNOWN_ERROR", data: E.toError(reason) })
TE.chainW((checkedPath) =>
TE.tryCatch(
() => fs.readFile(checkedPath),
(reason) => error({ code: "UNKNOWN_ERROR", data: E.toError(reason) })
)
),
// Checking if parsed file data is array.