From 985fc0ccea83fc8d90c2301ba56a9831e06dbd8e Mon Sep 17 00:00:00 2001 From: johnplaisted Date: Fri, 4 May 2018 15:14:07 -0700 Subject: [PATCH] Allow the transpiler to take URIs so that URLs may be used. Useful for servers that output sourcemaps to ensure the sourcemap points to a server file and not one on disc. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=195478973 --- .../javascript/jscomp/CommandLineRunner.java | 2 +- .../javascript/jscomp/CompilerOptions.java | 4 +- .../google/javascript/jscomp/SourceMap.java | 43 ++++-- .../javascript/jscomp/ant/CompileTask.java | 4 +- .../jscomp/deps/ClosureBundler.java | 10 +- .../jscomp/transpile/BaseTranspiler.java | 23 ++- .../jscomp/transpile/CachingTranspiler.java | 9 +- .../jscomp/transpile/TranspileResult.java | 8 +- .../jscomp/transpile/Transpiler.java | 34 ++--- .../jscomp/CommandLineRunnerTest.java | 9 +- .../javascript/jscomp/SourceMapTest.java | 142 ++++++++++++------ .../jscomp/deps/ClosureBundlerTest.java | 9 +- .../jscomp/transpile/BaseTranspilerTest.java | 17 ++- .../transpile/CachingTranspilerTest.java | 29 +++- .../jscomp/transpile/TranspileResultTest.java | 37 +++-- 15 files changed, 243 insertions(+), 137 deletions(-) diff --git a/src/com/google/javascript/jscomp/CommandLineRunner.java b/src/com/google/javascript/jscomp/CommandLineRunner.java index 0ad60d0a563..ba22fd8a290 100644 --- a/src/com/google/javascript/jscomp/CommandLineRunner.java +++ b/src/com/google/javascript/jscomp/CommandLineRunner.java @@ -1071,7 +1071,7 @@ List getSourceMapLocationMappings() throws CmdLineExc ImmutableMap split = splitPipeParts( sourceMapLocationMapping, "--source_map_location_mapping"); for (Map.Entry mapping : split.entrySet()) { - locationMappings.add(new SourceMap.LocationMapping(mapping.getKey(), + locationMappings.add(new SourceMap.PrefixLocationMapping(mapping.getKey(), mapping.getValue())); } diff --git a/src/com/google/javascript/jscomp/CompilerOptions.java b/src/com/google/javascript/jscomp/CompilerOptions.java index b3514062cc4..8a8370e1f47 100644 --- a/src/com/google/javascript/jscomp/CompilerOptions.java +++ b/src/com/google/javascript/jscomp/CompilerOptions.java @@ -1089,7 +1089,7 @@ public void setTracerOutput(PrintStream out) { */ boolean resolveSourceMapAnnotations = true; - public List sourceMapLocationMappings = + public List sourceMapLocationMappings = Collections.emptyList(); /** @@ -2665,7 +2665,7 @@ public void setSourceMapFormat(SourceMap.Format sourceMapFormat) { } public void setSourceMapLocationMappings( - List sourceMapLocationMappings) { + List sourceMapLocationMappings) { this.sourceMapLocationMappings = sourceMapLocationMappings; } diff --git a/src/com/google/javascript/jscomp/SourceMap.java b/src/com/google/javascript/jscomp/SourceMap.java index a278f047d24..1c23c4e7c86 100644 --- a/src/com/google/javascript/jscomp/SourceMap.java +++ b/src/com/google/javascript/jscomp/SourceMap.java @@ -95,27 +95,47 @@ public static enum DetailLevel implements Predicate { } /** - * A simple pair of path prefixes to the desired "destination" location to use within the source - * map. + * Function that mape a "destination" location to use within the source map. Should return null + * if the value is not mapped. */ - public static final class LocationMapping { + @FunctionalInterface + public interface LocationMapping { + /** + * @param location the location to transform + * @return the transformed location or null if not transformed + */ + @Nullable + String map(String location); + } + + /** + * Simple {@link LocationMapping} that strips a prefix from a location. + */ + public static final class PrefixLocationMapping implements LocationMapping { final String prefix; final String replacement; - public LocationMapping(String prefix, String replacement) { + public PrefixLocationMapping(String prefix, String replacement) { this.prefix = prefix; this.replacement = replacement; } @Override + public String map(String location) { + if (location.startsWith(prefix)) { + return replacement + location.substring(prefix.length()); + } + return null; + } + public String toString() { return "(" + prefix + "|" + replacement + ")"; } @Override public boolean equals(Object other) { - if (other instanceof LocationMapping) { - return ((LocationMapping) other).prefix.equals(prefix) - && ((LocationMapping) other).replacement.equals(replacement); + if (other instanceof PrefixLocationMapping) { + return ((PrefixLocationMapping) other).prefix.equals(prefix) + && ((PrefixLocationMapping) other).replacement.equals(replacement); } else { return false; } @@ -128,7 +148,7 @@ public int hashCode() { } private final SourceMapGenerator generator; - private List prefixMappings = Collections.emptyList(); + private List prefixMappings = Collections.emptyList(); private final Map sourceLocationFixupCache = new HashMap<>(); /** @@ -206,9 +226,8 @@ private String fixupSourceLocation(String sourceFile) { // Replace the first prefix found with its replacement for (LocationMapping mapping : prefixMappings) { - if (sourceFile.startsWith(mapping.prefix)) { - fixed = mapping.replacement + sourceFile.substring( - mapping.prefix.length()); + fixed = mapping.map(sourceFile); + if (fixed != null) { break; } } @@ -246,7 +265,7 @@ public void validate(boolean validate) { /** * @param sourceMapLocationMappings */ - public void setPrefixMappings(List sourceMapLocationMappings) { + public void setPrefixMappings(List sourceMapLocationMappings) { this.prefixMappings = sourceMapLocationMappings; } diff --git a/src/com/google/javascript/jscomp/ant/CompileTask.java b/src/com/google/javascript/jscomp/ant/CompileTask.java index aa1732f013a..d45d759b040 100644 --- a/src/com/google/javascript/jscomp/ant/CompileTask.java +++ b/src/com/google/javascript/jscomp/ant/CompileTask.java @@ -32,7 +32,7 @@ import com.google.javascript.jscomp.SourceFile; import com.google.javascript.jscomp.SourceMap; import com.google.javascript.jscomp.SourceMap.Format; -import com.google.javascript.jscomp.SourceMap.LocationMapping; +import com.google.javascript.jscomp.SourceMap.PrefixLocationMapping; import com.google.javascript.jscomp.WarningLevel; import java.io.File; import java.io.FileOutputStream; @@ -476,7 +476,7 @@ private CompilerOptions createCompilerOptions() { if (!Strings.isNullOrEmpty(sourceMapLocationMapping)) { String[] tokens = sourceMapLocationMapping.split("\\|", -1); - LocationMapping lm = new LocationMapping(tokens[0], tokens[1]); + PrefixLocationMapping lm = new PrefixLocationMapping(tokens[0], tokens[1]); options.setSourceMapLocationMappings(Arrays.asList(lm)); } diff --git a/src/com/google/javascript/jscomp/deps/ClosureBundler.java b/src/com/google/javascript/jscomp/deps/ClosureBundler.java index 477cd749591..c118fe98927 100644 --- a/src/com/google/javascript/jscomp/deps/ClosureBundler.java +++ b/src/com/google/javascript/jscomp/deps/ClosureBundler.java @@ -23,8 +23,9 @@ import com.google.javascript.jscomp.transpile.Transpiler; import java.io.File; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.charset.Charset; -import java.nio.file.Paths; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -135,7 +136,12 @@ public String getSourceMap(String path) { } private String transpile(String s, Transpiler t) { - TranspileResult result = t.transpile(Paths.get(path), s); + TranspileResult result; + try { + result = t.transpile(new URI(path), s); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } sourceMapCache.put(path, result.sourceMap()); return result.transpiled(); } diff --git a/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java b/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java index 9f47bbf62b4..1cbe4dfb461 100644 --- a/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java +++ b/src/com/google/javascript/jscomp/transpile/BaseTranspiler.java @@ -35,7 +35,8 @@ import com.google.javascript.jscomp.bundle.TranspilationException; import com.google.javascript.rhino.Node; import java.io.IOException; -import java.nio.file.Path; +import java.net.URI; +import java.net.URISyntaxException; /** * Basic Transpiler implementation for outputting ES5 code. @@ -51,7 +52,7 @@ public final class BaseTranspiler implements Transpiler { } @Override - public TranspileResult transpile(Path path, String code) { + public TranspileResult transpile(URI path, String code) { CompileResult result = compilerSupplier.compile(path, code); if (!result.transpiled) { return new TranspileResult(path, code, code, ""); @@ -84,7 +85,7 @@ public String runtime() { * time when we're in single-file mode. */ public static class CompilerSupplier { - public CompileResult compile(Path path, String code) { + public CompileResult compile(URI path, String code) { Compiler compiler = compiler(); Result result = compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options()); @@ -149,9 +150,19 @@ protected void setOptions(CompilerOptions options) { options.setSourceMapOutputPath("/dev/null"); options.setSourceMapIncludeSourcesContent(true); // Make sourcemaps use absolute paths, so that the path is not duplicated if a build tool adds - // a sourceurl. + // a sourceurl. Exception: if the location has a scheme (like http:) then leave the path + // intact. This makes this usable from web servers. options.setSourceMapLocationMappings( - ImmutableList.of(new SourceMap.LocationMapping("", "/"))); + ImmutableList.of((location) -> { + try { + if (new URI(location).getScheme() != null) { + return location; + } + } catch (URISyntaxException e) { + // Swallow, return the absolute version below. + } + return new SourceMap.PrefixLocationMapping("", "/").map(location); + })); } protected static final SourceFile EXTERNS = @@ -167,7 +178,7 @@ protected void setOptions(CompilerOptions options) { */ public static class EsmToCjsCompilerSupplier extends CompilerSupplier { @Override - public CompileResult compile(Path path, String code) { + public CompileResult compile(URI path, String code) { CompilerOptions options = new CompilerOptions(); options.setLanguageIn(LanguageMode.ECMASCRIPT_NEXT); options.setEmitUseStrict(false); diff --git a/src/com/google/javascript/jscomp/transpile/CachingTranspiler.java b/src/com/google/javascript/jscomp/transpile/CachingTranspiler.java index d64041db0bb..b0357a4a2a9 100644 --- a/src/com/google/javascript/jscomp/transpile/CachingTranspiler.java +++ b/src/com/google/javascript/jscomp/transpile/CachingTranspiler.java @@ -25,7 +25,7 @@ import com.google.common.cache.LoadingCache; import com.google.common.util.concurrent.UncheckedExecutionException; import com.google.javascript.jscomp.bundle.TranspilationException; -import java.nio.file.Path; +import java.net.URI; import java.util.Objects; /** @@ -51,7 +51,7 @@ public TranspileResult load(Key key) { } @Override - public TranspileResult transpile(Path path, String code) { + public TranspileResult transpile(URI path, String code) { try { return cache.getUnchecked(new Key(path, code)); } catch (UncheckedExecutionException e) { @@ -72,9 +72,10 @@ public String runtime() { } private static final class Key { - private final Path path; + private final URI path; private final String code; - Key(Path path, String code) { + + Key(URI path, String code) { this.path = checkNotNull(path); this.code = checkNotNull(code); } diff --git a/src/com/google/javascript/jscomp/transpile/TranspileResult.java b/src/com/google/javascript/jscomp/transpile/TranspileResult.java index 3899dbb5156..549b3868241 100644 --- a/src/com/google/javascript/jscomp/transpile/TranspileResult.java +++ b/src/com/google/javascript/jscomp/transpile/TranspileResult.java @@ -20,7 +20,7 @@ import com.google.common.escape.Escaper; import com.google.common.net.PercentEscaper; -import java.nio.file.Path; +import java.net.URI; import java.util.Objects; /** @@ -29,19 +29,19 @@ */ public final class TranspileResult { - private final Path path; + private final URI path; private final String original; private final String transpiled; private final String sourceMap; - public TranspileResult(Path path, String original, String transpiled, String sourceMap) { + public TranspileResult(URI path, String original, String transpiled, String sourceMap) { this.path = checkNotNull(path); this.original = checkNotNull(original); this.transpiled = checkNotNull(transpiled); this.sourceMap = checkNotNull(sourceMap); } - public Path path() { + public URI path() { return path; } diff --git a/src/com/google/javascript/jscomp/transpile/Transpiler.java b/src/com/google/javascript/jscomp/transpile/Transpiler.java index 5fe0fb61f7e..51586099c82 100644 --- a/src/com/google/javascript/jscomp/transpile/Transpiler.java +++ b/src/com/google/javascript/jscomp/transpile/Transpiler.java @@ -16,7 +16,7 @@ package com.google.javascript.jscomp.transpile; -import java.nio.file.Path; +import java.net.URI; /** * Common interface for a transpiler. @@ -39,11 +39,8 @@ * */ public interface Transpiler { - /** - * Transforms the given chunk of code. The input should be an entire file - * worth of code. - */ - TranspileResult transpile(Path path, String code); + /** Transforms the given chunk of code. The input should be an entire file worth of code. */ + TranspileResult transpile(URI path, String code); /** * Returns any necessary runtime code as a string. This should include @@ -52,18 +49,17 @@ public interface Transpiler { */ String runtime(); - /** - * Null implementation that does no transpilation at all. - */ - Transpiler NULL = new Transpiler() { - @Override - public TranspileResult transpile(Path path, String code) { - return new TranspileResult(path, code, code, ""); - } + /** Null implementation that does no transpilation at all. */ + Transpiler NULL = + new Transpiler() { + @Override + public TranspileResult transpile(URI path, String code) { + return new TranspileResult(path, code, code, ""); + } - @Override - public String runtime() { - return ""; - } - }; + @Override + public String runtime() { + return ""; + } + }; } diff --git a/test/com/google/javascript/jscomp/CommandLineRunnerTest.java b/test/com/google/javascript/jscomp/CommandLineRunnerTest.java index 2e3ec3f0473..b547930aa86 100644 --- a/test/com/google/javascript/jscomp/CommandLineRunnerTest.java +++ b/test/com/google/javascript/jscomp/CommandLineRunnerTest.java @@ -1033,9 +1033,9 @@ public void testSourceMapLocationsTranslations1() { args.add("--source_map_location_mapping=foo/|http://bar"); testSame("var x = 3;"); - List mappings = lastCompiler.getOptions().sourceMapLocationMappings; + List mappings = lastCompiler.getOptions().sourceMapLocationMappings; assertThat(ImmutableSet.copyOf(mappings)) - .containsExactly(new LocationMapping("foo/", "http://bar")); + .containsExactly(new SourceMap.PrefixLocationMapping("foo/", "http://bar")); } public void testSourceMapLocationsTranslations2() { @@ -1046,11 +1046,12 @@ public void testSourceMapLocationsTranslations2() { args.add("--source_map_location_mapping=xxx/|http://yyy"); testSame("var x = 3;"); - List mappings = lastCompiler.getOptions() + List mappings = lastCompiler.getOptions() .sourceMapLocationMappings; assertThat(ImmutableSet.copyOf(mappings)) .containsExactly( - new LocationMapping("foo/", "http://bar"), new LocationMapping("xxx/", "http://yyy")); + new SourceMap.PrefixLocationMapping("foo/", "http://bar"), + new SourceMap.PrefixLocationMapping("xxx/", "http://yyy")); } public void testSourceMapLocationsTranslations3() { diff --git a/test/com/google/javascript/jscomp/SourceMapTest.java b/test/com/google/javascript/jscomp/SourceMapTest.java index 3970e56ac78..fb4dc8258ea 100644 --- a/test/com/google/javascript/jscomp/SourceMapTest.java +++ b/test/com/google/javascript/jscomp/SourceMapTest.java @@ -16,6 +16,7 @@ package com.google.javascript.jscomp; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.debugging.sourcemap.SourceMapConsumer; import com.google.debugging.sourcemap.SourceMapConsumerV3; @@ -25,73 +26,117 @@ import java.io.IOException; import java.util.List; -/** - * - * @author johnlenz@google.com (John Lenz) - */ +/** @author johnlenz@google.com (John Lenz) */ public final class SourceMapTest extends SourceMapTestCase { - public SourceMapTest() { - } + public SourceMapTest() {} private List mappings; public void testPrefixReplacement1() throws IOException { // mapping can be used to remove a prefix - mappings = ImmutableList.of(new SourceMap.LocationMapping("pre/", "")); - checkSourceMap2("alert(1);", Compiler.joinPathParts("pre", "file1"), "alert(2);", - Compiler.joinPathParts("pre", "file2") , "{\n" + - "\"version\":3,\n" + - "\"file\":\"testcode\",\n" + - "\"lineCount\":1,\n" + - "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",\n" + - "\"sources\":[\"file1\",\"file2\"],\n" + - "\"names\":[\"alert\"]\n" + - "}\n"); + mappings = ImmutableList.of(new SourceMap.PrefixLocationMapping("pre/", "")); + checkSourceMap2( + "alert(1);", + Compiler.joinPathParts("pre", "file1"), + "alert(2);", + Compiler.joinPathParts("pre", "file2"), + Joiner.on('\n') + .join( + "{", + "\"version\":3,", + "\"file\":\"testcode\",", + "\"lineCount\":1,", + "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",", + "\"sources\":[\"file1\",\"file2\"],", + "\"names\":[\"alert\"]", + "}\n")); } public void testPrefixReplacement2() throws IOException { // mapping can be used to replace a prefix - mappings = ImmutableList.of( - new SourceMap.LocationMapping("pre/file", "src")); - checkSourceMap2("alert(1);", Compiler.joinPathParts("pre", "file1"), "alert(2);", - "pre/file2" , "{\n" + - "\"version\":3,\n" + - "\"file\":\"testcode\",\n" + - "\"lineCount\":1,\n" + - "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",\n" + - "\"sources\":[\"src1\",\"src2\"],\n" + - "\"names\":[\"alert\"]\n" + - "}\n"); + mappings = ImmutableList.of(new SourceMap.PrefixLocationMapping("pre/file", "src")); + checkSourceMap2( + "alert(1);", + Compiler.joinPathParts("pre", "file1"), + "alert(2);", + "pre/file2", + Joiner.on('\n') + .join( + "{", + "\"version\":3,", + "\"file\":\"testcode\",", + "\"lineCount\":1,", + "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",", + "\"sources\":[\"src1\",\"src2\"],", + "\"names\":[\"alert\"]", + "}\n")); } public void testPrefixReplacement3() throws IOException { // multiple mappings can be applied - mappings = ImmutableList.of(new SourceMap.LocationMapping("file1", "x"), - new SourceMap.LocationMapping("file2", "y")); - checkSourceMap2("alert(1);", "file1", "alert(2);", "file2" , "{\n" + - "\"version\":3,\n" + - "\"file\":\"testcode\",\n" + - "\"lineCount\":1,\n" + - "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",\n" + - "\"sources\":[\"x\",\"y\"],\n" + - "\"names\":[\"alert\"]\n" + - "}\n"); + mappings = + ImmutableList.of( + new SourceMap.PrefixLocationMapping("file1", "x"), + new SourceMap.PrefixLocationMapping("file2", "y")); + checkSourceMap2( + "alert(1);", + "file1", + "alert(2);", + "file2", + Joiner.on('\n') + .join( + "{", + "\"version\":3,", + "\"file\":\"testcode\",", + "\"lineCount\":1,", + "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",", + "\"sources\":[\"x\",\"y\"],", + "\"names\":[\"alert\"]", + "}\n")); } public void testPrefixReplacement4() throws IOException { // first match wins - mappings = ImmutableList.of(new SourceMap.LocationMapping("file1", "x"), - new SourceMap.LocationMapping("file", "y")); - checkSourceMap2("alert(1);", "file1", "alert(2);", "file2" , "{\n" + - "\"version\":3,\n" + - "\"file\":\"testcode\",\n" + - "\"lineCount\":1,\n" + - "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",\n" + - "\"sources\":[\"x\",\"y2\"],\n" + - "\"names\":[\"alert\"]\n" + - "}\n"); + mappings = + ImmutableList.of( + new SourceMap.PrefixLocationMapping("file1", "x"), + new SourceMap.PrefixLocationMapping("file", "y")); + checkSourceMap2( + "alert(1);", + "file1", + "alert(2);", + "file2", + Joiner.on('\n') + .join( + "{", + "\"version\":3,", + "\"file\":\"testcode\",", + "\"lineCount\":1,", + "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",", + "\"sources\":[\"x\",\"y2\"],", + "\"names\":[\"alert\"]", + "}\n")); + } + + public void testLambdaReplacement() throws IOException { + mappings = ImmutableList.of((location) -> "mapped/" + location); + checkSourceMap2( + "alert(1);", + "file1", + "alert(2);", + "file2", + Joiner.on('\n') + .join( + "{", + "\"version\":3,", + "\"file\":\"mapped/testcode\",", + "\"lineCount\":1,", + "\"mappings\":\"AAAAA,KAAA,CAAM,CAAN,C,CCAAA,KAAA,CAAM,CAAN;\",", + "\"sources\":[\"mapped/file1\",\"mapped/file2\"],", + "\"names\":[\"alert\"]", + "}\n")); } @Override @@ -109,8 +154,7 @@ public void setUp() { } private void checkSourceMap2( - String js1, String file1, String js2, String file2, String expectedMap) - throws IOException { + String js1, String file1, String js2, String file2, String expectedMap) throws IOException { RunResult result = compile(js1, file1, js2, file2); assertEquals(expectedMap, result.sourceMapFileContent); assertEquals(result.sourceMapFileContent, getSourceMap(result)); diff --git a/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java b/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java index 1f4da364e40..fd063452612 100644 --- a/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java +++ b/test/com/google/javascript/jscomp/deps/ClosureBundlerTest.java @@ -23,7 +23,7 @@ import com.google.javascript.jscomp.transpile.TranspileResult; import com.google.javascript.jscomp.transpile.Transpiler; import java.io.IOException; -import java.nio.file.Paths; +import java.net.URI; import junit.framework.TestCase; import org.mockito.Mockito; @@ -109,13 +109,14 @@ public void testTraditionalWithEvalWithSourceUrl() throws IOException { .isEqualTo("eval(\"\\x22a string\\x22\\n//# sourceURL\\x3dURL\\n\");\n"); } - public void testTranspilation() throws IOException { + public void testTranspilation() throws Exception { String input = "goog.module('Foo');\nclass Foo {}"; + URI uri = new URI("foo.js"); Transpiler transpiler = Mockito.mock(Transpiler.class, RETURNS_SMART_NULLS); when(transpiler.runtime()).thenReturn("RUNTIME;"); - when(transpiler.transpile(Paths.get("foo.js"), input)) - .thenReturn(new TranspileResult(Paths.get("foo.js"), input, "TRANSPILED;", "")); + when(transpiler.transpile(uri, input)) + .thenReturn(new TranspileResult(uri, input, "TRANSPILED;", "")); ClosureBundler bundler = new ClosureBundler(transpiler).withPath("foo.js"); StringBuilder sb = new StringBuilder(); diff --git a/test/com/google/javascript/jscomp/transpile/BaseTranspilerTest.java b/test/com/google/javascript/jscomp/transpile/BaseTranspilerTest.java index 636dddd39c8..110d9d756db 100644 --- a/test/com/google/javascript/jscomp/transpile/BaseTranspilerTest.java +++ b/test/com/google/javascript/jscomp/transpile/BaseTranspilerTest.java @@ -21,8 +21,8 @@ import static org.mockito.Mockito.when; import com.google.javascript.jscomp.bundle.TranspilationException; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.net.URI; +import java.net.URISyntaxException; import junit.framework.TestCase; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -35,8 +35,17 @@ public final class BaseTranspilerTest extends TestCase { private BaseTranspiler.CompilerSupplier compiler; @Mock(answer = RETURNS_SMART_NULLS) BaseTranspiler.CompilerSupplier mockCompiler; - private static final Path FOO_JS = Paths.get("foo.js"); - private static final Path SOURCE_JS = Paths.get("source.js"); + private static final URI FOO_JS; + private static final URI SOURCE_JS; + + static { + try { + FOO_JS = new URI("foo.js"); + SOURCE_JS = new URI("source.js"); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } @Override public void setUp() { diff --git a/test/com/google/javascript/jscomp/transpile/CachingTranspilerTest.java b/test/com/google/javascript/jscomp/transpile/CachingTranspilerTest.java index 4c867599af5..beef4451774 100644 --- a/test/com/google/javascript/jscomp/transpile/CachingTranspilerTest.java +++ b/test/com/google/javascript/jscomp/transpile/CachingTranspilerTest.java @@ -23,8 +23,8 @@ import static org.mockito.Mockito.when; import com.google.common.cache.CacheBuilder; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.net.URI; +import java.net.URISyntaxException; import junit.framework.TestCase; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -35,13 +35,26 @@ public final class CachingTranspilerTest extends TestCase { private Transpiler transpiler; @Mock(answer = RETURNS_SMART_NULLS) Transpiler delegate; - private static final Path FOO_JS = Paths.get("foo.js"); - private static final Path BAR_JS = Paths.get("bar.js"); - private static final Path QUX_JS = Paths.get("qux.js"); + private static final URI FOO_JS; + private static final URI BAR_JS; + private static final URI QUX_JS; - private static final TranspileResult RESULT1 = new TranspileResult(FOO_JS, "bar", "baz", ""); - private static final TranspileResult RESULT2 = new TranspileResult(QUX_JS, "qux", "corge", ""); - private static final TranspileResult RESULT3 = new TranspileResult(BAR_JS, "baz", "xyzzy", ""); + private static final TranspileResult RESULT1; + private static final TranspileResult RESULT2; + private static final TranspileResult RESULT3; + + static { + try { + FOO_JS = new URI("foo.js"); + BAR_JS = new URI("bar.js"); + QUX_JS = new URI("qux.js"); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + RESULT1 = new TranspileResult(FOO_JS, "bar", "baz", ""); + RESULT2 = new TranspileResult(QUX_JS, "qux", "corge", ""); + RESULT3 = new TranspileResult(BAR_JS, "baz", "xyzzy", ""); + } @Override public void setUp() { diff --git a/test/com/google/javascript/jscomp/transpile/TranspileResultTest.java b/test/com/google/javascript/jscomp/transpile/TranspileResultTest.java index f599323e1da..2569dfef9d8 100644 --- a/test/com/google/javascript/jscomp/transpile/TranspileResultTest.java +++ b/test/com/google/javascript/jscomp/transpile/TranspileResultTest.java @@ -19,38 +19,43 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.testing.EqualsTester; -import java.nio.file.Paths; +import java.net.URI; import junit.framework.TestCase; /** Tests for {@link TranspileResult}. */ public final class TranspileResultTest extends TestCase { - public void testEquals() { + public void testEquals() throws Exception { new EqualsTester() .addEqualityGroup( - new TranspileResult(Paths.get("a"), "b", "c", "d"), - new TranspileResult(Paths.get("a"), "b", "c", "d")) - .addEqualityGroup(new TranspileResult(Paths.get("A"), "b", "c", "d")) - .addEqualityGroup(new TranspileResult(Paths.get("a"), "B", "c", "d")) - .addEqualityGroup(new TranspileResult(Paths.get("a"), "b", "C", "d")) - .addEqualityGroup(new TranspileResult(Paths.get("a"), "b", "c", "D")) + new TranspileResult(new URI("a"), "b", "c", "d"), + new TranspileResult(new URI("a"), "b", "c", "d")) + .addEqualityGroup(new TranspileResult(new URI("A"), "b", "c", "d")) + .addEqualityGroup(new TranspileResult(new URI("a"), "B", "c", "d")) + .addEqualityGroup(new TranspileResult(new URI("a"), "b", "C", "d")) + .addEqualityGroup(new TranspileResult(new URI("a"), "b", "c", "D")) .testEquals(); } - public void testEmbedSourceMap_noSourceMap() { - TranspileResult result = new TranspileResult(Paths.get("a"), "b", "c", ""); + public void testEmbedSourceMap_noSourceMap() throws Exception { + TranspileResult result = new TranspileResult(new URI("a"), "b", "c", ""); assertThat(result.embedSourcemap()).isSameAs(result); assertThat(result.embedSourcemapUrl("foo")).isSameAs(result); } - public void testEmbedSourceMap() { - TranspileResult result = new TranspileResult(Paths.get("a"), "b", "c", "{\"version\": 3}"); + public void testEmbedSourceMap() throws Exception { + TranspileResult result = new TranspileResult(new URI("a"), "b", "c", "{\"version\": 3}"); assertThat(result.embedSourcemap()) - .isEqualTo(new TranspileResult( - Paths.get("a"), "b", "c\n//# sourceMappingURL=data:,%7B%22version%22%3A%203%7D\n", "")); + .isEqualTo( + new TranspileResult( + new URI("a"), + "b", + "c\n//# sourceMappingURL=data:,%7B%22version%22%3A%203%7D\n", + "")); assertThat(result.embedSourcemapUrl("foo.js.map")) - .isEqualTo(new TranspileResult( - Paths.get("a"), "b", "c\n//# sourceMappingURL=foo.js.map\n", "{\"version\": 3}")); + .isEqualTo( + new TranspileResult( + new URI("a"), "b", "c\n//# sourceMappingURL=foo.js.map\n", "{\"version\": 3}")); } }