Skip to content

Commit b4fbde4

Browse files
authored
devonfw#1318: added getPath method to get variable as Path + test (devonfw#1329)
1 parent 52e10fb commit b4fbde4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

ide/src/main/java/com/devonfw/tools/ide/env/var/EnvironmentVariables.java

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.devonfw.tools.ide.env.var;
22

33
import java.nio.file.Path;
4+
import java.nio.file.Paths;
45
import java.util.Locale;
56

67
import com.devonfw.tools.ide.log.IdeLogger;
@@ -27,6 +28,20 @@ default String get(String name) {
2728
return value;
2829
}
2930

31+
/**
32+
* @param name the name of the environment variable to get.
33+
* @return the value of the variable with the given {@code name} as {@link Path}. Will be {@code null} if no such
34+
* variable is defined.
35+
*/
36+
default Path getPath(String name) {
37+
38+
String value = get(name);
39+
if (value == null) {
40+
return null;
41+
}
42+
return Paths.get(value);
43+
}
44+
3045
/**
3146
* @param name the name of the environment variable to get.
3247
* @return the value of the variable with the given {@code name} without {@link #getParent() inheritance from parent}.

ide/src/test/java/com/devonfw/tools/ide/context/IdeContextTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public void testBasicProjectEnvironment() {
6868
assertThat(context.env().getIdeHome().resolve("readme")).hasContent("this is the IDE_HOME directory");
6969
assertThat(context.env().getIdeRoot().resolve("readme")).hasContent("this is the IDE_ROOT directory");
7070
assertThat(context.env().getUserHome().resolve("readme")).hasContent("this is the users HOME directory");
71+
assertThat(context.env().getVariables().getPath("M2_REPO"))
72+
.isEqualTo(context.env().getUserHome().resolve(".m2/repository"));
7173
assertThat(context.env().getDownloadCache().resolve("readme")).hasContent("this is the download cache");
7274
assertThat(context.env().getDownloadMetadata().resolve("readme")).hasContent("this is the download metadata");
7375
assertThat(context.env().getToolRepository().resolve("readme")).hasContent("this is the tool repository");

0 commit comments

Comments
 (0)