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

Allow path options to use user specific paths #4852

Closed
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 @@ -14,6 +14,7 @@

package com.google.devtools.build.lib.util;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.Converter;
Expand Down Expand Up @@ -103,7 +104,11 @@ public static class PathFragmentConverter

@Override
public PathFragment convert(String input) {
return PathFragment.create(input);
String path = Preconditions.checkNotNull(input);
if (!path.isEmpty() && path.startsWith("~/")) {
path = path.replace("~", System.getProperty("user.home"));
}
return PathFragment.create(path);
}

@Override
Expand All @@ -123,6 +128,9 @@ public List<PathFragment> convert(String input) {
List<PathFragment> list = new ArrayList<>();
for (String piece : input.split(":")) {
if (!piece.isEmpty()) {
if (piece.startsWith("~/")) {
piece = piece.replace("~", System.getProperty("user.home"));
}
list.add(PathFragment.create(piece));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.Assert.fail;

import com.google.common.collect.Lists;
import com.google.devtools.build.lib.util.OptionsUtils.PathFragmentConverter;
import com.google.devtools.build.lib.util.OptionsUtils.PathFragmentListConverter;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.Option;
Expand Down Expand Up @@ -166,6 +167,10 @@ private List<PathFragment> convert(String input) throws Exception {
return new PathFragmentListConverter().convert(input);
}

private PathFragment convertOne(String input) throws Exception {
return new PathFragmentConverter().convert(input);
}

@Test
public void emptyStringYieldsEmptyList() throws Exception {
assertThat(convert("")).isEqualTo(list());
Expand All @@ -183,12 +188,23 @@ public void converterSkipsEmptyStrings() throws Exception {

@Test
public void multiplePaths() throws Exception {
assertThat(convert("foo:/bar/baz:.:/tmp/bang"))
assertThat(convert("~/foo:foo:/bar/baz:.:/tmp/bang"))
.containsExactly(
fragment("foo"), fragment("/bar/baz"), fragment("."), fragment("/tmp/bang"))
fragment(System.getProperty("user.home") + "/foo"),
fragment("foo"),
fragment("/bar/baz"),
fragment("."),
fragment("/tmp/bang"))
.inOrder();
}

@Test
public void singlePath() throws Exception {
assertThat(convertOne("foo")).isEqualTo(fragment("foo"));
assertThat(convertOne("foo/bar/baz")).isEqualTo(fragment("foo/bar/baz"));
assertThat(convertOne("~/foo")).isEqualTo(fragment(System.getProperty("user.home") + "/foo"));
}

@Test
public void valueisUnmodifiable() throws Exception {
try {
Expand Down