Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#342: implement unit tests for mvn ToolCommandlet #373

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06ef780
first implementation
mvomiero May 28, 2024
acb5bfc
code cleanup
mvomiero May 31, 2024
21bdc6f
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero May 31, 2024
0224dba
commandlet arguments refactoring in test
mvomiero May 31, 2024
6d9beea
remove plugins
mvomiero May 31, 2024
cf008fd
check settingssecurityfile
mvomiero May 31, 2024
43dd789
settings.xml added to git
mvomiero Jun 3, 2024
5ee8cd7
update mvn rights
mvomiero Jun 3, 2024
a58348a
add mvn executable for windows
mvomiero Jun 3, 2024
3208742
fix operating system name
mvomiero Jun 3, 2024
4f5137e
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
mvomiero Jun 14, 2024
eed1d84
implement change requests
mvomiero Jun 14, 2024
252eaaf
implement change request
mvomiero Jun 14, 2024
5b609cc
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jun 19, 2024
e8d1609
implement assertion for content of settings files
mvomiero Jun 19, 2024
ea57690
add javadoc
mvomiero Jun 20, 2024
c2258cd
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jun 21, 2024
051ee46
Update cli/src/test/resources/ide-projects/mvn/project/workspaces/mai…
mvomiero Jun 26, 2024
d46ae28
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
hohwille Jul 1, 2024
0770b17
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jul 2, 2024
5d69643
implement change request and fix bug format encrypted values
mvomiero Jul 2, 2024
483d238
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jul 2, 2024
33408ca
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
hohwille Jul 4, 2024
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
Prev Previous commit
Next Next commit
implement change request and fix bug format encrypted values
  • Loading branch information
mvomiero committed Jul 2, 2024
commit 5d69643ac9df8fa929608040461d6896f89954ce
28 changes: 13 additions & 15 deletions cli/src/main/java/com/devonfw/tools/ide/tool/mvn/Mvn.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,14 @@ public class Mvn extends PluginBasedCommandlet {
*/
public static final String SETTINGS_FILE = "settings.xml";

hohwille marked this conversation as resolved.
Show resolved Hide resolved
/** The name of the M2 repo */
public static final String M2_CONFIG_FOLDER = ".m2";

private static final String DOCUMENTATION_PAGE_CONF = "https://github.com/devonfw/IDEasy/blob/main/documentation/conf.adoc";

private static final String ERROR_SETTINGS_FILE_MESSAGE =
"Failed to create settings file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;
private static final String ERROR_SETTINGS_FILE_MESSAGE = "Failed to create settings file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;

private static final String ERROR_SETTINGS_SECURITY_FILE_MESSAGE =
"Failed to create settings security file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;
public static final VariableSyntax VARIABLE_SYNTAX = VariableSyntax.SQUARE;
"Failed to create settings security file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;

private static final VariableSyntax VARIABLE_SYNTAX = VariableSyntax.SQUARE;

/**
* The constructor.
Expand Down Expand Up @@ -100,7 +97,8 @@ public void postInstall() {
templatesConfMvnFolder = templatesConfMvnLegacyFolder;
legacy = true;
} else {
this.context.warning("No maven templates found. Neither in {} nor in {} - configuration broken", templatesConfMvnFolder, templatesConfMvnLegacyFolder);
this.context.warning("No maven templates found. Neither in {} nor in {} - configuration broken", templatesConfMvnFolder,
templatesConfMvnLegacyFolder);
hasMvnTemplates = false;
}
}
Expand Down Expand Up @@ -137,11 +135,11 @@ private void createSettingsSecurityFile(Path settingsSecurityFile) {

ProcessResult result = pc.run(ProcessMode.DEFAULT_CAPTURE);

String encryptedMasterPassword = result.getOut().toString();
String encryptedMasterPassword = result.getOut().get(0);

String settingsSecurityXml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<settingsSecurity>\n" + " <master>" + encryptedMasterPassword + "</master>\n"
+ "</settingsSecurity>";
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<settingsSecurity>\n" + " <master>" + encryptedMasterPassword + "</master>\n"
+ "</settingsSecurity>";
try {
Files.writeString(settingsSecurityFile, settingsSecurityXml);
step.success();
Expand Down Expand Up @@ -173,7 +171,7 @@ private void createSettingsFile(Path settingsFile, Path settingsSecurityFile, Pa
} else if (!gitSettingsUrl.equals(gitContext.DEFAULT_SETTINGS_GIT_URL)) {
Set<String> variables = findVariables(content);
for (String variable : variables) {
String secret = getEncryptedPassword(variable, settingsSecurityFile);
hohwille marked this conversation as resolved.
Show resolved Hide resolved
String secret = getEncryptedPassword(variable);
content = content.replace(VARIABLE_SYNTAX.create(variable), secret);
}
}
Expand All @@ -186,15 +184,15 @@ private void createSettingsFile(Path settingsFile, Path settingsSecurityFile, Pa
}
}

private String getEncryptedPassword(String variable, Path settingsSecurityFile) {
private String getEncryptedPassword(String variable) {

String input = this.context.askForInput("Please enter secret value for variable " + variable + ":");

ProcessContext pc = this.context.newProcess().executable("mvn");
pc.addArgs("--encrypt-password", input);
ProcessResult result = pc.run(ProcessMode.DEFAULT_CAPTURE);

String encryptedPassword = result.getOut().toString();
String encryptedPassword = result.getOut().get(0);
hohwille marked this conversation as resolved.
Show resolved Hide resolved
this.context.info("Encrypted as " + encryptedPassword);

return encryptedPassword;
Expand All @@ -220,7 +218,7 @@ public void installPlugin(PluginDescriptor plugin) {
this.context.success("Successfully added {} to {}", plugin.getName(), mavenPlugin.toString());
} else {
this.context.warning("Plugin {} has wrong properties\n" //
+ "Please check the plugin properties file in {}", mavenPlugin.getFileName(), mavenPlugin.toAbsolutePath());
+ "Please check the plugin properties file in {}", mavenPlugin.getFileName(), mavenPlugin.toAbsolutePath());
}
}
}
9 changes: 5 additions & 4 deletions cli/src/test/java/com/devonfw/tools/ide/tool/mvn/MvnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class MvnTest extends AbstractIdeContextTest {

private static final String PROJECT_MVN = "mvn";

private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\[(.*?)\\]");
//private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\[(.*?)\\]");
private static final Pattern VARIABLE_PATTERN = Pattern.compile("<([^>]+)>(.*?)</\\1>");
hohwille marked this conversation as resolved.
Show resolved Hide resolved

/**
* Tests the installation of {@link Mvn}
Expand Down Expand Up @@ -77,7 +78,7 @@ private void checkInstallation(IdeTestContext context) throws IOException {

Path settingsFile = context.getConfPath().resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_FILE);
assertThat(settingsFile).exists();
assertFileContent(settingsFile, List.of("testLogin", "testPassword"));
assertFileContent(settingsFile, List.of("${env.M2_REPO}", "repository", "testLogin", "testPassword"));
hohwille marked this conversation as resolved.
Show resolved Hide resolved

Path settingsSecurityFile = context.getConfPath().resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_SECURITY_FILE);
assertThat(settingsSecurityFile).exists();
Expand All @@ -88,8 +89,8 @@ private void assertFileContent(Path filePath, List<String> expectedValues) throw

String content = new String(Files.readAllBytes(filePath));
Matcher matcher = VARIABLE_PATTERN.matcher(content);
List<String> values = matcher.results().map(matchResult -> matchResult.group(1)).collect(Collectors.toList());
List<String> values = matcher.results().map(matchResult -> matchResult.group(2)).collect(Collectors.toList());

assertThat(values).containsExactlyElementsOf(expectedValues);
assertThat(values).containsExactlyInAnyOrderElementsOf(expectedValues);
}
}