Skip to content

Commit

Permalink
added testing cases and divided converting window file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Mar 27, 2024
1 parent 670835d commit 3b3dd8e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
40 changes: 22 additions & 18 deletions src/main/java/net/snowflake/client/config/SFClientConfigParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,7 @@ public static String getConfigFilePathFromJDBCJarLocation() {

if (systemGetProperty("os.name") != null
&& systemGetProperty("os.name").toLowerCase().startsWith("windows")) {

// Find the Windows file path pattern: ex) C:\ or D:\
Pattern windowsFilePattern = Pattern.compile("[C-Z]:[\\\\/]");
Matcher matcher = windowsFilePattern.matcher(updatedPath);
String prefix = "";

// Path translation for windows
if (updatedPath.startsWith("/")) {
updatedPath = updatedPath.substring(1);
} else if (updatedPath.startsWith("file:\\")) {
updatedPath = updatedPath.substring(6);
} else if (updatedPath.startsWith("\\")) {
updatedPath = updatedPath.substring(2);
} else if (matcher.find() && matcher.start() != 0) {
prefix = updatedPath.substring(0, matcher.start());
updatedPath = updatedPath.substring(matcher.start());
}
updatedPath = prefix + updatedPath.replace("/", "\\");
updatedPath = convertToWindowsPath(updatedPath);
}
return updatedPath;
}
Expand All @@ -115,4 +98,25 @@ && systemGetProperty("os.name").toLowerCase().startsWith("windows")) {
return "";
}
}

public static String convertToWindowsPath(String filePath) {
// Find the Windows file path pattern: ex) C:\ or D:\
Pattern windowsFilePattern = Pattern.compile("[C-Z]:[\\\\/]");
Matcher matcher = windowsFilePattern.matcher(filePath);
String prefix = "";

// Path translation for windows
if (filePath.startsWith("/")) {
filePath = filePath.substring(1);
} else if (filePath.startsWith("file:\\")) {
filePath = filePath.substring(6);
} else if (filePath.startsWith("\\")) {
filePath = filePath.substring(2);
} else if (matcher.find() && matcher.start() != 0) {
prefix = filePath.substring(0, matcher.start());
filePath = filePath.substring(matcher.start());
}
filePath = prefix + filePath.replace("/", "\\");
return filePath;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package net.snowflake.client.config;

import static net.snowflake.client.config.SFClientConfigParser.SF_CLIENT_CONFIG_ENV_NAME;
import static net.snowflake.client.config.SFClientConfigParser.SF_CLIENT_CONFIG_FILE_NAME;
import static net.snowflake.client.config.SFClientConfigParser.getConfigFilePathFromJDBCJarLocation;
import static net.snowflake.client.jdbc.SnowflakeUtil.systemGetProperty;
import static net.snowflake.client.jdbc.SnowflakeUtil.systemSetEnv;
import static net.snowflake.client.jdbc.SnowflakeUtil.systemUnsetEnv;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static net.snowflake.client.config.SFClientConfigParser.*;
import static net.snowflake.client.jdbc.SnowflakeUtil.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mockStatic;

import java.io.IOException;
Expand Down Expand Up @@ -135,11 +128,18 @@ public void testgetConfigFileNameFromJDBCJarLocation() {
}

@Test
public void testgetConfigFileNameFromJDBCJarLocationForWindows() {
try (MockedStatic<SnowflakeUtil> mockedSnowflakeUtil = mockStatic(SnowflakeUtil.class)) {
mockedSnowflakeUtil.when(() -> systemGetProperty("os.name")).thenReturn("windows");
String jdbcDirectoryPath = getConfigFilePathFromJDBCJarLocation();
assertFalse(jdbcDirectoryPath.contains("/")); // windows use \ in paths
public void testconvertToWindowsPath() {
String mockWindowsPath = "C:/Program Files/example.txt";
String resultWindowsPath = "C:\\Program Files\\example.txt";
String[] testCases = new String[] {"", "file:\\", "\\\\", "/"};
String mockCloudPrefix = "cloud://";

for (String testcase : testCases) {
assertEquals(resultWindowsPath, convertToWindowsPath(testcase + mockWindowsPath));
}

assertEquals(
mockCloudPrefix + resultWindowsPath,
convertToWindowsPath(mockCloudPrefix + mockWindowsPath));
}
}

0 comments on commit 3b3dd8e

Please sign in to comment.