-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathExtn-Git-integ-test.js
More file actions
353 lines (302 loc) · 16.3 KB
/
Extn-Git-integ-test.js
File metadata and controls
353 lines (302 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, it, expect, beforeEach, afterEach, awaitsForDone,beforeAll, awaitsFor */
define(function (require, exports, module) {
if(!Phoenix.isNativeApp) {
return;
}
let $, __PR, testWindow, ExtensionLoader, Menus, Commands, CommandManager, EditorManager,
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
anotherTestFolder = SpecRunnerUtils.getTestPath("/spec/LowLevelFileIO-test-files");
let testPathGit;
function forCommandEnabled(commandID, enabled = true) {
const command = CommandManager.get(commandID);
if(!commandID){
throw new Error(`no such command ${commandID}`);
}
return awaitsFor(()=>{
return command && command.getEnabled() === enabled;
}, `Git ${commandID} to be ${enabled? "enabled" : "disabled"}`);
}
async function showGitPanel() {
if(!$("#git-panel").is(":visible")) {
await __PR.execCommand(Commands.CMD_GIT_TOGGLE_PANEL);
}
}
describe("LegacyInteg:Git Workflows test", function () {
beforeAll(async function () {
testWindow = await SpecRunnerUtils.createTestWindowAndRun({forceReload: true});
// Load module instances from brackets.test
$ = testWindow.$;
__PR = testWindow.__PR;
Menus = testWindow.brackets.test.Menus;
ExtensionLoader = testWindow.brackets.test.ExtensionLoader;
Commands = testWindow.brackets.test.Commands;
CommandManager = testWindow.brackets.test.CommandManager;
EditorManager = testWindow.brackets.test.EditorManager;
testPathGit = await SpecRunnerUtils.getTempTestDirectory("/spec/EditorCommandHandlers-test-files");
await SpecRunnerUtils.loadProjectInTestWindow(testPathGit);
await ExtensionLoader._loadDefaultExtension("Git");
await awaitsFor(()=>{
return !!Menus.getSubMenu(Menus.SubMenuIds.GIT_SUB_MENU);
}, "Git menus to be present", 10000);
}, 30000);
describe("Init repo and do all tests in order", function () {
// ordering of tests in this matters and it may not run as individual tests.
let $gitPanel, $gitIcon;
beforeAll(async function () {
$gitPanel = $("#git-panel");
$gitIcon = $("#git-toolbar-icon");
});
it("should only git settings, init and clone commands be enabled in non-git repos", async function () {
await forCommandEnabled(Commands.CMD_GIT_INIT);
await forCommandEnabled(Commands.CMD_GIT_CLONE);
await forCommandEnabled(Commands.CMD_GIT_SETTINGS_COMMAND_ID);
await forCommandEnabled(Commands.CMD_GIT_TOGGLE_PANEL);
// others are disabled
await forCommandEnabled(Commands.CMD_GIT_REFRESH, false);
await forCommandEnabled(Commands.CMD_GIT_REFRESH, false);
await forCommandEnabled(Commands.CMD_GIT_FETCH, false);
await forCommandEnabled(Commands.CMD_GIT_PULL, false);
await forCommandEnabled(Commands.CMD_GIT_PUSH, false);
});
it("Should Git icon be hidden in non-git repo", async function () {
expect($gitIcon.is(":visible")).toBeFalse();
});
it("Should be able to show git panel in non-git repo, and icon will come up", async function () {
await __PR.execCommand(Commands.CMD_GIT_TOGGLE_PANEL);
expect($gitPanel.is(":visible")).toBeTrue();
expect($gitIcon.is(":visible")).toBeTrue();
// verify that only the init and clone button is visible
expect($gitPanel.find(".git-init").is(":visible")).toBeTrue();
expect($gitPanel.find(".git-clone").is(":visible")).toBeTrue();
// in non git repos the git buttons are not visible
expect($gitPanel.find(".git-commit").is(":visible")).toBeFalse();
expect($gitPanel.find(".git-prev-gutter").is(":visible")).toBeFalse();
expect($gitPanel.find(".git-file-history").is(":visible")).toBeFalse();
expect($gitPanel.find(".git-right-icons").is(":visible")).toBeFalse();
});
it("Should be able to initialize git repo", async function () {
await showGitPanel();
$gitPanel.find(".git-init").click();
await awaitsFor(()=>{
return $gitPanel.find(".modified-file").length === 4;
}, "4 files to be added in modified files list", 10000);
expect($(".check-all").prop("checked")).toBeFalse();
});
function clickOpenFile(elementNumber) {
const $elements = $gitPanel.find(".modified-file"); // Get all .modified-file elements
if (elementNumber >= 0 && elementNumber < $elements.length) {
$($elements[elementNumber]).trigger("mousedown"); // Trigger mousedown on the specified element
} else {
console.error("Invalid element number:", elementNumber); // Handle invalid index
}
}
it("Should clicking on file in git panel should open it", async function () {
await showGitPanel();
clickOpenFile(0);
await awaitsFor(()=>{
const editor = EditorManager.getActiveEditor();
if(!editor){
return false;
}
return editor.document.file.fullPath.endsWith(".gitignore");
}, "first file to open");
});
it("should git username and email be valid", async function () {
const tempUser = "phcodeTestGitUser";
const tempEmail = "phcodeTestGitUser@gmail.com";
// username validate
let currentVal = await testWindow.phoenixGitEvents.Git.getConfig("user.name");
if(!currentVal) {
await testWindow.phoenixGitEvents.Git.setConfig("user.name", tempUser, true);
currentVal = await testWindow.phoenixGitEvents.Git.getConfig("user.name");
expect(currentVal).toBe(tempUser);
} else {
expect(currentVal).toBeDefined();
}
// email validate
currentVal = await testWindow.phoenixGitEvents.Git.getConfig("user.email");
if(!currentVal) {
await testWindow.phoenixGitEvents.Git.setConfig("user.email", tempEmail, true);
currentVal = await testWindow.phoenixGitEvents.Git.getConfig("user.email");
expect(currentVal).toBe(tempEmail);
} else {
expect(currentVal).toBeDefined();
}
});
async function commitAllBtnClick() {
await showGitPanel();
if(!$(".check-all").prop("checked")) {
$(".check-all").click();
await awaitsFor(()=>{
const checkboxes = document.querySelectorAll(".check-one");
const commitIsDisabled = $(".git-commit").prop("disabled");
return Array.from(checkboxes).every(checkbox => checkbox.checked) && !commitIsDisabled;
}, "All files to be staged for commit", 10000);
}
$(".git-commit").click();
await __PR.waitForModalDialog("#git-commit-dialog");
}
async function commmitDlgWithMessage(message) {
$("input[name='commit-message']").val(message);
__PR.clickDialogButtonID(__PR.Dialogs.DIALOG_BTN_OK);
await __PR.waitForModalDialogClosed("#git-commit-dialog");
}
function expectTextToContain(srcText, list) {
const nonEmptyLines = srcText
.split("\n") // Split the text into lines
.map(line => line.trim()) // Trim each line
.filter(line => line !== "").join("\n"); // Remove empty lines
for(const text of list) {
expect(nonEmptyLines).toContain(text);
}
}
it("Should be able to stage, show commit dialog and cancel dialog on initialized git repo", async function () {
await commitAllBtnClick();
__PR.clickDialogButtonID(__PR.Dialogs.DIALOG_BTN_CANCEL);
await __PR.waitForModalDialogClosed("#git-commit-dialog");
});
it("Should git dialog show commit diff and lint errors", async function () {
await commitAllBtnClick();
// check lint errors
await awaitsFor(()=>{
return $(".lint-errors").text().includes("test.html");
}, "lint errors to be shown", 10000);
expect($(".lint-errors").text()).toContain("<html> is missing required \"lang\" attribute");
expect($(".lint-errors").is(":visible")).toBeTrue();
// check commit diff
await awaitsFor(()=>{
return $(".commit-diff").text().includes("test.html");
}, "commit-diff to be shown", 10000);
expectTextToContain($(".commit-diff").text(), [
".gitignore", "test.css", "test.html", "test.js",
"/node_modules/", "color:", `<head>`,
`console.log`
]);
// dismiss dialog
__PR.clickDialogButtonID(__PR.Dialogs.DIALOG_BTN_CANCEL);
await __PR.waitForModalDialogClosed("#git-commit-dialog");
});
it("Should be able to commit the files", async function () {
await commitAllBtnClick();
await commmitDlgWithMessage("first commit");
await awaitsFor(()=>{
return $(".git-edited-list tr").length === 0;
}, "no files to be commited", 10000);
});
it("Should editing new file and saving add it to changed files list", async function () {
__PR.setCursors(["5:15"]);
__PR.typeAtCursor("\nhelloIG");
await __PR.saveActiveFile();
await awaitsFor(()=>{
return $(".git-edited-list tr").length === 1;
}, "new edited file to come up in status", 10000);
// now commit
await commitAllBtnClick();
await commmitDlgWithMessage("second commit");
await awaitsFor(()=>{
return $(".git-edited-list tr").length === 0;
}, "no files to be commited", 10000);
});
async function gotoChange(line, direction) {
await awaitsFor(()=>{
$(`.git-${direction}-gutter`).click();
const editor = EditorManager.getActiveEditor();
return editor.getCursorPos().line === line;
}, `should go to previous change ${line}`, 10000, 100);
}
it("Should be able to navigate to next and previous changes and then discard changes", async function () {
__PR.setCursors(["1:1"]);
__PR.typeAtCursor("changeLine1\n");
__PR.setCursors(["4:1"]);
__PR.typeAtCursor("changeLine2\n");
await __PR.saveActiveFile();
await awaitsFor(()=>{
return $(".git-edited-list tr").length === 1;
}, "new edited file to come up in status", 10000);
// next previous buttons tests
await gotoChange(3, "prev");
await gotoChange(0, "prev");
await gotoChange(3, "next");
// discard all changes with panel button
$(".btn-git-undo").click();
__PR.clickDialogButtonID(__PR.Dialogs.DIALOG_BTN_OK);
await awaitsFor(()=>{
return $(".git-edited-list tr").length === 0;
}, "no files to be commited", 10000);
});
async function waitForHistoryVisible(visible) {
await awaitsFor(() => {
return $gitPanel.find("#git-history-list").is(":visible") === visible;
}, `History list to be visible: ${visible}`);
}
async function testHistoryToggle(whichHistory) {
const $historyToggleButton = $gitPanel.find(whichHistory);
$historyToggleButton.trigger("click");
await waitForHistoryVisible(true);
const $historyList = $gitPanel.find("#git-history-list");
// Check if the first and second commits are displayed
const $historyCommits = $historyList.find(".commit-subject");
expect($historyCommits.length).toBeGreaterThanOrEqual(2);
// Verify the content of the first and second commits
expect($historyCommits.eq(0).text().trim()).toBe("second commit");
expect($historyCommits.eq(1).text().trim()).toBe("first commit");
$historyToggleButton.trigger("click");
await waitForHistoryVisible(false);
}
it("should show history with first and second commit on clicking global history", async () => {
await testHistoryToggle(".git-history-toggle");
});
it("should show history with first and second commit on clicking global history", async () => {
await testHistoryToggle(".git-file-history");
});
async function waitForHistoryViewerVisible(visible) {
await awaitsFor(() => {
return $("#history-viewer").is(":visible") === visible;
}, `History viewer to be visible: ${visible}`);
}
async function testHistoryViewerToggle(commitIndex) {
const $historyList = $gitPanel.find("#git-history-list");
const $commitRow = $historyList.find(".history-commit").eq(commitIndex);
// Ensure the commit row exists
expect($commitRow.length).toBe(1);
// Click the row to show the history viewer
$commitRow.trigger("click");
await waitForHistoryViewerVisible(true);
// Verify that the history viewer shows the correct commit
const $historyViewer = $("#editor-holder .git");
await awaitsFor(() => {
return $historyViewer.find(".commit-title").text().trim().includes("first commit");
}, `History viewer to have commit detail`);
// Click the row again to dismiss the history viewer
$commitRow.trigger("click");
await waitForHistoryViewerVisible(false);
}
it("should show the history viewer when clicking the first commit row and dismiss it on clicking again", async () => {
const $historyToggleButton = $gitPanel.find(".git-history-toggle");
$historyToggleButton.trigger("click");
await waitForHistoryVisible(true); // Ensure the history list is visible
await testHistoryViewerToggle(1); // Test for the first commit row
});
});
});
});