Skip to content

Commit

Permalink
test(nx-heroku): add unit tests for stdout parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Mar 7, 2023
1 parent 80f8ef0 commit 45a696f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions packages/nx-heroku/src/executors/common/heroku/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
98 changes: 98 additions & 0 deletions packages/nx-heroku/src/executors/common/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
parseJsonString,
parseTable,
removeConsoleOutputColors,
} from './utils';

// script -F test heroku config --app <app-name>
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 <app-name>
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);
});
});
5 changes: 4 additions & 1 deletion packages/nx-heroku/src/executors/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 45a696f

Please sign in to comment.