Skip to content

Commit

Permalink
devonfw#1024: added tests of WindowsHelperImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-vcapgemini committed Feb 17, 2025
1 parent 075a79c commit 31d8719
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ static WindowsHelper get(IdeContext context) {
// IdeContext API is already too large
return ((AbstractIdeContext) context).getWindowsHelper();
}

}
11 changes: 11 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/os/WindowsHelperImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public String getRegistryValue(String path, String key) {

ProcessResult result = this.context.newProcess().executable("reg").addArgs("query", path, "/v", key).run(ProcessMode.DEFAULT_CAPTURE);
List<String> out = result.getOut();
return retrieveRegString(key, out);
}

/**
* Parses the result of a registry query and outputs the given key.
*
* @param key the key to look for.
* @param out List of keys from registry query result.
* @return the registry value.
*/
protected String retrieveRegString(String key, List<String> out) {
for (String line : out) {
int i = line.indexOf(key);
if (i >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,7 @@ protected Path getIdeRootPathFromEnv() {
return null;
}

public void setUserEnvironmentVariable(String userEnvironmentVariable) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.devonfw.tools.ide.os;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.AbstractIdeTestContext;
import com.devonfw.tools.ide.context.IdeSlf4jContext;

/**
* Tests for {@link WindowsHelperImpl}.
*/
public class WindowsHelperImplTest extends AbstractIdeContextTest {

/**
* Tests if the USER_PATH registry entry can be parsed properly.
*/
@Test
public void testWindowsHelperParseRegString() {
// arrange
AbstractIdeTestContext context = new IdeSlf4jContext();
WindowsHelperImpl helper = new WindowsHelperImpl(context);
List<String> output = new ArrayList<>();
output.add("");
output.add("HKEY_CURRENT_USER\\Environment");
output.add(" PATH REG_SZ D:\\projects\\_ide\\installation\\bin;");
output.add("");
// act
String regString = helper.retrieveRegString("PATH", output);
// assert
assertThat(regString).isEqualTo("D:\\projects\\_ide\\installation\\bin;");
}

/**
* Tests if an empty list of outputs will result in null.
*/
@Test
public void testWindowsHelperParseEmptyRegStringReturnsNull() {
// arrange
AbstractIdeTestContext context = new IdeSlf4jContext();
WindowsHelperImpl helper = new WindowsHelperImpl(context);
List<String> output = new ArrayList<>();
// act
String regString = helper.retrieveRegString("PATH", output);
// assert
assertThat(regString).isNull();
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# if this file is present, it will replace and mock System.getenv in the text context
PATH="/usr/bin:${IDE_ROOT}/_ide/bin"
#PATH="/usr/bin:${IDE_ROOT}/_ide/bin"
PATH=${IDE_ROOT}/_ide/bin

0 comments on commit 31d8719

Please sign in to comment.