|
11 | 11 | import 'reflect-metadata'; |
12 | 12 |
|
13 | 13 | import { InversifyBinding } from '../src/inversify/inversify-binding'; |
14 | | -import { Main } from '../src/main'; |
| 14 | +import { Main, DEVWORKSPACE_DEVFILE, DEVWORKSPACE_DEVFILE_SOURCE } from '../src/main'; |
15 | 15 | import fs from 'fs-extra'; |
16 | 16 | import * as jsYaml from 'js-yaml'; |
17 | 17 | import * as axios from 'axios'; |
| 18 | +import { DEVWORKSPACE_METADATA_ANNOTATION } from '../src/generate'; |
18 | 19 |
|
19 | 20 | describe('Test Main with stubs', () => { |
20 | 21 | const FAKE_DEVFILE_PATH = '/my-fake-devfile-path'; |
@@ -191,6 +192,104 @@ describe('Test Main with stubs', () => { |
191 | 192 |
|
192 | 193 | const result = { |
193 | 194 | schemaVersion: '2.1.0', |
| 195 | + attributes: { |
| 196 | + [DEVWORKSPACE_METADATA_ANNOTATION]: { |
| 197 | + [DEVWORKSPACE_DEVFILE]: 'schemaVersion: 2.1.0', |
| 198 | + [DEVWORKSPACE_DEVFILE_SOURCE]: jsYaml.dump({ |
| 199 | + factory: { |
| 200 | + params: `url=${FAKE_DEVFILE_URL}`, |
| 201 | + }, |
| 202 | + }), |
| 203 | + }, |
| 204 | + }, |
| 205 | + projects: [ |
| 206 | + { |
| 207 | + name: 'my-repo', |
| 208 | + git: { |
| 209 | + remotes: { |
| 210 | + origin: 'http://foo.bar', |
| 211 | + }, |
| 212 | + checkoutFrom: { |
| 213 | + revision: 'my-branch', |
| 214 | + }, |
| 215 | + }, |
| 216 | + }, |
| 217 | + ], |
| 218 | + }; |
| 219 | + expect(generateMethod).toBeCalledWith(jsYaml.dump(result), "''\n", FAKE_OUTPUT_FILE, 'true', 'my-image'); |
| 220 | + }); |
| 221 | + |
| 222 | + test('success with custom devfile Url (devfile includes attributes)', async () => { |
| 223 | + const main = new Main(); |
| 224 | + initArgs(undefined, FAKE_DEVFILE_URL, undefined, FAKE_EDITOR_URL, FAKE_OUTPUT_FILE, 'true', 'my-image'); |
| 225 | + process.argv.push('--project.foo=bar'); |
| 226 | + containerGetMethod.mockReset(); |
| 227 | + const githubResolverResolveMethod = jest.fn(); |
| 228 | + const githubResolverMock = { |
| 229 | + resolve: githubResolverResolveMethod as any, |
| 230 | + }; |
| 231 | + |
| 232 | + const getContentUrlMethod = jest.fn(); |
| 233 | + const getCloneUrlMethod = jest.fn(); |
| 234 | + const getBranchNameMethod = jest.fn(); |
| 235 | + const getRepoNameMethod = jest.fn(); |
| 236 | + |
| 237 | + const githubUrlMock = { |
| 238 | + getContentUrl: githubResolverResolveMethod as any, |
| 239 | + getCloneUrl: getCloneUrlMethod as any, |
| 240 | + getBranchName: getBranchNameMethod as any, |
| 241 | + getRepoName: getRepoNameMethod as any, |
| 242 | + }; |
| 243 | + getContentUrlMethod.mockReturnValue('http://foo.bar'); |
| 244 | + getCloneUrlMethod.mockReturnValue('http://foo.bar'); |
| 245 | + getBranchNameMethod.mockReturnValue('my-branch'); |
| 246 | + getRepoNameMethod.mockReturnValue('my-repo'); |
| 247 | + githubResolverResolveMethod.mockReturnValue(githubUrlMock); |
| 248 | + containerGetMethod.mockReturnValueOnce(githubResolverMock); |
| 249 | + |
| 250 | + const urlFetcherFetchTextMethod = jest.fn(); |
| 251 | + const urlFetcherMock = { |
| 252 | + fetchText: urlFetcherFetchTextMethod as any, |
| 253 | + }; |
| 254 | + urlFetcherFetchTextMethod.mockReturnValueOnce('schemaVersion: 2.1.0\nattributes:\n foo: bar'); |
| 255 | + containerGetMethod.mockReturnValueOnce(urlFetcherMock); |
| 256 | + |
| 257 | + const validateDevfileMethod = jest.fn(); |
| 258 | + const devfileSchemaValidatorMock = { |
| 259 | + validateDevfile: validateDevfileMethod as any, |
| 260 | + }; |
| 261 | + validateDevfileMethod.mockReturnValueOnce({ valid: true }); |
| 262 | + containerGetMethod.mockReturnValueOnce(devfileSchemaValidatorMock); |
| 263 | + |
| 264 | + const loadEditorMethod = jest.fn(); |
| 265 | + const editorResolverMock = { |
| 266 | + loadEditor: loadEditorMethod as any, |
| 267 | + }; |
| 268 | + loadEditorMethod.mockReturnValue(''); |
| 269 | + containerGetMethod.mockReturnValueOnce(editorResolverMock); |
| 270 | + |
| 271 | + // last one is generate mock |
| 272 | + containerGetMethod.mockReturnValueOnce(generateMock); |
| 273 | + const returnCode = await main.start(); |
| 274 | + expect(mockedConsoleError).toBeCalledTimes(0); |
| 275 | + expect(loadEditorMethod).toBeCalled(); |
| 276 | + expect(urlFetcherFetchTextMethod).toBeCalled(); |
| 277 | + |
| 278 | + expect(returnCode).toBeTruthy(); |
| 279 | + |
| 280 | + const result = { |
| 281 | + schemaVersion: '2.1.0', |
| 282 | + attributes: { |
| 283 | + foo: 'bar', |
| 284 | + [DEVWORKSPACE_METADATA_ANNOTATION]: { |
| 285 | + [DEVWORKSPACE_DEVFILE]: 'schemaVersion: 2.1.0\nattributes:\n foo: bar', |
| 286 | + [DEVWORKSPACE_DEVFILE_SOURCE]: jsYaml.dump({ |
| 287 | + factory: { |
| 288 | + params: `url=${FAKE_DEVFILE_URL}`, |
| 289 | + }, |
| 290 | + }), |
| 291 | + }, |
| 292 | + }, |
194 | 293 | projects: [ |
195 | 294 | { |
196 | 295 | name: 'my-repo', |
|
0 commit comments