From a1cb99bb030b1ce63cc364d23780c4298cfd2be8 Mon Sep 17 00:00:00 2001 From: Matteo Boschi Date: Mon, 17 Jun 2019 17:58:12 +0200 Subject: [PATCH] fix tests --- src/utils/azure.ts | 16 +++++++++---- test/commands/users/list.test.ts | 39 +++++++++++++++----------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/utils/azure.ts b/src/utils/azure.ts index 543efcf..f28929f 100644 --- a/src/utils/azure.ts +++ b/src/utils/azure.ts @@ -47,15 +47,23 @@ export const getStorageConnection = async (name: string) => * hasCosmosConnection checks if the host has az cli installed and resouceGroup and name * are valid inputs for cosmosdb */ -export const hasCosmosConnection = (resourceGroup: string, name: string) => { +export const hasCosmosConnection = async ( + resourceGroup: string, + name: string +) => { try { - execa.sync( + const successExitCode = "SUCCESS"; + const documentEndpoint = await execa( `az cosmosdb show -g ${resourceGroup} -n ${name} --query documentEndpoint -o tsv` ); - execa.sync( + const primaryReadonlyMasterKey = execa.sync( `az cosmosdb list-keys -g ${resourceGroup} -n ${name} --query primaryReadonlyMasterKey -o tsv` ); - return true; + + return ( + documentEndpoint.exitCodeName === successExitCode && + primaryReadonlyMasterKey.exitCodeName === successExitCode + ); } catch (e) { return false; } diff --git a/test/commands/users/list.test.ts b/test/commands/users/list.test.ts index be4fd6e..ee60444 100644 --- a/test/commands/users/list.test.ts +++ b/test/commands/users/list.test.ts @@ -2,28 +2,25 @@ import { expect, test } from "@oclif/test"; import { hasCosmosConnection } from "../../../src/utils/azure"; describe("list all profiles", () => { - const isCosmosConnectionAvailable = hasCosmosConnection( - "agid-rg-test", - "agid-cosmosdb-test" - ); /** * if host has the az client installed and valid cosmos credentials we test command first output line matches - * the header row (fiscalCode column name). Otherwise the test expects for an error with 2 as exit status + * the header row (fiscalCode column name). Otherwise the test will be skipped */ - if (isCosmosConnectionAvailable) { - test - .stdout() - .command(["profiles:list"]) - .it("runs profiles command to list all users", ctx => { - expect(ctx.stdout).match(/^fiscalCode/); - }); - } else { - test - .stdout() - .command(["profiles:list"]) - .exit(2) - .it( - "check the command goes in error (no cosmos credential available or az client not installed)" - ); - } + + before(async function(): Promise { + const isCosmosConnectionAvailable = await hasCosmosConnection( + "agid-rg-test", + "agid-cosmosdb-test" + ); + if (!isCosmosConnectionAvailable) { + // tslint:disable-next-line: no-invalid-this + this.skip(); + } + }); + test + .stdout() + .command(["profiles:list"]) + .it("runs profiles command to list all users", ctx => { + expect(ctx.stdout).match(/^fiscalCode/); + }); });