Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn for missing targets #659

Merged
merged 7 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Warn for missing targets (#632 #391)

## v1.30.4 - (2024-10-07)

- Handle Postman API non-200 responses better (#660)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ Options:
--filterFile Path/URL to openapi-format config file (oas-format-filter.json) [string]
--envFile Path to the .env file to inject environment variables [string]
--collectionName Overwrite OpenAPI title to set the Postman collection name [string]
--cliOptionsFile Path/URL to Portman CLI options file [string]
--cliOptionsFile Path/URL to Portman CLI options file [string]
--ignoreCircularRefs Ignore circular references in OpenAPI spec (default: false) [boolean]
--logAssignVariables Toggle logging of assigned variables (default: true) [boolean]
--warn/--no-warn Toggle warnings for missing openApiOperationIds (default: true) [boolean]
--init Configure Portman CLI options in an interactive manner [string]
--extraUnknownFormats Add extra unknown formats to json schema tests [array]
```
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@apidevtools/swagger-parser": "^10.1.0",
"@faker-js/faker": "5.5.3",
"ajv": "^8.12.0",
"axios": "^1.6.5",
"axios": "^1.7.7",
"chalk": "^4.1.2",
"dot-object": "^2.1.5",
"dotenv": "^10.0.0",
Expand Down
36 changes: 35 additions & 1 deletion src/Portman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { clearTmpDirectory, getConfig } from './lib'
import { OpenApiFormatter, OpenApiParser } from './oas'
import { PostmanParser } from './postman'
import { IOpenApiToPostmanConfig, OpenApiToPostmanService, PostmanSyncService } from './services'
import { PortmanConfig, PortmanTestTypes } from './types'
import { PortmanConfig, PortmanTestTypes, Track } from './types'
import { PortmanOptions } from './types/PortmanOptions'
import { validate } from './utils/PortmanConfig.validator'
import { PortmanError } from './utils/PortmanError'
Expand Down Expand Up @@ -189,6 +189,7 @@ export class Portman {

async after(): Promise<void> {
const { consoleLine, collectionFile } = this

await clearTmpDirectory()
console.log(chalk.green(consoleLine))

Expand All @@ -199,6 +200,9 @@ export class Portman {
)

console.log(chalk.green(consoleLine))

// Display warning
this.displayMissingTargets(this.testSuite.track)
}

async parseOpenApiSpec(): Promise<void> {
Expand Down Expand Up @@ -666,4 +670,34 @@ export class Portman {
process.exit(1)
}
}

displayMissingTargets = (track: Track): void => {
const {
consoleLine,
options: { warn }
} = this

if (warn === false) {
return
}

// Deduplicate missing targets
const uniqueOperationIds = Array.from(new Set(track.openApiOperationIds))
const uniqueOperations = Array.from(new Set(track.openApiOperations))

if (uniqueOperationIds.length > 0 || uniqueOperations.length === 0) {
console.log(
chalk.yellow(`WARNING: The following targets are missing from the OpenAPI specification.`)
)
if (uniqueOperationIds.length > 0) {
const idsList = uniqueOperationIds.join(', ')
console.log(chalk.yellow(`operationId:\t\t${idsList}`))
}
if (uniqueOperations.length > 0) {
const opsList = uniqueOperations.join(', ')
console.log(chalk.yellow(`openApiOperation:\t${opsList}`))
}
console.log(chalk.yellow(consoleLine))
}
}
}
5 changes: 4 additions & 1 deletion src/application/IntegrationTestWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export class IntegrationTestWriter {

const pmOperation = testSuite.postmanParser.getOperationById(openApiOperationId)

if (!pmOperation) return
if (!pmOperation) {
this.testSuite.track.openApiOperationIds.push(openApiOperationId)
return
}

const folderId = variationWriter.variationFolder.id
// const folderName = pmOperation.getParentFolderName()
Expand Down
18 changes: 18 additions & 0 deletions src/application/TestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ResponseTime,
StatusCode,
TestSuiteOptions,
Track,
VariationTestConfig
} from '../types'
import { inRange } from '../utils'
Expand All @@ -61,6 +62,9 @@ export class TestSuite {

requestTestTypes: PortmanReqTestType[]

// Tracker
public track = { openApiOperationIds: [], openApiOperations: [] } as Track

constructor(testSuiteOptions: TestSuiteOptions) {
const { oasParser, postmanParser, config, options } = testSuiteOptions

Expand Down Expand Up @@ -168,10 +172,24 @@ export class TestSuite {

if (openApiOperation) {
pmOperations = this.postmanParser.getOperationsByPath(openApiOperation)
// Track missing operations
if (pmOperations.length === 0) {
this.track.openApiOperations.push(openApiOperation)
}
} else if (openApiOperationId) {
pmOperations = this.postmanParser.getOperationsByIds([openApiOperationId])

// Track missing operations
if (pmOperations.length === 0) {
this.track.openApiOperationIds.push(openApiOperationId)
}
} else if (openApiOperationIds) {
pmOperations = this.postmanParser.getOperationsByIds(openApiOperationIds)

// Track missing operations
if (pmOperations.length === 0) {
this.track.openApiOperationIds.push(...openApiOperationIds)
}
}

if (settings?.excludeForOperations) {
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ require('dotenv').config()
describe: 'Toggle logging of assigned variables',
type: 'boolean'
})
.option('warn', {
describe: 'Toggle warnings for missing openApiOperationIds',
type: 'boolean'
})
.option('init', {
describe: 'Initialize Portman and generate a Portman CLI configuration file',
type: 'boolean'
Expand Down Expand Up @@ -232,6 +236,7 @@ require('dotenv').config()
const oaOutput = options.oaOutput || ''
const collectionName = options.collectionName || ''
const logAssignVariables = options?.logAssignVariables
const warn = options?.warn || true
const extraUnknownFormats = options?.extraUnknownFormats || []
const syncPostmanCollectionIds = options?.syncPostmanCollectionIds || false

Expand All @@ -254,6 +259,7 @@ require('dotenv').config()
oaOutput,
collectionName,
logAssignVariables,
warn,
extraUnknownFormats,
syncPostmanCollectionIds
})
Expand Down
1 change: 1 addition & 0 deletions src/types/PortmanOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface PortmanOptions {
oaUrl?: string
init?: boolean
logAssignVariables?: boolean
warn?: boolean
extraUnknownFormats?: string[]
syncPostmanCollectionIds?: boolean
}
4 changes: 4 additions & 0 deletions src/types/PortmanTestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export type PortmanReqTestType = {
export interface PortmanTestSuite {
pmReqTestType?: PortmanReqTestType
}
export interface Track {
openApiOperationIds: string[]
openApiOperations: string[]
}
Loading