Skip to content

Commit

Permalink
cli: Add test that runs the CLI app
Browse files Browse the repository at this point in the history
  • Loading branch information
karlvr committed Apr 19, 2024
1 parent b03806d commit dede8c1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/rare-fans-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@openapi-generator-plus/test-generator": patch
"openapi-generator-plus": patch
---

Add test that runs the CLI app
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"access": "public"
},
"devDependencies": {
"@openapi-generator-plus/test-generator": "1.0.0",
"@types/node-fetch": "^2.6.11"
}
}
24 changes: 24 additions & 0 deletions packages/cli/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import fs from 'fs'
import path from 'path'
import { tmpdir } from 'os'
import { createConfig } from '../config'
import generateCommand from '../generate'

test('no config specified', async() => {
const config = await createConfig({ _: [] }, async() => ({ outputPath: 'test output', inputPath: 'test input', generator: 'test generator' }))
Expand All @@ -20,3 +24,23 @@ test('config with overrides', async() => {
expect(config.outputPath).toEqual('output')
expect(config.generator).toEqual('generator')
})

/* Note that the test generator doesn't actually generate files, but we still get to test the functionality of the CLI app */
describe('generate', () => {
const basePath = path.join(__dirname, 'specs')
const files = fs.readdirSync(basePath)

for (const file of files) {
test(file, async() => {
const outputPath = path.join(tmpdir(), 'openapi-generator-plus', 'cli')
fs.mkdirSync(outputPath, { recursive: true })

await generateCommand([
'-o', path.join(outputPath, file),
'-g', '@openapi-generator-plus/test-generator',
'--clean',
path.join(basePath, file),
])
})
}
})
47 changes: 47 additions & 0 deletions packages/cli/src/__tests__/specs/simple.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
openapi: 3.1.0
info:
title: Example
description: Lorem ipsum
version: '1.0.1'
servers:
- url: http://example.com/api/v1
description: Example server
- url: https://example.com/api/v1
paths: {}
components:
schemas:
Test2Request:
type: object
required:
- first
- second
properties:
first:
type:
- string
- "null"
second:
type: string
enum: [ABC, DEF]
third:
type: string
fourth:
type: number
fifth:
type: boolean
Test2Response:
type: object
properties:
messages:
type: array
items:
type: string
object:
anyOf:
- $ref: "#/components/schemas/Test2Object"
- type: "null"
Test2Object:
type: object
properties:
test2:
type: string
5 changes: 4 additions & 1 deletion packages/test-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ const testGeneratorConstructor: CodegenGeneratorConstructor = (config, generator
// NOOP
},
watchPaths: () => [],
cleanPathPatterns: () => undefined,
cleanPathPatterns: () => {
/* So we trigger the clean function of the cli app */
return ['test*']
},
templateRootContext: () => ({}),
}
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit dede8c1

Please sign in to comment.