Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 4c9da5a

Browse files
authored
[FIX] issue 999: error in generate.ts is masked and hence command lin… (#312)
* [FIX] issue 999: error in generate.ts is masked and hence command line is returning status 0 * reorder test to run the git tests first
1 parent 57ecd62 commit 4c9da5a

File tree

2 files changed

+289
-264
lines changed

2 files changed

+289
-264
lines changed

src/commands/infra/generate.test.ts

Lines changed: 166 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,147 @@ beforeEach(() => {
5252
jest.restoreAllMocks();
5353
});
5454

55+
//////////////////////////////////////////////////////////////////////////////
56+
//
57+
// --- start git tests
58+
//
59+
//////////////////////////////////////////////////////////////////////////////
60+
61+
const testCheckRemoteGitExist = async (positive: boolean) => {
62+
const { safeLoggingUrl, source, sourcePath } = await getMockedDataForGitTests(
63+
positive
64+
);
65+
if (!fs.existsSync(sourcePath)) {
66+
createGenerated(sourcePath);
67+
}
68+
await checkRemoteGitExist(sourcePath, source, safeLoggingUrl);
69+
};
70+
71+
describe("test checkRemoteGitExist function", () => {
72+
it("postive Test", async () => {
73+
await testCheckRemoteGitExist(true);
74+
// no exception thrown
75+
});
76+
// cannot do negative test because it will take too long
77+
// and timeout
78+
xit("negative Test", async () => {
79+
try {
80+
await testCheckRemoteGitExist(false);
81+
expect(true).toBe(false);
82+
} catch (e) {
83+
expect(e).toBeDefined();
84+
}
85+
});
86+
});
87+
88+
const testGitFetchPull = async (positive: boolean) => {
89+
const { safeLoggingUrl, sourcePath } = await getMockedDataForGitTests(
90+
positive
91+
);
92+
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
93+
await gitFetchPull(sourcePath, safeLoggingUrl);
94+
}
95+
};
96+
97+
describe("test gitFetchPull function", () => {
98+
it("postive Test", async () => {
99+
await testGitFetchPull(true);
100+
// no exception thrown
101+
});
102+
it("negative Test", async () => {
103+
try {
104+
await testGitFetchPull(false);
105+
expect(true).toBe(false);
106+
} catch (e) {
107+
expect(e).toBeDefined();
108+
}
109+
});
110+
});
111+
112+
const testGitCheckout = async (positive: boolean) => {
113+
const { sourcePath } = await getMockedDataForGitTests(positive);
114+
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
115+
await gitCheckout(sourcePath, "v0.0.1");
116+
}
117+
};
118+
119+
describe("test gitCheckout function", () => {
120+
it("postive Test", async () => {
121+
await testGitCheckout(true);
122+
// no exception thrown
123+
});
124+
it("negative Test", async () => {
125+
try {
126+
await testGitCheckout(false);
127+
expect(true).toBe(false);
128+
} catch (e) {
129+
expect(e).toBeDefined();
130+
}
131+
});
132+
});
133+
134+
describe("test gitClone function", () => {
135+
it("postive Test", async () => {
136+
const git = simpleGit();
137+
git.clone = async () => {
138+
return "ok";
139+
};
140+
await gitClone(git, "source", "path");
141+
// no exception thrown
142+
});
143+
it("negative Test", async () => {
144+
const git = simpleGit();
145+
git.clone = async () => {
146+
throw new Error("Error");
147+
};
148+
try {
149+
await gitClone(git, "source", "path");
150+
expect(true).toBe(false);
151+
} catch (e) {
152+
expect(e.message).toBe("Error");
153+
}
154+
});
155+
});
156+
157+
describe("Validate remote git source", () => {
158+
test("Validating that a git source is cloned to .spk/templates", async () => {
159+
jest
160+
.spyOn(generate, "checkRemoteGitExist")
161+
.mockReturnValueOnce(Promise.resolve());
162+
jest.spyOn(generate, "gitFetchPull").mockReturnValueOnce(Promise.resolve());
163+
jest.spyOn(generate, "gitCheckout").mockReturnValueOnce(Promise.resolve());
164+
165+
const mockParentPath = "src/commands/infra/mocks/discovery-service";
166+
const mockProjectPath = "src/commands/infra/mocks/discovery-service/west";
167+
const sourceConfiguration = validateDefinition(
168+
mockParentPath,
169+
mockProjectPath
170+
);
171+
const source = validateTemplateSources(
172+
sourceConfiguration,
173+
mockParentPath,
174+
mockProjectPath
175+
);
176+
try {
177+
await validateRemoteSource(source);
178+
expect(true).toBe(false);
179+
} catch (err) {
180+
expect(err).toBeDefined();
181+
}
182+
});
183+
});
184+
185+
jest.spyOn(generate, "gitClone").mockReturnValue(Promise.resolve());
186+
jest.spyOn(generate, "createGenerated").mockReturnValue();
187+
jest.spyOn(generate, "checkTfvars").mockReturnValue();
188+
jest.spyOn(generate, "writeTfvarsFile").mockReturnValue();
189+
190+
//////////////////////////////////////////////////////////////////////////////
191+
//
192+
// --- end git tests
193+
//
194+
//////////////////////////////////////////////////////////////////////////////
195+
55196
describe("fetch execute function", () => {
56197
it("negative time, expected exit code to be 1", async () => {
57198
const exitFn = jest.fn();
@@ -66,6 +207,31 @@ describe("fetch execute function", () => {
66207
expect(exitFn).toBeCalledTimes(1);
67208
expect(exitFn.mock.calls).toEqual([[1]]);
68209
});
210+
it("negative time, simulate error in generateConfig function", async () => {
211+
jest
212+
.spyOn(generate, "validateDefinition")
213+
.mockReturnValueOnce(DefinitionYAMLExistence.PARENT_ONLY);
214+
jest.spyOn(generate, "validateTemplateSources").mockReturnValueOnce({});
215+
jest
216+
.spyOn(generate, "validateRemoteSource")
217+
.mockReturnValueOnce(Promise.resolve());
218+
jest
219+
.spyOn(infraCommon, "getSourceFolderNameFromURL")
220+
.mockImplementationOnce(() => {
221+
throw new Error("Fake");
222+
});
223+
const exitFn = jest.fn();
224+
await execute(
225+
{
226+
output: "",
227+
project: "test"
228+
},
229+
exitFn
230+
);
231+
232+
expect(exitFn).toBeCalledTimes(1);
233+
expect(exitFn.mock.calls).toEqual([[1]]);
234+
});
69235
it("with project value", async () => {
70236
jest
71237
.spyOn(generate, "validateDefinition")
@@ -489,135 +655,6 @@ const getMockedDataForGitTests = async (
489655
};
490656
};
491657

492-
const testCheckRemoteGitExist = async (positive: boolean) => {
493-
const { safeLoggingUrl, source, sourcePath } = await getMockedDataForGitTests(
494-
positive
495-
);
496-
if (!fs.existsSync(sourcePath)) {
497-
createGenerated(sourcePath);
498-
}
499-
await checkRemoteGitExist(sourcePath, source, safeLoggingUrl);
500-
};
501-
502-
describe("test checkRemoteGitExist function", () => {
503-
it("postive Test", async () => {
504-
await testCheckRemoteGitExist(true);
505-
// no exception thrown
506-
});
507-
// cannot do negative test because it will take too long
508-
// and timeout
509-
xit("negative Test", async () => {
510-
try {
511-
await testCheckRemoteGitExist(false);
512-
expect(true).toBe(false);
513-
} catch (e) {
514-
expect(e).toBeDefined();
515-
}
516-
});
517-
});
518-
519-
const testGitFetchPull = async (positive: boolean) => {
520-
const { safeLoggingUrl, sourcePath } = await getMockedDataForGitTests(
521-
positive
522-
);
523-
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
524-
await gitFetchPull(sourcePath, safeLoggingUrl);
525-
}
526-
};
527-
528-
describe("test gitFetchPull function", () => {
529-
it("postive Test", async () => {
530-
await testGitFetchPull(true);
531-
// no exception thrown
532-
});
533-
it("negative Test", async () => {
534-
try {
535-
await testGitFetchPull(false);
536-
expect(true).toBe(false);
537-
} catch (e) {
538-
expect(e).toBeDefined();
539-
}
540-
});
541-
});
542-
543-
const testGitCheckout = async (positive: boolean) => {
544-
const { sourcePath } = await getMockedDataForGitTests(positive);
545-
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
546-
await gitCheckout(sourcePath, "v0.0.1");
547-
}
548-
};
549-
550-
describe("test gitCheckout function", () => {
551-
it("postive Test", async () => {
552-
await testGitCheckout(true);
553-
// no exception thrown
554-
});
555-
it("negative Test", async () => {
556-
try {
557-
await testGitCheckout(false);
558-
expect(true).toBe(false);
559-
} catch (e) {
560-
expect(e).toBeDefined();
561-
}
562-
});
563-
});
564-
565-
describe("test gitClone function", () => {
566-
it("postive Test", async () => {
567-
const git = simpleGit();
568-
git.clone = async () => {
569-
return "ok";
570-
};
571-
await gitClone(git, "source", "path");
572-
// no exception thrown
573-
});
574-
it("negative Test", async () => {
575-
const git = simpleGit();
576-
git.clone = async () => {
577-
throw new Error("Error");
578-
};
579-
try {
580-
await gitClone(git, "source", "path");
581-
expect(true).toBe(false);
582-
} catch (e) {
583-
expect(e.message).toBe("Error");
584-
}
585-
});
586-
});
587-
588-
describe("Validate remote git source", () => {
589-
test("Validating that a git source is cloned to .spk/templates", async () => {
590-
jest
591-
.spyOn(generate, "checkRemoteGitExist")
592-
.mockReturnValueOnce(Promise.resolve());
593-
jest.spyOn(generate, "gitFetchPull").mockReturnValueOnce(Promise.resolve());
594-
jest.spyOn(generate, "gitCheckout").mockReturnValueOnce(Promise.resolve());
595-
596-
const mockParentPath = "src/commands/infra/mocks/discovery-service";
597-
const mockProjectPath = "src/commands/infra/mocks/discovery-service/west";
598-
const sourceConfiguration = validateDefinition(
599-
mockParentPath,
600-
mockProjectPath
601-
);
602-
const source = validateTemplateSources(
603-
sourceConfiguration,
604-
mockParentPath,
605-
mockProjectPath
606-
);
607-
try {
608-
await validateRemoteSource(source);
609-
expect(true).toBe(false);
610-
} catch (err) {
611-
expect(err).toBeDefined();
612-
}
613-
});
614-
});
615-
616-
jest.spyOn(generate, "gitClone").mockReturnValue(Promise.resolve());
617-
jest.spyOn(generate, "createGenerated").mockReturnValue();
618-
jest.spyOn(generate, "checkTfvars").mockReturnValue();
619-
jest.spyOn(generate, "writeTfvarsFile").mockReturnValue();
620-
621658
describe("Validate replacement of variables between parent and leaf definitions", () => {
622659
test("Validating that leaf definitions take precedence when generating multi-cluster definitions", async () => {
623660
const mockParentPath = "src/commands/infra/mocks/discovery-service";

0 commit comments

Comments
 (0)