Skip to content

Commit

Permalink
fill in test api function in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay288 committed Sep 9, 2022
1 parent f2fe177 commit d86648d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"nodemon": "^2.0.19",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.0",
"valid-url": "^1.0.9"
"valid-url": "^1.0.9",
"chalk": "^4.1.2"
}
}
47 changes: 43 additions & 4 deletions cli/src/testAPI.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import axios from "axios"
import chalk from "chalk"
import { getConfig } from "./utils"

import { Test, runTest } from "@metlo/testing"
import { Test, runTest, TestResult, Result } from "@metlo/testing"

interface testAPIArgs {
host: string
target: string
}

const getTests = async (args: testAPIArgs): Promise<Test[]> => {
interface APIEndpoint {
method: string
host: string
path: string
}

interface TestResponse extends Test {
apiEndpoint: APIEndpoint
}

const getTests = async (args: testAPIArgs): Promise<TestResponse[]> => {
const config = getConfig()
const testResponse = await axios.get<Test[]>(
const testResponse = await axios.get<TestResponse[]>(
`${config.metloHost}/api/v1/test/list?hostname=${args.host}`,
)
return testResponse.data
Expand All @@ -20,7 +31,35 @@ const testAPI = async (args: testAPIArgs) => {
try {
const tests = await getTests(args)
const outputs = await Promise.all(tests.map(runTest))
console.log(outputs)
const testWithOutputs: [TestResponse, Result[]][] = outputs.map((e, i) => [
tests[i],
e,
])
let headerToFailures = new Map<string, TestResult[]>()
testWithOutputs.forEach(([test, results]) => {
const name = `${chalk.red(test.name)}\n${test.apiEndpoint.method} ${test.apiEndpoint.path}`
const failureResults = results
.flatMap(e => e.testResults)
.filter(e => !e.success)
if (failureResults.length > 0) {
let currentFailures = headerToFailures.get(name) || []
currentFailures.push(...failureResults)
headerToFailures.set(name, currentFailures)
}
})
let output = ""
headerToFailures.forEach((testResults, key) => {
output += `${key}\n\n`
output += testResults.map(e => e.output).join("\n")
})
if (output.length > 0) {
console.log(chalk.red.bold("Some Tests Failed:\n"))
console.log(output)
process.exit(1)
} else {
console.log(chalk.green.bold("All tests succeeded!"))
process.exit(0)
}
} catch (e) {
console.error(e.message)
return
Expand Down
13 changes: 4 additions & 9 deletions cli/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ chalk@^2.0.0:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^4.0.0:
chalk@^4.0.0, chalk@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
Expand Down Expand Up @@ -356,11 +356,6 @@ diff@^4.0.1:
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

dotenv@^16.0.2:
version "16.0.2"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.2.tgz#0b0f8652c016a3858ef795024508cddc4bffc5bf"
integrity sha512-JvpYKUmzQhYoIFgK2MOnF3bciIZoItIIoryihy0rIA+H4Jy0FmgyKYAHCTN98P5ybGSJcIFbh6QKeJdtZd1qhA==

enquirer@^2.3.6:
version "2.3.6"
resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.3.6.tgz#2a7fe5dd634a1e4125a975ec994ff5456dc3734d"
Expand Down Expand Up @@ -730,9 +725,9 @@ tslib@^2.4.0:
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==

typescript@^4.7.4:
version "4.8.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.2.tgz#e3b33d5ccfb5914e4eeab6699cf208adee3fd790"
integrity sha512-C0I1UsrrDHo2fYI5oaCGbSejwX4ch+9Y5jTQELvovfmFkK3HHSZJB8MSJcWLmCUBzQBchCrZ9rMRV6GuNrvGtw==
version "4.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.3.tgz#d59344522c4bc464a65a730ac695007fdb66dd88"
integrity sha512-goMHfm00nWPa8UvR/CPSvykqf6dVV8x/dp0c5mFTMTIu0u0FlGWRioyy7Nn0PGAdHxpJZnuO/ut+PpQ8UiHAig==

undefsafe@^2.0.5:
version "2.0.5"
Expand Down

0 comments on commit d86648d

Please sign in to comment.