replace ~ by @{userhome} on unix systems

This commit is contained in:
Armin Schrenk
2023-06-20 16:37:32 +02:00
parent 2c0474ec46
commit ec645a4bb9
11 changed files with 38 additions and 119 deletions

View File

@@ -18,7 +18,6 @@ import java.util.stream.StreamSupport;
public class Environment {
private static final Logger LOG = LoggerFactory.getLogger(Environment.class);
private static final Path RELATIVE_HOME_DIR = Paths.get("~");
private static final char PATH_LIST_SEP = ':';
private static final int DEFAULT_MIN_PW_LENGTH = 8;
private static final String SETTINGS_PATH_PROP_NAME = "cryptomator.settingsPath";
@@ -89,7 +88,7 @@ public class Environment {
}
public Optional<Path> getLogDir() {
return getPath(LOG_DIR_PROP_NAME).map(this::replaceHomeDir);
return getPath(LOG_DIR_PROP_NAME);
}
public Optional<String> getLoopbackAlias() {
@@ -97,11 +96,11 @@ public class Environment {
}
public Optional<Path> getPluginDir() {
return getPath(PLUGIN_DIR_PROP_NAME).map(this::replaceHomeDir);
return getPath(PLUGIN_DIR_PROP_NAME);
}
public Optional<Path> getMountPointsDir() {
return getPath(MOUNTPOINT_DIR_PROP_NAME).map(this::replaceHomeDir);
return getPath(MOUNTPOINT_DIR_PROP_NAME);
}
/**
@@ -130,23 +129,10 @@ public class Environment {
return Optional.ofNullable(value).map(Paths::get);
}
// visible for testing
public Path getHomeDir() {
return getPath("user.home").orElseThrow();
}
// visible for testing
public Stream<Path> getPaths(String propertyName) {
Stream<String> rawSettingsPaths = getRawList(propertyName, PATH_LIST_SEP);
return rawSettingsPaths.filter(Predicate.not(Strings::isNullOrEmpty)).map(Paths::get).map(this::replaceHomeDir);
}
private Path replaceHomeDir(Path path) {
if (path.startsWith(RELATIVE_HOME_DIR)) {
return getHomeDir().resolve(RELATIVE_HOME_DIR.relativize(path));
} else {
return path;
}
return rawSettingsPaths.filter(Predicate.not(Strings::isNullOrEmpty)).map(Path::of);
}
private Stream<String> getRawList(String propertyName, char separator) {

View File

@@ -22,41 +22,7 @@ public class EnvironmentTest {
@BeforeEach
public void init() {
env = Mockito.spy(Environment.getInstance());
Mockito.when(env.getHomeDir()).thenReturn(Path.of("/home/testuser"));
}
@Test
@DisplayName("cryptomator.settingsPath=~/.config/Cryptomator/settings.json:~/.Cryptomator/settings.json")
public void testSettingsPath() {
System.setProperty("cryptomator.settingsPath", "~/.config/Cryptomator/settings.json:~/.Cryptomator/settings.json");
List<Path> result = env.getSettingsPath().toList();
MatcherAssert.assertThat(result, Matchers.hasSize(2));
MatcherAssert.assertThat(result, Matchers.contains(Paths.get("/home/testuser/.config/Cryptomator/settings.json"), //
Paths.get("/home/testuser/.Cryptomator/settings.json")));
}
@Test
@DisplayName("cryptomator.ipcSocketPath=~/.config/Cryptomator/ipc.socket:~/.Cryptomator/ipc.socket")
public void testIpcSocketPath() {
System.setProperty("cryptomator.ipcSocketPath", "~/.config/Cryptomator/ipc.socket:~/.Cryptomator/ipc.socket");
List<Path> result = env.ipcSocketPath().toList();
MatcherAssert.assertThat(result, Matchers.hasSize(2));
MatcherAssert.assertThat(result, Matchers.contains(Paths.get("/home/testuser/.config/Cryptomator/ipc.socket"), //
Paths.get("/home/testuser/.Cryptomator/ipc.socket")));
}
@Test
@DisplayName("cryptomator.integrationsWin.keychainPaths=~/AppData/Roaming/Cryptomator/keychain.json")
public void testKeychainPath() {
System.setProperty("cryptomator.integrationsWin.keychainPaths", "~/AppData/Roaming/Cryptomator/keychain.json");
List<Path> result = env.getKeychainPath().toList();
MatcherAssert.assertThat(result, Matchers.hasSize(1));
MatcherAssert.assertThat(result, Matchers.contains(Paths.get("/home/testuser/AppData/Roaming/Cryptomator/keychain.json")));
}
@Test
@DisplayName("cryptomator.logDir=/foo/bar")
public void testAbsoluteLogDir() {
@@ -67,17 +33,6 @@ public class EnvironmentTest {
Assertions.assertTrue(logDir.isPresent());
}
@Test
@DisplayName("cryptomator.logDir=~/foo/bar")
public void testRelativeLogDir() {
System.setProperty("cryptomator.logDir", "~/foo/bar");
Optional<Path> logDir = env.getLogDir();
Assertions.assertTrue(logDir.isPresent());
Assertions.assertEquals(Paths.get("/home/testuser/foo/bar"), logDir.get());
}
@Nested
@DisplayName("Path Lists")
public class SettingsPath {
@@ -101,28 +56,6 @@ public class EnvironmentTest {
MatcherAssert.assertThat(result, Matchers.hasItem(Paths.get("/foo/bar/test")));
}
@Test
@DisplayName("test.path.property=~/test")
public void testSingleHomeRelativePath() {
System.setProperty("test.path.property", "~/test");
List<Path> result = env.getPaths("test.path.property").toList();
MatcherAssert.assertThat(result, Matchers.hasSize(1));
MatcherAssert.assertThat(result, Matchers.hasItem(Paths.get("/home/testuser/test")));
}
@Test
@DisplayName("test.path.property=~/test:~/test2:/foo/bar/test")
public void testMultiplePaths() {
System.setProperty("test.path.property", "~/test:~/test2:/foo/bar/test");
List<Path> result = env.getPaths("test.path.property").toList();
MatcherAssert.assertThat(result, Matchers.hasSize(3));
MatcherAssert.assertThat(result, Matchers.contains(Paths.get("/home/testuser/test"), //
Paths.get("/home/testuser/test2"), //
Paths.get("/foo/bar/test")));
}
}
}