|
| 1 | +import { join } from 'path'; |
| 2 | +import { removeSync, outputFileSync } from 'fs-extra'; |
| 3 | +import { parse } from '../../src/parse'; |
| 4 | + |
| 5 | +describe('Parse command test', () => { |
| 6 | + // inject environment variables |
| 7 | + process.env.REGION = 'ap-guangzhou'; |
| 8 | + |
| 9 | + const configFileContent = `org: orgDemo |
| 10 | +app: appDemo |
| 11 | +stage: dev |
| 12 | +component: http |
| 13 | +name: httpDemo |
| 14 | +
|
| 15 | +inputs: |
| 16 | + src: |
| 17 | + src: ./ |
| 18 | + exclude: |
| 19 | + - .env |
| 20 | + region: \${env:REGION} |
| 21 | + faas: |
| 22 | + framework: express |
| 23 | + runtime: Nodejs10.15 |
| 24 | + apigw: |
| 25 | + protocols: |
| 26 | + - http |
| 27 | + - https |
| 28 | +`; |
| 29 | + const demoPath = join(__dirname, 'demo'); |
| 30 | + const outputPath = join(__dirname, 'output'); |
| 31 | + const layerPath = join(__dirname, 'layer'); |
| 32 | + const fileName = 'serverless.yml'; |
| 33 | + |
| 34 | + beforeAll(() => { |
| 35 | + const configFile = join(demoPath, fileName); |
| 36 | + outputFileSync(configFile, configFileContent); |
| 37 | + }); |
| 38 | + |
| 39 | + afterAll(() => { |
| 40 | + removeSync(demoPath); |
| 41 | + removeSync(outputPath); |
| 42 | + removeSync(layerPath); |
| 43 | + }); |
| 44 | + |
| 45 | + const configFile = join(demoPath, fileName); |
| 46 | + |
| 47 | + test(`should success parse ${fileName} file`, async () => { |
| 48 | + const res = parse({ |
| 49 | + rootDir: __dirname, |
| 50 | + input: configFile, |
| 51 | + }); |
| 52 | + expect(res).toEqual({ |
| 53 | + org: 'orgDemo', |
| 54 | + app: 'appDemo', |
| 55 | + stage: 'dev', |
| 56 | + component: 'http', |
| 57 | + name: 'httpDemo', |
| 58 | + inputs: { |
| 59 | + src: { |
| 60 | + src: './', |
| 61 | + exclude: ['.env'], |
| 62 | + }, |
| 63 | + region: 'ap-guangzhou', |
| 64 | + faas: { |
| 65 | + framework: 'express', |
| 66 | + runtime: 'Nodejs10.15', |
| 67 | + }, |
| 68 | + apigw: { |
| 69 | + protocols: ['http', 'https'], |
| 70 | + }, |
| 71 | + }, |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + test(`should success parse ${fileName} file using slsOptions`, async () => { |
| 76 | + const res = parse({ |
| 77 | + rootDir: __dirname, |
| 78 | + input: configFile, |
| 79 | + slsOptionsJson: '{"inputs":{"src":"./src"}}', |
| 80 | + }); |
| 81 | + expect(res).toEqual({ |
| 82 | + org: 'orgDemo', |
| 83 | + app: 'appDemo', |
| 84 | + stage: 'dev', |
| 85 | + component: 'http', |
| 86 | + name: 'httpDemo', |
| 87 | + inputs: { |
| 88 | + src: './src', |
| 89 | + region: 'ap-guangzhou', |
| 90 | + faas: { |
| 91 | + framework: 'express', |
| 92 | + runtime: 'Nodejs10.15', |
| 93 | + }, |
| 94 | + apigw: { |
| 95 | + protocols: ['http', 'https'], |
| 96 | + }, |
| 97 | + }, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + test(`should success parse ${fileName} file using slsOptions with new property`, async () => { |
| 102 | + const res = parse({ |
| 103 | + rootDir: __dirname, |
| 104 | + input: configFile, |
| 105 | + slsOptionsJson: '{"inputs":{"src":"./","test":1}}', |
| 106 | + }); |
| 107 | + expect(res).toEqual({ |
| 108 | + org: 'orgDemo', |
| 109 | + app: 'appDemo', |
| 110 | + stage: 'dev', |
| 111 | + component: 'http', |
| 112 | + name: 'httpDemo', |
| 113 | + inputs: { |
| 114 | + src: './', |
| 115 | + test: 1, |
| 116 | + region: 'ap-guangzhou', |
| 117 | + faas: { |
| 118 | + framework: 'express', |
| 119 | + runtime: 'Nodejs10.15', |
| 120 | + }, |
| 121 | + apigw: { |
| 122 | + protocols: ['http', 'https'], |
| 123 | + }, |
| 124 | + }, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + test(`should success parse ${fileName} file override by slsOptions `, async () => { |
| 129 | + const res = parse({ |
| 130 | + rootDir: __dirname, |
| 131 | + input: configFile, |
| 132 | + override: true, |
| 133 | + slsOptionsJson: |
| 134 | + '{"org": "orgDemo","app": "appDemo","stage": "dev","component": "http","name": "httpDemoTest","inputs":{"src":"./","region":"ap-guangzhou"}}', |
| 135 | + }); |
| 136 | + expect(res).toEqual({ |
| 137 | + org: 'orgDemo', |
| 138 | + app: 'appDemo', |
| 139 | + stage: 'dev', |
| 140 | + component: 'http', |
| 141 | + name: 'httpDemoTest', |
| 142 | + inputs: { |
| 143 | + src: './', |
| 144 | + region: 'ap-guangzhou', |
| 145 | + }, |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + test(`should success parse ${fileName} file using layerOptions of http component`, async () => { |
| 150 | + const res = parse({ |
| 151 | + rootDir: __dirname, |
| 152 | + input: configFile, |
| 153 | + layerOptionsJson: |
| 154 | + '{"org":"orgDemo","app":"appDemo","stage":"dev","runtime":"Nodejs10.15","region":"ap-guangzhou"}', |
| 155 | + }); |
| 156 | + expect(res).toEqual({ |
| 157 | + org: 'orgDemo', |
| 158 | + app: 'appDemo', |
| 159 | + stage: 'dev', |
| 160 | + component: 'http', |
| 161 | + name: 'httpDemo', |
| 162 | + inputs: { |
| 163 | + region: 'ap-guangzhou', |
| 164 | + src: { |
| 165 | + src: './', |
| 166 | + exclude: ['.env'], |
| 167 | + }, |
| 168 | + faas: { |
| 169 | + framework: 'express', |
| 170 | + runtime: 'Nodejs10.15', |
| 171 | + layers: [ |
| 172 | + { |
| 173 | + name: '${output:${stage}:${app}:appDemo-layer.name}', |
| 174 | + version: '${output:${stage}:${app}:appDemo-layer.version}', |
| 175 | + }, |
| 176 | + ], |
| 177 | + }, |
| 178 | + apigw: { |
| 179 | + protocols: ['http', 'https'], |
| 180 | + }, |
| 181 | + }, |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments