-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new command template - add new command template to print stack ros template with json/yaml - unit test Refs: #5 --------- Signed-off-by: seven <zilisheng1996@gmail.com>
- Loading branch information
Showing
8 changed files
with
360 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { TemplateFormat } from '../types'; | ||
import yaml from 'yaml'; | ||
import { generateStackTemplate } from '../stack/deploy'; | ||
import { constructActionContext, logger } from '../common'; | ||
import { parseYaml } from '../stack'; | ||
|
||
export const template = ( | ||
stackName: string, | ||
options: { format: TemplateFormat; location: string; stage: string | undefined }, | ||
) => { | ||
const context = constructActionContext({ ...options, stackName }); | ||
const iac = parseYaml(context.iacLocation); | ||
const { template } = generateStackTemplate(stackName, iac, context); | ||
|
||
const output = | ||
options.format === TemplateFormat.JSON | ||
? JSON.stringify(template, null, 2) | ||
: yaml.stringify(template); | ||
|
||
logger.info(`\n${output}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { template } from '../../src/commands/template'; | ||
import { TemplateFormat } from '../../src/types'; | ||
import { jsonTemplate, yamlTemplate } from '../fixtures/templateFixture'; | ||
|
||
const mockedLogger = jest.fn(); | ||
jest.mock('../../src/common/logger', () => ({ | ||
logger: { info: (...args: unknown[]) => mockedLogger(...args), debug: jest.fn() }, | ||
})); | ||
const stackName = 'printTemplateStack'; | ||
const location = 'tests/fixtures/serverless-insight.yml'; | ||
|
||
describe('Unit test for template command', () => { | ||
beforeEach(() => { | ||
mockedLogger.mockRestore(); | ||
}); | ||
it('should print the template in JSON format by default', () => { | ||
const options = { | ||
format: TemplateFormat.JSON, | ||
location, | ||
stage: undefined, | ||
}; | ||
|
||
template(stackName, options); | ||
|
||
expect(mockedLogger).toHaveBeenCalledWith(`\n${JSON.stringify(jsonTemplate, null, 2)}`); | ||
}); | ||
|
||
it('should print the template in YAML format when specified', () => { | ||
const options = { format: TemplateFormat.YAML, location, stage: undefined }; | ||
|
||
template(stackName, options); | ||
|
||
expect(mockedLogger).toHaveBeenCalledWith(yamlTemplate); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.