Skip to content

Commit

Permalink
chore(test remove junit pioneer): do not use junit pioneer in unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sun Seng David TAN <[email protected]>
  • Loading branch information
sunix committed Dec 20, 2022
1 parent 0428b70 commit 9d5c905
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
4 changes: 0 additions & 4 deletions kubernetes-client-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,6 @@
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
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 @@ -868,30 +869,34 @@ private static boolean tryNamespaceFromPath(Config config) {
}

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

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

protected static String getHomeDir(Predicate<String> directoryExists) {
String home = System.getenv("HOME");
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;
if (directoryExists.test(homeDir)) {
return homeDir;
}
}
String userProfile = System.getenv("USERPROFILE");
String userProfile = getEnvVar.apply("USERPROFILE");
if (userProfile != null && !userProfile.isEmpty() && directoryExists.test(userProfile)) {
return userProfile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
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;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -674,51 +671,70 @@ private String getTestPathValue(File commandFolder) {
}

@Test
@SetSystemProperty(key = "os.name", value = "Windows")
@SetEnvironmentVariable(key = "HOMEDRIVE", value = "C:\\Users\\")
@SetEnvironmentVariable(key = "HOMEPATH", value = "user")
@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));

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));

}

@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));

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));

}

@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));

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));

}

@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));

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
@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));

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

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

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

}

}
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@

<!-- Testing versions -->
<junit.version>5.9.1</junit.version>
<junit-pioneer.version>1.7.0</junit-pioneer.version>
<assertj.core.version>3.23.1</assertj.core.version>
<awaitility.version>4.2.0</awaitility.version>
<approvaltests.version>18.5.0</approvaltests.version>
Expand Down Expand Up @@ -815,13 +814,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>${junit-pioneer.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down

0 comments on commit 9d5c905

Please sign in to comment.