Skip to content

Commit bf697a1

Browse files
JoshwinThomasIBMJoshwinThomasIBM
authored andcommitted
changes for Automated test cases to cover the two locations for test reports
1 parent 889f172 commit bf697a1

File tree

5 files changed

+186
-5
lines changed

5 files changed

+186
-5
lines changed

src/test/MavenTestDevModeActions.ts

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ it('getViewControl works with the correct label', async() => {
3939

4040
it('Open dasboard shows items - Maven', async () => {
4141

42+
43+
await utils.executeMvnClean();// executing the mvn clean to remove the target directory before the main tests
44+
await utils.clearMavenPluginCache();// clear the cache before the tests , ensuring latest plugins will be used for the tests
4245
// Wait for the Liberty Dashboard to load and expand. The dashboard only expands after using the 'expand()' method.
4346
await utils.delay(65000);
4447
section.expand();
@@ -215,20 +218,94 @@ it('View Unit test report for maven project', async () => {
215218

216219
await utils.launchDashboardAction(item,constants.UTR_DASHABOARD_ACTION, constants.UTR_DASHABOARD_MAC_ACTION);
217220
tabs = await new EditorView().getOpenEditorTitles();
218-
//expect (tabs[1], "Unit test report not found").to.equal(constants.SUREFIRE_REPORT_TITLE);
219-
expect (tabs.indexOf(constants.SUREFIRE_REPORT_TITLE)>-1, "Unit test report not found").to.equal(true);
221+
expect (tabs.indexOf(constants.SUREFIRE_REPORT_TITLE)>-1, "Unit test report not found");
222+
await utils.closeEditor();// closing the tab after view unit test report is successful
220223

221224
}).timeout(10000);
222225

223226
it('View Integration test report for maven project', async () => {
224227

225228
await utils.launchDashboardAction(item, constants.ITR_DASHBOARD_ACTION, constants.ITR_DASHBOARD_MAC_ACTION);
226229
tabs = await new EditorView().getOpenEditorTitles();
227-
//expect (tabs[2], "Integration test report not found").to.equal(constants.FAILSAFE_REPORT_TITLE);
228-
expect (tabs.indexOf(constants.FAILSAFE_REPORT_TITLE)>-1, "Integration test report not found").to.equal(true);
230+
expect (tabs.indexOf(constants.FAILSAFE_REPORT_TITLE)>-1, "Integration test report not found");
231+
await utils.closeEditor();// closing the tab after view Integration test report is successful
229232

230233
}).timeout(10000);
231234

235+
it('Run tests for sample maven project with surefire version 3.4.0', async () => {
236+
237+
await utils.clearMavenPluginCache();// Clears the cache to ensure the specific surefire versions are downloaded for the next test
238+
await utils.modifyFileContent(constants.MAVEN_TEST_WRAPPER_APP_POM_PATH, constants.COMMENT_REGEX, constants.SUREFIRE_3_4_0_PLUGIN_CONTENT);// Modifies pom.xml to inlcude surefire version 3.4.0
239+
await utils.launchDashboardAction(item, constants.START_DASHBOARD_ACTION_WITH_PARAM, constants.START_DASHBOARD_MAC_ACTION_WITH_PARAM);
240+
const foundCommand = await utils.chooseCmdFromHistory("-DhotTests=true");
241+
expect(foundCommand).to.be.true;
242+
await utils.delay(30000);
243+
const serverStartStatus = await utils.checkTerminalforServerState(constants.SERVER_START_STRING);
244+
if (!serverStartStatus)
245+
console.log("Server started with params message not found in the terminal ");
246+
else {
247+
console.log("Server succuessfully started");
248+
await utils.launchDashboardAction(item, constants.STOP_DASHBOARD_ACTION, constants.STOP_DASHBOARD_MAC_ACTION);
249+
const serverStopStatus = await utils.checkTerminalforServerState(constants.SERVER_STOP_STRING);
250+
await utils.modifyFileContent(constants.MAVEN_TEST_WRAPPER_APP_POM_PATH, constants.PLUGIN_BLOCK_REGEX,constants.POM_COMMENT);// Removes specific verison of the surefire plugin added in pom file for testing
251+
await utils.clearMavenPluginCache();// Clear the plugin cache to remove the current versions and ensure the latest plugins are used for the next tests.
252+
if (!serverStopStatus) {
253+
console.error("Server stopped message not found in the terminal");
254+
}
255+
else {
256+
console.log("Server stopped successfully");
257+
}
258+
expect(serverStopStatus).to.be.true;
259+
}
260+
expect(serverStartStatus).to.be.true;
261+
}).timeout(350000);
262+
263+
it('check all test reports exists', async () => {
264+
// Define the report paths
265+
const reportPaths = [
266+
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.FAILSAFE_HTML),
267+
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.SUREFIRE_HTML),
268+
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.SITE, constants.SUREFIRE_REPORT_HTML),
269+
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.SITE, constants.FAILSAFE_REPORT_HTML)
270+
];
271+
// Check if all reports exist
272+
const checkPromises = reportPaths.map(reportPath => {
273+
return new Promise(resolve => {
274+
const exists = fs.existsSync(reportPath);
275+
resolve(exists);
276+
});
277+
});
278+
279+
// Wait for all checks to complete
280+
const existenceResults = await Promise.all(checkPromises);
281+
282+
// All report files should exist
283+
expect(existenceResults.every(result => result === true)).to.be.true;
284+
}).timeout(10000);
285+
286+
287+
it('View Unit test report for maven project with surefire 3.4.0', async () => {
288+
289+
//Deleting the reports generated by the latest version of the surefire plugin
290+
let deleteFailsafeReport = await utils.deleteReports(path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.FAILSAFE_HTML));
291+
let deleteSurefireReport = await utils.deleteReports(path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.SUREFIRE_HTML));
292+
expect(deleteFailsafeReport && deleteSurefireReport).to.be.true;
293+
await utils.launchDashboardAction(item,constants.UTR_DASHABOARD_ACTION, constants.UTR_DASHABOARD_MAC_ACTION);
294+
tabs = await new EditorView().getOpenEditorTitles();
295+
expect (tabs.indexOf(constants.SUREFIRE_REPORT_TITLE)>-1, "Unit test report not found");
296+
await utils.closeEditor();// closing the tab after view unit test report with surefire 3.4.0 is successful
297+
298+
}).timeout(10000);
299+
300+
it('View Integration test report for maven project with surefire 3.4.0', async () => {
301+
302+
await utils.launchDashboardAction(item, constants.ITR_DASHBOARD_ACTION, constants.ITR_DASHBOARD_MAC_ACTION);
303+
tabs = await new EditorView().getOpenEditorTitles();
304+
expect (tabs.indexOf(constants.FAILSAFE_REPORT_TITLE)>-1, "Integration test report not found");
305+
await utils.closeEditor();// closing the tab after view Integration test report with surefire 3.4.0 is successful
306+
307+
}).timeout(10000);
308+
232309
/**
233310
* All future test cases should be written before the test that attaches the debugger, as this will switch the UI to the debugger view.
234311
* If, for any reason, a test case needs to be written after the debugger test, ensure that the UI is switched back to the explorer view before executing the subsequent tests.

src/test/definitions/constants.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ export const ATTACH_DEBUGGER_DASHBOARD_MAC_ACTION = "Liberty: Attach debugger";
4040
export const MAVEN_DEVMODE_DEBUG_PORT_PARM = "-DdebugPort";
4141
/** Gradle: Dev mode debug port argument key. */
4242
export const GRADLE_DEVMODE_DEBUG_PORT_PARM = "--libertyDebugPort";
43+
export const CLOSE_EDITOR = "View: Close Editor";
44+
export const TARGET = "target";
45+
export const REPORTS = "reports";
46+
export const SITE = "site";
47+
export const FAILSAFE_HTML = "failsafe.html";
48+
export const SUREFIRE_HTML = "surefire.html";
49+
export const SUREFIRE_REPORT_HTML ="surefire-report.html";
50+
export const FAILSAFE_REPORT_HTML ="failsafe-report.html";
51+
export const COMMENT_REGEX = /<!--\s*Test report insertion point, do not remove\s*-->/;
52+
export const PLUGIN_BLOCK_REGEX = /<!--\s*replace this content\s*-->([\s\S]*?)<!--\s*replace this content end\s*-->/;
53+
export const SUREFIRE_3_4_0_PLUGIN_CONTENT = `<!-- replace this content -->
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-surefire-report-plugin</artifactId>
57+
<version>3.4.0</version>
58+
</plugin>
59+
<!-- replace this content end -->`;
60+
export const POM_COMMENT = '<!-- Test report insertion point, do not remove -->';
61+
export const MAVEN_TEST_WRAPPER_APP_POM_PATH = 'src/test/resources/maven/liberty.maven.test.wrapper.app/pom.xml';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mvn clean
2+
3+
<!-- File is added to execute its content as a command from the command palette -->

src/test/resources/maven/liberty.maven.test.wrapper.app/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<stripVersion>true</stripVersion>
7171
</configuration>
7272
</plugin>
73+
<!-- Test report insertion point, do not remove -->
7374
<plugin>
7475
<groupId>org.apache.maven.plugins</groupId>
7576
<artifactId>maven-failsafe-plugin</artifactId>

src/test/utils/testUtils.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
* SPDX-License-Identifier: EPL-2.0
99
*/
1010
import path = require('path');
11-
import { Workbench, InputBox, DefaultTreeItem, ModalDialog } from 'vscode-extension-tester';
11+
import { Workbench, InputBox, DefaultTreeItem, ModalDialog, VSBrowser } from 'vscode-extension-tester';
1212
import * as fs from 'fs';
1313
import { STOP_DASHBOARD_MAC_ACTION } from '../definitions/constants';
1414
import { MapContextMenuforMac } from './macUtils';
1515
import clipboard = require('clipboardy');
1616
import { expect } from 'chai';
17+
import * as constants from '../definitions/constants';
1718

1819
export function delay(millisec: number) {
1920
return new Promise( resolve => setTimeout(resolve, millisec) );
@@ -202,4 +203,84 @@ export async function clearCommandPalette() {
202203
await dialog.pushButton('Clear');
203204
}
204205

206+
/**
207+
* Function clears the mvn plugin cache
208+
*/
209+
export async function clearMavenPluginCache(): Promise<void> {
210+
// Check if the platform is Linux or macOS
211+
const homeDirectory = process.platform === 'linux' || process.platform === 'darwin' ? process.env.HOME // For Linux/macOS, use HOME
212+
: process.platform === 'win32' ? process.env.USERPROFILE // For Windows, use USERPROFILE
213+
: undefined; // In case the platform is unknown
214+
if (!homeDirectory) {
215+
throw new Error('Home directory not found');
216+
}
217+
const mavenRepoPath = path.join(homeDirectory, '.m2', 'repository', 'org', 'apache', 'maven', 'plugins');
218+
removeDirectoryByPath(mavenRepoPath);
219+
}
220+
221+
export async function removeDirectoryByPath(projectPath: string): Promise<void> {
222+
try {
223+
await fs.promises.access(projectPath);
224+
const projectContent = await fs.promises.readdir(projectPath);
225+
226+
await Promise.all(
227+
projectContent.map(async (projectFiles) => {
228+
const projectContentPath = path.join(projectPath, projectFiles);
229+
const stats = await fs.promises.lstat(projectContentPath);
230+
231+
if (stats.isDirectory()) {
232+
await removeDirectoryByPath(projectContentPath);
233+
} else {
234+
await fs.promises.unlink(projectContentPath);
235+
}
236+
})
237+
);
238+
239+
await fs.promises.rmdir(projectPath);
240+
} catch (error) {
241+
console.error(`Error removing new project: ${error}`);
242+
}
243+
}
244+
245+
// General function to modify any file content using RegExp for searching
246+
export async function modifyFileContent(filePath: string, searchPattern: RegExp, replaceString: string) {
247+
// Read the file
248+
fs.readFile(filePath, 'utf8', (err, data) => {
249+
if (err) {
250+
console.error('Error reading the file:', err);
251+
return;
252+
}
253+
254+
// Check if the searchPattern matches any content in the file
255+
if (searchPattern.test(data)) {
256+
// Replace the matched content with the replaceString
257+
const updatedData = data.replace(searchPattern, replaceString);
258+
259+
// Write the modified content back to the file
260+
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
261+
if (err) {
262+
console.error('Error writing to the file:', err);
263+
} else {
264+
console.log('File updated successfully');
265+
}
266+
});
267+
} else {
268+
console.log(`The pattern "${searchPattern}" was not found in the file.`);
269+
}
270+
});
271+
}
272+
273+
// Method to close the open tabs
274+
export async function closeEditor() {
275+
const workbench = new Workbench();
276+
await workbench.executeCommand(constants.CLOSE_EDITOR);
277+
}
278+
279+
// Method to execute the maven clean before the tests are executed
280+
export async function executeMvnClean() {
281+
await VSBrowser.instance.openResources(path.join(getMvnProjectPath(), 'ForTest.md'));
282+
const workbench = new Workbench();
283+
await workbench.executeCommand('workbench.action.terminal.runSelectedText');
284+
}
285+
205286

0 commit comments

Comments
 (0)