Skip to content

Commit

Permalink
Correctly propagate the source kind to zip entries.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=241408949
  • Loading branch information
tjgq authored and blickly committed Apr 3, 2019
1 parent f64cd02 commit 60fc1c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
21 changes: 17 additions & 4 deletions src/com/google/javascript/jscomp/SourceFile.java
Expand Up @@ -360,13 +360,13 @@ private static boolean isZipEntry(String path) {
}

@GwtIncompatible("java.io.File")
private static SourceFile fromZipEntry(String zipURL, Charset inputCharset) {
private static SourceFile fromZipEntry(String zipURL, Charset inputCharset, SourceKind kind) {
checkArgument(isZipEntry(zipURL));
String[] components = zipURL.split(Pattern.quote(BANG_SLASH.replace("/", File.separator)));
try {
String zipPath = components[0];
String relativePath = components[1];
return fromZipEntry(zipPath, zipPath, relativePath, inputCharset);
return fromZipEntry(zipPath, zipPath, relativePath, inputCharset, kind);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Expand All @@ -376,8 +376,21 @@ private static SourceFile fromZipEntry(String zipURL, Charset inputCharset) {
public static SourceFile fromZipEntry(
String originalZipPath, String absoluteZipPath, String entryPath, Charset inputCharset)
throws MalformedURLException {
return fromZipEntry(
originalZipPath, absoluteZipPath, entryPath, inputCharset, SourceKind.STRONG);
}

@GwtIncompatible("java.io.File")
public static SourceFile fromZipEntry(
String originalZipPath,
String absoluteZipPath,
String entryPath,
Charset inputCharset,
SourceKind kind)
throws MalformedURLException {
// No longer throws MalformedURLException but we are keeping it for backward compatbility.
return builder()
.withKind(kind)
.withCharset(inputCharset)
.withOriginalPath(originalZipPath + BANG_SLASH + entryPath)
.buildFromZipEntry(
Expand Down Expand Up @@ -489,7 +502,7 @@ public SourceFile buildFromPath(Path path) {
checkNotNull(path);
checkNotNull(charset);
if (isZipEntry(path.toString())) {
return fromZipEntry(path.toString(), charset);
return fromZipEntry(path.toString(), charset, kind);
}
return new OnDisk(path, originalPath, charset, kind);
}
Expand Down Expand Up @@ -658,7 +671,7 @@ private static class AtZip extends SourceFile {
private transient Charset inputCharset;

AtZip(ZipEntryReader zipEntryReader, String originalPath, Charset c, SourceKind kind) {
super(originalPath, SourceKind.STRONG);
super(originalPath, kind);
this.inputCharset = c;
this.zipEntryReader = zipEntryReader;
setOriginalPath(originalPath);
Expand Down
16 changes: 14 additions & 2 deletions test/com/google/javascript/jscomp/SourceFileTest.java
Expand Up @@ -135,14 +135,26 @@ public void testSourceFileResolvesZipEntries() throws IOException {
Path jsZipPath = Files.createTempFile("test", ".js.zip");
createZipWithContent(jsZipPath, expectedContent);

// Test SourceFile#fromZipEntry(String, String, String, Charset)
// Test SourceFile#fromZipEntry(String, String, String, Charset, SourceKind)
SourceFile sourceFileFromZipEntry =
SourceFile.fromZipEntry(
jsZipPath.toString(),
jsZipPath.toAbsolutePath().toString(),
"foo.js",
StandardCharsets.UTF_8);
StandardCharsets.UTF_8,
SourceKind.WEAK);
assertThat(sourceFileFromZipEntry.getCode()).isEqualTo(expectedContent);
assertThat(sourceFileFromZipEntry.getKind()).isEqualTo(SourceKind.WEAK);

// Test SourceFile#fromZipEntry(String, String, String, Charset)
SourceFile sourceFileFromZipEntryDefaultKind =
SourceFile.fromZipEntry(
jsZipPath.toString(),
jsZipPath.toAbsolutePath().toString(),
"foo.js",
StandardCharsets.UTF_8);
assertThat(sourceFileFromZipEntryDefaultKind.getCode()).isEqualTo(expectedContent);
assertThat(sourceFileFromZipEntryDefaultKind.getKind()).isEqualTo(SourceKind.STRONG);

// Test SourceFile#fromFile(String)
SourceFile sourceFileFromFileString =
Expand Down

0 comments on commit 60fc1c7

Please sign in to comment.