Skip to content

Commit

Permalink
Merge pull request #6 from astroilov/master
Browse files Browse the repository at this point in the history
Special-case trailing slash in path prefix.
  • Loading branch information
andreystroilov committed Jun 26, 2015
2 parents 0d8424e + 9a9ef49 commit ad84733
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public abstract class AbstractUpload
* build's outcome
* @param pathPrefix Path prefix to strip from uploaded files when determining
* the filename in GCS. Null indicates no stripping. Filenames that do not
* start with this prefix will not be modified.
* start with this prefix will not be modified. Trailing slash is
* automatically added if it is missing.
*/
public AbstractUpload(String bucket, boolean sharedPublicly,
boolean forFailedJobs, @Nullable String pathPrefix,
Expand All @@ -126,6 +127,9 @@ public AbstractUpload(String bucket, boolean sharedPublicly,
this.bucketNameWithVars = checkNotNull(bucket);
this.sharedPublicly = sharedPublicly;
this.forFailedJobs = forFailedJobs;
if (pathPrefix != null && !pathPrefix.endsWith("/")) {
pathPrefix += "/";
}
this.pathPrefix = pathPrefix;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<p>
The specified prefix will be stripped from all uploaded filenames. Filenames that do not start with this prefix will not be modified.
The specified prefix will be stripped from all uploaded filenames. Filenames that do not start with this prefix will not be modified. If this prefix does not have a trailing slash, it will be added automatically.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void testKeepPathPrefix() throws Exception {
}

@Test
public void testStripPathPrefix() throws Exception {
public void testStripPathPrefixWithCorrectPrefix() throws Exception {
final boolean sharedPublicly = false;
final boolean forFailedJobs = true;
final String pathPrefix = STRIP_PREFIX;
Expand All @@ -330,6 +330,83 @@ public void testStripPathPrefix() throws Exception {
underTest.perform(credentials, build, TaskListener.NULL);
}

@Test
public void testStripPathPrefixWithWrongPrefix() throws Exception {
final boolean sharedPublicly = false;
final boolean forFailedJobs = true;
final String pathPrefix = WRONG_PREFIX;

final AbstractUpload.UploadSpec uploads =
new AbstractUpload.UploadSpec(workspace,
ImmutableList.of(workspaceSubdirFile));

FakeUpload underTest = new FakeUpload(BUCKET_URI,
sharedPublicly, forFailedJobs, pathPrefix,
new MockUploadModule(executor),
FAKE_DETAILS,
uploads);

executor.throwWhen(Storage.Buckets.Get.class, notFoundException);
executor.passThruWhen(Storage.Buckets.Insert.class,
checkBucketName(BUCKET_NAME));
executor.passThruWhen(Storage.Objects.Insert.class,
checkObjectName(SUBDIR_FILENAME)); // full, non-stripped filename

underTest.perform(credentials, build, TaskListener.NULL);
}

@Test
public void testStripPathPrefixNoTrailingSlash() throws Exception {
final boolean sharedPublicly = false;
final boolean forFailedJobs = true;
final String pathPrefix = STRIP_PREFIX_NO_SLASH;

final AbstractUpload.UploadSpec uploads =
new AbstractUpload.UploadSpec(workspace,
ImmutableList.of(workspaceSubdirFile));

FakeUpload underTest = new FakeUpload(BUCKET_URI,
sharedPublicly, forFailedJobs, pathPrefix,
new MockUploadModule(executor),
FAKE_DETAILS,
uploads);

executor.throwWhen(Storage.Buckets.Get.class, notFoundException);
executor.passThruWhen(Storage.Buckets.Insert.class,
checkBucketName(BUCKET_NAME));
executor.passThruWhen(Storage.Objects.Insert.class,
checkObjectName(PREFIX_STRIPPED_FILENAME));

underTest.perform(credentials, build, TaskListener.NULL);
}

@Test
public void testStripPathPrefixWithNonDirectoryPrefix() throws Exception {
final boolean sharedPublicly = false;
final boolean forFailedJobs = true;
// This string is a prefix of the input file string,
// but is not at a directory boundary; it should not be stripped.
final String pathPrefix = STRIP_PREFIX_MALFORMED;

final AbstractUpload.UploadSpec uploads =
new AbstractUpload.UploadSpec(workspace,
ImmutableList.of(workspaceSubdirFile));

FakeUpload underTest = new FakeUpload(BUCKET_URI,
sharedPublicly, forFailedJobs, pathPrefix,
new MockUploadModule(executor),
FAKE_DETAILS,
uploads);

executor.throwWhen(Storage.Buckets.Get.class, notFoundException);
executor.passThruWhen(Storage.Buckets.Insert.class,
checkBucketName(BUCKET_NAME));
executor.passThruWhen(Storage.Objects.Insert.class,
checkObjectName(SUBDIR_FILENAME)); // full, non-stripped filename

underTest.perform(credentials, build, TaskListener.NULL);
}

@Test
public void testOnePartPrefix() throws Exception {
final boolean sharedPublicly = false;
Expand Down Expand Up @@ -718,10 +795,12 @@ private File makeTempDir(String name) throws IOException {
private static final String FILENAME = "bar.baz";
private static final String SUBDIR_PREFIX = "foo/bar";
private static final String SUBDIR_FILENAME = "foo/bar/bar.baz";
private static final String WRONG_PREFIX = "qqq/";
private static final String STRIP_PREFIX = "foo/";
private static final String STRIP_PREFIX_NO_SLASH = "foo";
private static final String STRIP_PREFIX_MALFORMED = "foo/ba";
private static final String PREFIX_STRIPPED_FILENAME = "bar/bar.baz";
private static final String FAKE_DETAILS = "These are my fake details";

private static final String FIRST_NAME = "foo";
private static final String SECOND_NAME = "bar";
}

0 comments on commit ad84733

Please sign in to comment.