From 45a696feb848d5fb5c23023200d3cf14f2c0f043 Mon Sep 17 00:00:00 2001 From: Edouard Date: Tue, 7 Mar 2023 14:14:20 +0100 Subject: [PATCH] test(nx-heroku): add unit tests for stdout parsers --- README.md | 4 +- .../executors/common/heroku/config-vars.ts | 1 - .../src/executors/common/heroku/webhooks.ts | 2 - .../src/executors/common/utils.spec.ts | 98 +++++++++++++++++++ .../nx-heroku/src/executors/common/utils.ts | 5 +- 5 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 packages/nx-heroku/src/executors/common/utils.spec.ts diff --git a/README.md b/README.md index ddce15f..d887a67 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ sequenceDiagram participant Nx as Nx Plugin participant CLI as Heroku participant Git as Git - participant App as Deployed app + participant App as Application note over Nx,Nx: Set internal variables and default options Nx->>Nx: Setup @@ -145,7 +145,7 @@ sequenceDiagram opt resetRepo is set to true in options Nx->>CLI: Reset the app repository end - Nx->>Git: Deploy app + Nx->>Git: Build and release app opt watchDelay is set to > 0 Nx->>Git: Wait for the app to be deployed until the timeout is reached end diff --git a/packages/nx-heroku/src/executors/common/heroku/config-vars.ts b/packages/nx-heroku/src/executors/common/heroku/config-vars.ts index 3fc417c..2360d7f 100644 --- a/packages/nx-heroku/src/executors/common/heroku/config-vars.ts +++ b/packages/nx-heroku/src/executors/common/heroku/config-vars.ts @@ -21,7 +21,6 @@ export async function getConfigVars(options: { if (rawAppEnv.includes(`Invalid credentials provided`)) { throw new Error('Invalid credentials provided'); } - rawAppEnv.shift(); return rawAppEnv.reduce((acc, line) => { const parts = line.split(':'); const key = parts.shift().trim(); diff --git a/packages/nx-heroku/src/executors/common/heroku/webhooks.ts b/packages/nx-heroku/src/executors/common/heroku/webhooks.ts index ca10bc9..e176958 100644 --- a/packages/nx-heroku/src/executors/common/heroku/webhooks.ts +++ b/packages/nx-heroku/src/executors/common/heroku/webhooks.ts @@ -15,8 +15,6 @@ export async function getWebhooks( return []; } const res = parseTable(stdout); - // remove header from webhooks response - res.shift(); return res.map((webhook) => { const [id, url, include, level] = webhook.split(' '); return { id, url, include, level }; diff --git a/packages/nx-heroku/src/executors/common/utils.spec.ts b/packages/nx-heroku/src/executors/common/utils.spec.ts new file mode 100644 index 0000000..0d6d6f4 --- /dev/null +++ b/packages/nx-heroku/src/executors/common/utils.spec.ts @@ -0,0 +1,98 @@ +import { + parseJsonString, + parseTable, + removeConsoleOutputColors, +} from './utils'; + +// script -F test heroku config --app +const drainRawOutput = ` +[ + { + "addon": null, + "created_at": "2022-12-05T16:31:10Z", + "id": "e9c0bbad-1d2a-464a-a3b1-4baa7c402ff3", + "token": "d.9f414bb2-f095-435b-a51d-3d81a18da875", + "updated_at": "2022-12-05T16:31:10Z", + "url": "https://logger.doamin.com/api/logger" + } +] +[?25h +`; + +// script -F test heroku config --app +const configVarsRawOutput = ` +=== s1-auth-service-staging Config Vars +CLUSTER_ENABLED: true +DISK_STORAGE_TRESHOLD: 161061273600 +EVENT_LOOP_DELAY_THRESHOLD: 100 +HOSTNAME: 0.0.0.0 +LOG_CONCURRENCY: true +MAX_PAYLOAD_SIZE: 1 +MEMORY_RSS_TRESHOLD: +NODE_ENV: staging +NPM_TOKEN: npm_token +PROCFILE: apps/x-service/Procfile +PROJECT_ENV: staging +PROJECT_NAME: x-service +PROXY_SERVER_URL: https://x.service.dev +SERVER_URL: http://localhost:3000 +TAG: v1.9.4 +USE_YARN_CACHE: undefined +YARN2_SKIP_PRUNING: true +`; + +const configVarsClean = `=== s1-auth-service-staging Config Vars +CLUSTER_ENABLED: true +DISK_STORAGE_TRESHOLD: 161061273600 +EVENT_LOOP_DELAY_THRESHOLD: 100 +HOSTNAME: 0.0.0.0 +LOG_CONCURRENCY: true +MAX_PAYLOAD_SIZE: 1 +MEMORY_RSS_TRESHOLD: +NODE_ENV: staging +NPM_TOKEN: npm_token +PROCFILE: apps/x-service/Procfile +PROJECT_ENV: staging +PROJECT_NAME: x-service +PROXY_SERVER_URL: https://x.service.dev +SERVER_URL: http://localhost:3000 +TAG: v1.9.4 +USE_YARN_CACHE: undefined +YARN2_SKIP_PRUNING: true`; + +describe('Utils', () => { + it.todo('expandOptions - should expand options'); + + it('removeConsoleOutputColors - should remove ANSI colors code', () => { + const result = removeConsoleOutputColors(configVarsRawOutput); + expect(result).toBe(configVarsClean); + }); + + it('parseJsonString - should parse raw json string', () => { + const result = parseJsonString(drainRawOutput); + expect(result).toBeDefined(); + expect(result).toHaveLength(1); + const [firstItem] = result; + expect(firstItem).toHaveProperty('addon', null); + expect(firstItem).toHaveProperty('created_at', '2022-12-05T16:31:10Z'); + expect(firstItem).toHaveProperty( + 'id', + 'e9c0bbad-1d2a-464a-a3b1-4baa7c402ff3' + ); + expect(firstItem).toHaveProperty( + 'token', + 'd.9f414bb2-f095-435b-a51d-3d81a18da875' + ); + expect(firstItem).toHaveProperty('updated_at', '2022-12-05T16:31:10Z'); + expect(firstItem).toHaveProperty( + 'url', + 'https://logger.doamin.com/api/logger' + ); + }); + + it('parseTable - should parse raw config vars string', () => { + const result = parseTable(configVarsRawOutput); + expect(result).toBeDefined(); + expect(result).toHaveLength(17); + }); +}); diff --git a/packages/nx-heroku/src/executors/common/utils.ts b/packages/nx-heroku/src/executors/common/utils.ts index 99877d1..0d3a8af 100644 --- a/packages/nx-heroku/src/executors/common/utils.ts +++ b/packages/nx-heroku/src/executors/common/utils.ts @@ -16,7 +16,10 @@ export function parseJsonString(stdout: string) { } export function parseTable(stdout: string) { - return removeConsoleOutputColors(stdout)?.split('\n'); + const lines = removeConsoleOutputColors(stdout)?.split('\n'); + // remove header from response + lines.shift(); + return lines; } export function sleep(ms: number) {