From a9b336e3f1ef089951e02e469f164d15ae939315 Mon Sep 17 00:00:00 2001 From: cgdecker Date: Mon, 27 Apr 2020 14:48:44 -0700 Subject: Fix JimfsPath.resolve(Name) to work correctly when the path is the empty path. Unlike JimfsPath.resolve(String) or resolve(Path), it was incorrectly including its (empty string) name in the resolved path. This only showed up when iterating the files in the working directory via its DirectoryStream; the Paths you would get would look like "/foo.txt" instead of just "foo.txt". Fixes https://github.com/google/jimfs/issues/105 RELNOTES=Fixed an issue where incorrect `Path`s were returned from a `DirectoryStream` for the working directory (i.e. the directory `fs.getPath("")` returns). See #105. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=308702918 --- jimfs/src/main/java/com/google/common/jimfs/JimfsPath.java | 6 +----- .../src/test/java/com/google/common/jimfs/JimfsPathTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/jimfs/src/main/java/com/google/common/jimfs/JimfsPath.java b/jimfs/src/main/java/com/google/common/jimfs/JimfsPath.java index 7c6b115..9d18837 100644 --- a/jimfs/src/main/java/com/google/common/jimfs/JimfsPath.java +++ b/jimfs/src/main/java/com/google/common/jimfs/JimfsPath.java @@ -251,11 +251,7 @@ final class JimfsPath implements Path { /** Resolves the given name against this path. The name is assumed not to be a root name. */ JimfsPath resolve(Name name) { - if (name.toString().isEmpty()) { - return this; - } - return pathService.createPathInternal( - root, ImmutableList.builder().addAll(names).add(name).build()); + return resolve(pathService.createFileName(name)); } @Override diff --git a/jimfs/src/test/java/com/google/common/jimfs/JimfsPathTest.java b/jimfs/src/test/java/com/google/common/jimfs/JimfsPathTest.java index da7d43c..5b59081 100644 --- a/jimfs/src/test/java/com/google/common/jimfs/JimfsPathTest.java +++ b/jimfs/src/test/java/com/google/common/jimfs/JimfsPathTest.java @@ -218,6 +218,17 @@ public class JimfsPathTest { assertResolvedPathEquals("foo/bar", pathService.emptyPath(), "foo/bar"); } + @Test + public void testResolveName_againstEmptyPath() { + // resolve(Name) is only used in the DirectoryStream implementation, so it's only used to + // resolve the names of real existing files against some base directory's path. The base + // directory path could be the working directory path (i.e. just an empty string), in which case + // we need to be sure to return a path that is just the name of the file as opposed a path with + // two names, one being the empty string and the other the file name). + // See https://github.com/google/jimfs/issues/105 + assertPathEquals("foo", pathService.emptyPath().resolve(Name.simple("foo"))); + } + @Test public void testResolveSibling_givenEmptyPath() { Path path = pathService.parsePath("foo/bar"); -- cgit v1.2.3