Skip to content

Commit 8f18166

Browse files
committed
fixup! fixup! fix: code refactoring
Signed-off-by: Oleksii Orel <[email protected]>
1 parent 11cb5cd commit 8f18166

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type DevfileLike = V230Devfile & {
3030
};
3131
};
3232

33-
const DEVWORKSPACE_METADATA_ANNOTATION = 'dw.metadata.annotations';
33+
export const DEVWORKSPACE_METADATA_ANNOTATION = 'dw.metadata.annotations';
3434

3535
@injectable()
3636
export class Generate {

src/main.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import * as axios from 'axios';
1212
import * as fs from 'fs-extra';
13-
import { Generate } from './generate';
13+
import { Generate, DEVWORKSPACE_METADATA_ANNOTATION } from './generate';
1414
import { DevfileSchemaValidator } from './devfile-schema/devfile-schema-validator';
1515
import * as jsYaml from 'js-yaml';
1616
import { InversifyBinding } from './inversify/inversify-binding';
@@ -21,6 +21,9 @@ import { DevfileContext } from './api/devfile-context';
2121
import { GitUrlResolver } from './resolve/git-url-resolver';
2222
import { ValidatorResult } from 'jsonschema';
2323

24+
export const DEVWORKSPACE_DEVFILE = 'che.eclipse.org/devfile';
25+
export const DEVWORKSPACE_DEVFILE_SOURCE = 'che.eclipse.org/devfile-source';
26+
2427
export class Main {
2528
/**
2629
* Default constructor.
@@ -70,6 +73,18 @@ export class Main {
7073
// load content
7174
const devfileParsed = jsYaml.load(devfileContent);
7275

76+
if (!devfileParsed.attributes) {
77+
devfileParsed.attributes = {};
78+
}
79+
devfileParsed.attributes[DEVWORKSPACE_METADATA_ANNOTATION] = {
80+
[DEVWORKSPACE_DEVFILE]: devfileContent,
81+
[DEVWORKSPACE_DEVFILE_SOURCE]: jsYaml.dump({
82+
factory: {
83+
params: 'url=' + params.devfileUrl,
84+
},
85+
}),
86+
};
87+
7388
// is there projects in the devfile ?
7489
if (devfileParsed && !devfileParsed.projects) {
7590
// no, so add the current project being cloned

tests/main.spec.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
import 'reflect-metadata';
1212

1313
import { InversifyBinding } from '../src/inversify/inversify-binding';
14-
import { Main } from '../src/main';
14+
import { Main, DEVWORKSPACE_DEVFILE, DEVWORKSPACE_DEVFILE_SOURCE } from '../src/main';
1515
import fs from 'fs-extra';
1616
import * as jsYaml from 'js-yaml';
1717
import * as axios from 'axios';
18+
import { DEVWORKSPACE_METADATA_ANNOTATION } from '../src/generate';
1819

1920
describe('Test Main with stubs', () => {
2021
const FAKE_DEVFILE_PATH = '/my-fake-devfile-path';
@@ -191,6 +192,104 @@ describe('Test Main with stubs', () => {
191192

192193
const result = {
193194
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+
},
194293
projects: [
195294
{
196295
name: 'my-repo',

0 commit comments

Comments
 (0)