Skip to content

Commit

Permalink
fix(Config.getHomeDir): should behave like kubectl and use HOME env v…
Browse files Browse the repository at this point in the history
…ariable on windows if available

Signed-off-by: Sun Seng David TAN <[email protected]>
  • Loading branch information
sunix committed Dec 19, 2022
1 parent d50f30e commit 0428b70
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,10 @@ private static boolean isDirectoryAndExists(String filePath) {
}

protected static String getHomeDir(Predicate<String> directoryExists) {
String home = System.getenv("HOME");
if (home != null && !home.isEmpty() && directoryExists.test(home)) {
return home;
}
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if (osName.startsWith("win")) {
String homeDrive = System.getenv("HOMEDRIVE");
Expand All @@ -892,10 +896,6 @@ protected static String getHomeDir(Predicate<String> directoryExists) {
return userProfile;
}
}
String home = System.getenv("HOME");
if (home != null && !home.isEmpty() && directoryExists.test(home)) {
return home;
}

// Fall back to user.home should never really get here
return System.getProperty("user.home", ".");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junitpioneer.jupiter.ClearEnvironmentVariable;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.junitpioneer.jupiter.SetSystemProperty;
Expand All @@ -49,6 +50,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.condition.OS.WINDOWS;

public class ConfigTest {

Expand Down Expand Up @@ -675,7 +677,48 @@ private String getTestPathValue(File commandFolder) {
@SetSystemProperty(key = "os.name", value = "Windows")
@SetEnvironmentVariable(key = "HOMEDRIVE", value = "C:\\Users\\")
@SetEnvironmentVariable(key = "HOMEPATH", value = "user")
void getHomeDir_shouldUseHomedriveHomepathOnWindows() {
@SetEnvironmentVariable(key = "USERPROFILE", value = "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\")
@ClearEnvironmentVariable(key = "HOME")
void getHomeDir_shouldUseHomedriveHomepathOnWindows_WhenHomeEnvVariableIsNotSet() {
assertEquals("C:\\Users\\user", Config.getHomeDir(f -> true));
}

@Test
@SetSystemProperty(key = "os.name", value = "Windows")
@ClearEnvironmentVariable(key = "HOMEDRIVE")
@ClearEnvironmentVariable(key = "HOMEPATH")
@SetEnvironmentVariable(key = "USERPROFILE", value = "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\")
@ClearEnvironmentVariable(key = "HOME")
void getHomeDir_shouldUseUserprofileOnWindows_WhenHomeHomedriveHomepathEnvVariablesAreNotSet() {
assertEquals("C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\", Config.getHomeDir(f -> true));
}

@Test
@SetSystemProperty(key = "os.name", value = "Windows")
@SetEnvironmentVariable(key = "HOMEDRIVE", value = "C:\\Users\\")
@SetEnvironmentVariable(key = "HOMEPATH", value = "user")
@SetEnvironmentVariable(key = "HOME", value = "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\")
void getHomeDir_shouldUseHomeEnvVariableOnWindows_WhenHomeEnvVariableIsSet() {
assertEquals("C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\", Config.getHomeDir(f -> true));
}

@Test
@EnabledOnOs({ WINDOWS })
@SetEnvironmentVariable(key = "HOMEDRIVE", value = "C:\\Users\\")
@SetEnvironmentVariable(key = "HOMEPATH", value = "user")
@SetEnvironmentVariable(key = "HOME", value = "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\")
void getHomeDir_shouldUseHomeEnvVariable_WhenEnabledOnWindows_WhenHomeEnvVariableIsSet() {
assertEquals("C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\", Config.getHomeDir(f -> true));
}

@Test
@SetSystemProperty(key = "user.home", value = "/home/user")
@ClearEnvironmentVariable(key = "HOMEDRIVE")
@ClearEnvironmentVariable(key = "HOMEPATH")
@ClearEnvironmentVariable(key = "HOME")
@ClearEnvironmentVariable(key = "USERPROFILE")
void getHomeDir_shouldReturnUserHomeProp_WhenHomeEnvVariablesAreNotSet() {
assertEquals("/home/user", Config.getHomeDir(f -> true));
}

}

0 comments on commit 0428b70

Please sign in to comment.