Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 81 additions & 4 deletions src/test/MavenTestDevModeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ it('getViewControl works with the correct label', async() => {

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


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

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

}).timeout(10000);

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

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

}).timeout(10000);

it('Run tests for sample maven project with surefire version 3.4.0', async () => {

await utils.clearMavenPluginCache();// Clears the cache to ensure the specific surefire versions are downloaded for the next test
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
await utils.launchDashboardAction(item, constants.START_DASHBOARD_ACTION_WITH_PARAM, constants.START_DASHBOARD_MAC_ACTION_WITH_PARAM);
const foundCommand = await utils.chooseCmdFromHistory("-DhotTests=true");
expect(foundCommand).to.be.true;
await utils.delay(30000);
const serverStartStatus = await utils.checkTerminalforServerState(constants.SERVER_START_STRING);
if (!serverStartStatus)
console.log("Server started with params message not found in the terminal ");
else {
console.log("Server succuessfully started");
await utils.launchDashboardAction(item, constants.STOP_DASHBOARD_ACTION, constants.STOP_DASHBOARD_MAC_ACTION);
const serverStopStatus = await utils.checkTerminalforServerState(constants.SERVER_STOP_STRING);
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
await utils.clearMavenPluginCache();// Clear the plugin cache to remove the current versions and ensure the latest plugins are used for the next tests.
if (!serverStopStatus) {
console.error("Server stopped message not found in the terminal");
}
else {
console.log("Server stopped successfully");
}
expect(serverStopStatus).to.be.true;
}
expect(serverStartStatus).to.be.true;
}).timeout(350000);

it('check all test reports exists', async () => {
// Define the report paths
const reportPaths = [
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.FAILSAFE_HTML),
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.SUREFIRE_HTML),
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.SITE, constants.SUREFIRE_REPORT_HTML),
path.join(utils.getMvnProjectPath(), constants.TARGET, constants.SITE, constants.FAILSAFE_REPORT_HTML)
];
// Check if all reports exist
const checkPromises = reportPaths.map(reportPath => {
return new Promise(resolve => {
const exists = fs.existsSync(reportPath);
resolve(exists);
});
});

// Wait for all checks to complete
const existenceResults = await Promise.all(checkPromises);

// All report files should exist
expect(existenceResults.every(result => result === true)).to.be.true;
}).timeout(10000);


it('View Unit test report for maven project with surefire 3.4.0', async () => {

//Deleting the reports generated by the latest version of the surefire plugin
let deleteFailsafeReport = await utils.deleteReports(path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.FAILSAFE_HTML));
let deleteSurefireReport = await utils.deleteReports(path.join(utils.getMvnProjectPath(), constants.TARGET, constants.REPORTS, constants.SUREFIRE_HTML));
expect(deleteFailsafeReport && deleteSurefireReport).to.be.true;
await utils.launchDashboardAction(item,constants.UTR_DASHABOARD_ACTION, constants.UTR_DASHABOARD_MAC_ACTION);
tabs = await new EditorView().getOpenEditorTitles();
expect (tabs.indexOf(constants.SUREFIRE_REPORT_TITLE)>-1, "Unit test report not found");
await utils.closeEditor();// closing the tab after view unit test report with surefire 3.4.0 is successful

}).timeout(10000);

it('View Integration test report for maven project with surefire 3.4.0', async () => {

await utils.launchDashboardAction(item, constants.ITR_DASHBOARD_ACTION, constants.ITR_DASHBOARD_MAC_ACTION);
tabs = await new EditorView().getOpenEditorTitles();
expect (tabs.indexOf(constants.FAILSAFE_REPORT_TITLE)>-1, "Integration test report not found");
await utils.closeEditor();// closing the tab after view Integration test report with surefire 3.4.0 is successful

}).timeout(10000);

/**
* All future test cases should be written before the test that attaches the debugger, as this will switch the UI to the debugger view.
* 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.
Expand Down
19 changes: 19 additions & 0 deletions src/test/definitions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,22 @@ export const ATTACH_DEBUGGER_DASHBOARD_MAC_ACTION = "Liberty: Attach debugger";
export const MAVEN_DEVMODE_DEBUG_PORT_PARM = "-DdebugPort";
/** Gradle: Dev mode debug port argument key. */
export const GRADLE_DEVMODE_DEBUG_PORT_PARM = "--libertyDebugPort";
export const CLOSE_EDITOR = "View: Close Editor";
export const TARGET = "target";
export const REPORTS = "reports";
export const SITE = "site";
export const FAILSAFE_HTML = "failsafe.html";
export const SUREFIRE_HTML = "surefire.html";
export const SUREFIRE_REPORT_HTML ="surefire-report.html";
export const FAILSAFE_REPORT_HTML ="failsafe-report.html";
export const COMMENT_REGEX = /<!--\s*Test report insertion point, do not remove\s*-->/;
export const PLUGIN_BLOCK_REGEX = /<!--\s*replace this content\s*-->([\s\S]*?)<!--\s*replace this content end\s*-->/;
export const SUREFIRE_3_4_0_PLUGIN_CONTENT = `<!-- replace this content -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- replace this content end -->`;
export const POM_COMMENT = '<!-- Test report insertion point, do not remove -->';
export const MAVEN_TEST_WRAPPER_APP_POM_PATH = 'src/test/resources/maven/liberty.maven.test.wrapper.app/pom.xml';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mvn clean

<!-- File is added to execute its content as a command from the command palette -->
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<stripVersion>true</stripVersion>
</configuration>
</plugin>
<!-- Test report insertion point, do not remove -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand Down
83 changes: 82 additions & 1 deletion src/test/utils/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
* SPDX-License-Identifier: EPL-2.0
*/
import path = require('path');
import { Workbench, InputBox, DefaultTreeItem, ModalDialog } from 'vscode-extension-tester';
import { Workbench, InputBox, DefaultTreeItem, ModalDialog, VSBrowser } from 'vscode-extension-tester';
import * as fs from 'fs';
import { STOP_DASHBOARD_MAC_ACTION } from '../definitions/constants';
import { MapContextMenuforMac } from './macUtils';
import clipboard = require('clipboardy');
import { expect } from 'chai';
import * as constants from '../definitions/constants';

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

/**
* Function clears the mvn plugin cache
*/
export async function clearMavenPluginCache(): Promise<void> {
// Check if the platform is Linux or macOS
const homeDirectory = process.platform === 'linux' || process.platform === 'darwin' ? process.env.HOME // For Linux/macOS, use HOME
: process.platform === 'win32' ? process.env.USERPROFILE // For Windows, use USERPROFILE
: undefined; // In case the platform is unknown
if (!homeDirectory) {
throw new Error('Home directory not found');
}
const mavenRepoPath = path.join(homeDirectory, '.m2', 'repository', 'org', 'apache', 'maven', 'plugins');
removeDirectoryByPath(mavenRepoPath);
}

export async function removeDirectoryByPath(projectPath: string): Promise<void> {
try {
await fs.promises.access(projectPath);
const projectContent = await fs.promises.readdir(projectPath);

await Promise.all(
projectContent.map(async (projectFiles) => {
const projectContentPath = path.join(projectPath, projectFiles);
const stats = await fs.promises.lstat(projectContentPath);

if (stats.isDirectory()) {
await removeDirectoryByPath(projectContentPath);
} else {
await fs.promises.unlink(projectContentPath);
}
})
);

await fs.promises.rmdir(projectPath);
} catch (error) {
console.error(`Error removing new project: ${error}`);
}
}

// General function to modify any file content using RegExp for searching
export async function modifyFileContent(filePath: string, searchPattern: RegExp, replaceString: string) {
// Read the file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}

// Check if the searchPattern matches any content in the file
if (searchPattern.test(data)) {
// Replace the matched content with the replaceString
const updatedData = data.replace(searchPattern, replaceString);

// Write the modified content back to the file
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
if (err) {
console.error('Error writing to the file:', err);
} else {
console.log('File updated successfully');
}
});
} else {
console.log(`The pattern "${searchPattern}" was not found in the file.`);
}
});
}

// Method to close the open tabs
export async function closeEditor() {
const workbench = new Workbench();
await workbench.executeCommand(constants.CLOSE_EDITOR);
}

// Method to execute the maven clean before the tests are executed
export async function executeMvnClean() {
await VSBrowser.instance.openResources(path.join(getMvnProjectPath(), 'ForTest.md'));
const workbench = new Workbench();
await workbench.executeCommand('workbench.action.terminal.runSelectedText');
}


Loading