Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
W.I.P.
Browse files Browse the repository at this point in the history
  • Loading branch information
pierce-h committed Sep 29, 2020
1 parent 5b4b433 commit 06840e0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
8 changes: 5 additions & 3 deletions src/core/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "./test-app/tests";
import { SdkApp } from "./types";
import { TestResults, useTestResults } from "./test-app/runner/test-results";
import { loadAndValidateConfig } from "./test-app/runner/load-and-validate-config";
import { loadAndValidateConfig, LoadAndValidateConfigError } from "./test-app/runner/load-and-validate-config";
import { logFail, logPass, logStep } from "./utils/log-helpers";
import { logResults } from "./utils/log-helpers";
import { RateShipmentWithAllServices } from './test-app/tests/rate-shipment-with-all-services';
Expand Down Expand Up @@ -45,10 +45,12 @@ export default async function testApp(
staticConfig = await loadAndValidateConfig(pathToApp);
} catch (error) {
switch (error.code) {
case "ERR_CONNECT_CONFIG_SCHEMA":
case LoadAndValidateConfigError.SchemaInvalid:
case LoadAndValidateConfigError.Filesystem:
case LoadAndValidateConfigError.Syntax:
throw error;

default:
// IF the file doesnt exist an error will be thrown and we want to swallow that here
break;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/core/test-app/runner/load-and-validate-config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import Config from "./config";
import { readFile } from "../../utils/read-file";
import validateConfig from "./validate-config";
import { readFile, FileError } from "../../utils/read-file";
import validateConfig, { ValidateConfigError } from "./validate-config";

export const LoadAndValidateConfigError = { ...ValidateConfigError, ...FileError };

export async function loadAndValidateConfig(
pathToApp: string,
): Promise<Config> {
const config = await readFile<Config>(`${pathToApp}/connect.config.js`);

await validateConfig(config);
validateConfig(config);

return config;
}
6 changes: 5 additions & 1 deletion src/core/test-app/runner/validate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,15 @@ const schema = Joi.object().keys({
tests: testsSchema,
});

export enum ValidateConfigError {
SchemaInvalid = "ERR_SCHEMA_INVALID"
}

const validateConfig = (config: Config): Config => {
const { error, value } = schema.validate(config, joiOptions as ValidationOptions);

if (error) {
throw ono(error, { code: "ERR_CONNECT_CONFIG_SCHEMA" });
throw ono(error, { code: ValidateConfigError.SchemaInvalid });
}

return value as Config;
Expand Down
41 changes: 26 additions & 15 deletions src/core/utils/read-file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {
EcmaScriptModule
} from "@shipengine/connect-sdk";
import { error as sdkError, SystemErrorCode } from "@shipengine/connect-sdk/lib/internal";
import ono from '@jsdevtools/ono'
import { promises as fs } from "fs";
import * as jsYaml from "js-yaml";
import * as json5 from "json5";
import * as path from "path";

export enum FileError {
Filesystem = "ERR_FILESYSTEM",
Syntax = "ERR_SYNTAX"
}

/**
* Returns the contents of the specified UTF-8 text file
*/
Expand All @@ -15,12 +20,12 @@ async function readTextFile(absoluteFilePath: string): Promise<string> {
return await fs.readFile(absoluteFilePath, "utf8");
} catch (error) {
const err = error as Error;
throw sdkError(
SystemErrorCode.Filesystem,
`Unable to read ${absoluteFilePath}.`,
throw ono(
err,
{
err,
code: FileError.Filesystem
},
`Unable to read ${absoluteFilePath}.`,
);
}
}
Expand All @@ -37,10 +42,12 @@ async function readYamlFile<T>(absoluteFilePath: string): Promise<T> {
return parsedYaml as T;
} catch (error) {
const err = error as Error;
throw sdkError(
SystemErrorCode.Syntax,
throw ono(
err,
{
code: FileError.Syntax
},
`Unable to parse ${path.basename(absoluteFilePath)}.`,
{ err },
);
}
}
Expand All @@ -55,10 +62,12 @@ async function readJsonFile<T>(absoluteFilePath: string): Promise<T> {
return json5.parse(json) as T;
} catch (error) {
const err = error as Error;
throw sdkError(
SystemErrorCode.Syntax,
throw ono(
err,
{
code: FileError.Syntax
},
`Unable to parse ${path.basename(absoluteFilePath)}.`,
{ err },
);
}
}
Expand All @@ -77,10 +86,12 @@ async function importJavaScriptModule<T>(absoluteFilePath: string): Promise<T> {
return (exports as unknown) as T;
} catch (error) {
const err = error as Error;
throw sdkError(
SystemErrorCode.Filesystem,
throw ono(
err,
{
code: FileError.Filesystem
},
`Unable to import ${path.basename(absoluteFilePath)}.`,
{ err },
);
}
}
Expand All @@ -90,7 +101,7 @@ async function importJavaScriptModule<T>(absoluteFilePath: string): Promise<T> {
*/
export async function readFile<T>(absoluteFilePath: string): Promise<T> {
if (!(path.resolve(absoluteFilePath) === path.normalize(absoluteFilePath))) {
throw sdkError(SystemErrorCode.Filesystem, "Path must be absolute.");
throw ono({ code: FileError.Filesystem }, "Path must be absolute.");
}

switch (path.extname(absoluteFilePath)) {
Expand Down

0 comments on commit 06840e0

Please sign in to comment.