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

Fix home dir win #4198

Merged
merged 3 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down Expand Up @@ -867,34 +869,40 @@ private static boolean tryNamespaceFromPath(Config config) {
}

private static String getHomeDir() {
return getHomeDir(Config::isDirectoryAndExists, Config::getSystemEnvVariable);
}

private static boolean isDirectoryAndExists(String filePath) {
File f = new File(filePath);
return f.exists() && f.isDirectory();
}

private static String getSystemEnvVariable(String envVariableName) {
return System.getenv(envVariableName);
}

protected static String getHomeDir(Predicate<String> directoryExists, UnaryOperator<String> getEnvVar) {
String home = getEnvVar.apply("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");
String homePath = System.getenv("HOMEPATH");
String homeDrive = getEnvVar.apply("HOMEDRIVE");
String homePath = getEnvVar.apply("HOMEPATH");
if (homeDrive != null && !homeDrive.isEmpty() && homePath != null && !homePath.isEmpty()) {
String homeDir = homeDrive + homePath;
File f = new File(homeDir);
if (f.exists() && f.isDirectory()) {
if (directoryExists.test(homeDir)) {
return homeDir;
}
}
String userProfile = System.getenv("USERPROFILE");
if (userProfile != null && !userProfile.isEmpty()) {
File f = new File(userProfile);
if (f.exists() && f.isDirectory()) {
return userProfile;
}
}
}
String home = System.getenv("HOME");
if (home != null && !home.isEmpty()) {
File f = new File(home);
if (f.exists() && f.isDirectory()) {
return home;
String userProfile = getEnvVar.apply("USERPROFILE");
if (userProfile != null && !userProfile.isEmpty() && directoryExists.test(userProfile)) {
return userProfile;
}
}

//Fall back to user.home should never really get here
// 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 java.io.File;
import java.io.IOException;
Expand All @@ -46,6 +47,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 @@ -667,4 +669,91 @@ private String getTestPathValue(File commandFolder) {
"/opt/apache-maven/bin";
}
}

@Test
void getHomeDir_shouldUseHomedriveHomepathOnWindows_WhenHomeEnvVariableIsNotSet() {
String osNamePropToRestore = System.getProperty("os.name");
try {

System.setProperty("os.name", "Windows");

Map<String, String> envVars = new HashMap<String, String>();
envVars.put("HOMEDRIVE", "C:\\Users\\");
envVars.put("HOMEPATH", "user");
envVars.put("USERPROFILE", "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\");

assertEquals("C:\\Users\\user", Config.getHomeDir(f -> true, envVars::get));

} finally {
System.setProperty("os.name", osNamePropToRestore);
}
}

@Test
void getHomeDir_shouldUseUserprofileOnWindows_WhenHomeHomedriveHomepathEnvVariablesAreNotSet() {
String osNamePropToRestore = System.getProperty("os.name");
try {

System.setProperty("os.name", "Windows");

Map<String, String> envVars = new HashMap<String, String>();
envVars.put("USERPROFILE", "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\");

assertEquals("C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\",
Config.getHomeDir(f -> true, envVars::get));

} finally {
System.setProperty("os.name", osNamePropToRestore);
}
}

@Test
void getHomeDir_shouldUseHomeEnvVariableOnWindows_WhenHomeEnvVariableIsSet() {
String osNamePropToRestore = System.getProperty("os.name");
try {

System.setProperty("os.name", "Windows");

Map<String, String> envVars = new HashMap<String, String>();
envVars.put("HOMEDRIVE", "C:\\Users\\");
envVars.put("HOMEPATH", "user");
envVars.put("HOME", "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\");

assertEquals("C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\",
Config.getHomeDir(f -> true, envVars::get));

} finally {
System.setProperty("os.name", osNamePropToRestore);
}
}

@Test
@EnabledOnOs({ WINDOWS })
void getHomeDir_shouldUseHomeEnvVariable_WhenEnabledOnWindows_WhenHomeEnvVariableIsSet() {

Map<String, String> envVars = new HashMap<String, String>();
envVars.put("HOMEDRIVE", "C:\\Users\\");
envVars.put("HOMEPATH", "user");
envVars.put("HOME", "C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\");

assertEquals("C:\\Users\\user\\workspace\\myworkspace\\tools\\cygwin\\",
Config.getHomeDir(f -> true, envVars::get));

}

@Test
void getHomeDir_shouldReturnUserHomeProp_WhenHomeEnvVariablesAreNotSet() {
String userHomePropToRestore = System.getProperty("user.home");
try {

System.setProperty("user.home", "/home/user");

Map<String, String> emptyEnvVars = Collections.emptyMap();

assertEquals("/home/user", Config.getHomeDir(f -> true, emptyEnvVars::get));

} finally {
System.setProperty("user.home", userHomePropToRestore);
}
}
}