Skip to content

Commit 0e3fb68

Browse files
JoshwinThomasIBMJoshwinThomasIBM
authored andcommitted
Added a single method to modify and revert pom file
1 parent 600ab33 commit 0e3fb68

File tree

3 files changed

+28
-52
lines changed

3 files changed

+28
-52
lines changed

src/test/MavenTestDevModeActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ it('View Integration test report for maven project', async () => {
225225
it('Run tests for sample maven project with surefire version 3.4.0', async () => {
226226

227227
await utils.clearMavenPluginCache();// Clears the cache to ensure the specific surefire versions are downloaded for the next test
228-
await utils.modifyPomFile();// Modifies pom.xml to inlcude surefire version 3.4.0
228+
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
229229
await utils.launchDashboardAction(item, constants.START_DASHBOARD_ACTION_WITH_PARAM, constants.START_DASHBOARD_MAC_ACTION_WITH_PARAM);
230230
const foundCommand = await utils.chooseCmdFromHistory("-DhotTests=true");
231231
expect(foundCommand).to.be.true;
@@ -237,7 +237,7 @@ it('Run tests for sample maven project with surefire version 3.4.0', async () =>
237237
console.log("Server succuessfully started");
238238
await utils.launchDashboardAction(item, constants.STOP_DASHBOARD_ACTION, constants.STOP_DASHBOARD_MAC_ACTION);
239239
const serverStopStatus = await utils.checkTerminalforServerState(constants.SERVER_STOP_STRING);
240-
await utils.revertPomFile();// Removes specific verison of the surefire plugin added in pom file for testing
240+
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
241241
await utils.clearMavenPluginCache();// Clear the plugin cache to remove the current versions and ensure the latest plugins are used for the next tests.
242242
if (!serverStopStatus) {
243243
console.error("Server stopped message not found in the terminal");

src/test/definitions/constants.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,15 @@ export const SITE = "site";
4747
export const FAILSAFE_HTML = "failsafe.html";
4848
export const SUREFIRE_HTML = "surefire.html";
4949
export const SUREFIRE_REPORT_HTML ="surefire-report.html";
50-
export const FAILSAFE_REPORT_HTML ="failsafe-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';

src/test/utils/testUtils.ts

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -232,65 +232,30 @@ export async function removeDirectoryByPath(projectPath: string): Promise<void>
232232
}
233233
}
234234

235-
// Function to modify content inside pom XML to add surefire version 3.4.0 plugin
236-
export async function modifyPomFile() {
237-
//Read the POM file
238-
fs.readFile('src/test/resources/maven/liberty.maven.test.wrapper.app/pom.xml', 'utf8', (err, data) => {
235+
// General function to modify any file content using RegExp for searching
236+
export async function modifyFileContent(filePath: string, searchPattern: RegExp, replaceString: string) {
237+
// Read the file
238+
fs.readFile(filePath, 'utf8', (err, data) => {
239239
if (err) {
240-
console.log('Error reading the file:', err);
241240
console.error('Error reading the file:', err);
242241
return;
243242
}
244-
//Find the specific comment and replace its content
245-
const commentRegex = /<!--\s*Test report insertion point, do not remove\s*-->/;
246-
const newContent = `<plugin>
247-
<groupId>org.apache.maven.plugins</groupId>
248-
<artifactId>maven-surefire-report-plugin</artifactId>
249-
<version>3.4.0</version>
250-
</plugin>`;
251-
// Check if the comment is found
252-
if (commentRegex.test(data)) {
253-
const updatedData = data.replace(commentRegex, `<!-- replace this content -->\n${newContent}\n<!-- replace this content end -->`);
254-
//Write the modified content back to the POM file
255-
fs.writeFile('src/test/resources/maven/liberty.maven.test.wrapper.app/pom.xml', updatedData, 'utf8', (err) => {
256-
if (err) {
257-
console.log('Error writing to the file:', err);
258-
console.error('Error writing to the file:', err);
259-
} else {
260-
console.log('POM file updated successfully');
261-
}
262-
});
263-
} else {
264-
console.log('Comment with the specified marker not found in the POM file');
265-
}
266-
});
267-
}
268-
269-
// Function to revert changes made on the pom file for including surefire 3.4.0
270-
export async function revertPomFile() {
271-
//path to pom.xml in the test project
272-
const pomFilePath = 'src/test/resources/maven/liberty.maven.test.wrapper.app/pom.xml';
273-
//Read the POM file
274-
fs.readFile(pomFilePath, 'utf8', (err, data) => {
275-
if (err) {
276-
console.error('Error reading the file:', err);
277-
return;
278-
}
279-
//Find the inserted plugin block and revert it back to the original comment
280-
const pluginBlockRegex = /<!--\s*replace this content\s*-->([\s\S]*?)<!--\s*replace this content end\s*-->/;
281-
// Check if the inserted plugin block exists
282-
if (pluginBlockRegex.test(data)) {
283-
const revertedData = data.replace(pluginBlockRegex, `<!-- Test report insertion point, do not remove -->`);
284-
//Write the reverted content back to the POM file
285-
fs.writeFile(pomFilePath, revertedData, 'utf8', (err) => {
243+
244+
// Check if the searchPattern matches any content in the file
245+
if (searchPattern.test(data)) {
246+
// Replace the matched content with the replaceString
247+
const updatedData = data.replace(searchPattern, replaceString);
248+
249+
// Write the modified content back to the file
250+
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
286251
if (err) {
287252
console.error('Error writing to the file:', err);
288253
} else {
289-
console.log('POM file reverted successfully');
254+
console.log('File updated successfully');
290255
}
291256
});
292257
} else {
293-
console.log('Plugin block not found, nothing to revert.');
258+
console.log(`The pattern "${searchPattern}" was not found in the file.`);
294259
}
295260
});
296261
}

0 commit comments

Comments
 (0)