From 4c7002786e804343846af2a95868a938a8617713 Mon Sep 17 00:00:00 2001 From: Marc Egenrieder Date: Fri, 6 Mar 2020 16:28:54 +0100 Subject: [PATCH 1/2] Reports all template warnings in a test run instead of breaking at first --- .../tagliatelle/ReportBrokenTemplates.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java b/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java index cf392f440..5f085579c 100644 --- a/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java +++ b/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java @@ -10,6 +10,8 @@ import sirius.kernel.Sirius; import sirius.kernel.TestLifecycleParticipant; +import sirius.kernel.commons.Strings; +import sirius.kernel.commons.Tuple; import sirius.kernel.di.std.Part; import sirius.kernel.di.std.Priorized; import sirius.kernel.di.std.Register; @@ -20,6 +22,8 @@ import sirius.web.resources.Resource; import sirius.web.resources.Resources; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Pattern; /** @@ -53,6 +57,7 @@ public void beforeTests() { @Override public void afterTests() { + List> templateWarnings = new ArrayList<>(); Sirius.getClasspath().find(Pattern.compile(".*.pasta")).forEach(m -> { String templateName = "/" + m.group(0); if (templateName.startsWith("/default/")) { @@ -64,15 +69,13 @@ public void afterTests() { Compiler compiler = new Compiler(compilationContext, resource.getContentAsString()); compiler.compile(); // If no exception was thrown but still some warnings are present, we - // will still break the test unless the template is marked as "unchecked". - if (!compiler.getContext().getErrors().isEmpty() && !isUnchecked(compilationContext.getTemplate())) { - throw Exceptions.handle() - .withSystemErrorMessage("A warning ocurred while trying to compile: %s - %s." - + " Please fix the reported issue or mark the template as unchecked" - + " via a pragma.", - templateName, - compiler.getContext().getErrors().get(0).toString()) - .handle(); + // collect these warnings unless the template is marked as "unchecked". + // Any collected warnings will break the test at the end. + if (!isUnchecked(compilationContext.getTemplate())) { + final String finalName = templateName; + compiler.getContext().getErrors().forEach(error -> { + templateWarnings.add(Tuple.create(finalName, error.toString())); + }); } } catch (CompileException e) { if (!isUnchecked(compilationContext.getTemplate())) { @@ -84,6 +87,15 @@ public void afterTests() { } } }); + if (!templateWarnings.isEmpty()) { + StringBuilder warningError = new StringBuilder(); + templateWarnings.forEach(tuple -> warningError.append(Strings.apply( + "A warning ocurred while trying to compile: %s - %s.\n", + tuple.getFirst(), + tuple.getSecond()))); + warningError.append("Please fix the reported issue or mark the template as unchecked via a pragma."); + throw Exceptions.handle().withSystemErrorMessage(warningError.toString()).handle(); + } } private boolean isUnchecked(Template template) { From 76809462967007b23bdffe6f1e3a972dc7b12040 Mon Sep 17 00:00:00 2001 From: aha Date: Tue, 7 Apr 2020 17:39:29 +0200 Subject: [PATCH 2/2] Splits a large method into smaller ones using a stream pipeline. --- .../tagliatelle/ReportBrokenTemplates.java | 106 +++++++++++------- 1 file changed, 63 insertions(+), 43 deletions(-) diff --git a/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java b/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java index 5f085579c..b159e4196 100644 --- a/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java +++ b/src/test/java/sirius/tagliatelle/ReportBrokenTemplates.java @@ -8,10 +8,10 @@ package sirius.tagliatelle; +import parsii.tokenizer.ParseError; import sirius.kernel.Sirius; import sirius.kernel.TestLifecycleParticipant; import sirius.kernel.commons.Strings; -import sirius.kernel.commons.Tuple; import sirius.kernel.di.std.Part; import sirius.kernel.di.std.Priorized; import sirius.kernel.di.std.Register; @@ -22,9 +22,11 @@ import sirius.web.resources.Resource; import sirius.web.resources.Resources; -import java.util.ArrayList; -import java.util.List; +import java.util.IntSummaryStatistics; +import java.util.Objects; +import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * Tries to compile all visible tagliatelle templates and breaks the test run if compilation fails. @@ -57,48 +59,66 @@ public void beforeTests() { @Override public void afterTests() { - List> templateWarnings = new ArrayList<>(); - Sirius.getClasspath().find(Pattern.compile(".*.pasta")).forEach(m -> { - String templateName = "/" + m.group(0); - if (templateName.startsWith("/default/")) { - templateName = templateName.substring(8); - } - Resource resource = resources.resolve(templateName).orElse(null); - CompilationContext compilationContext = engine.createCompilationContext(templateName, resource, null); - try { - Compiler compiler = new Compiler(compilationContext, resource.getContentAsString()); - compiler.compile(); - // If no exception was thrown but still some warnings are present, we - // collect these warnings unless the template is marked as "unchecked". - // Any collected warnings will break the test at the end. - if (!isUnchecked(compilationContext.getTemplate())) { - final String finalName = templateName; - compiler.getContext().getErrors().forEach(error -> { - templateWarnings.add(Tuple.create(finalName, error.toString())); - }); - } - } catch (CompileException e) { - if (!isUnchecked(compilationContext.getTemplate())) { - throw Exceptions.handle() - .withSystemErrorMessage("Failed to compile: %s - %s", - templateName, - e.getErrors().get(0).toString()) - .handle(); - } - } - }); - if (!templateWarnings.isEmpty()) { - StringBuilder warningError = new StringBuilder(); - templateWarnings.forEach(tuple -> warningError.append(Strings.apply( - "A warning ocurred while trying to compile: %s - %s.\n", - tuple.getFirst(), - tuple.getSecond()))); - warningError.append("Please fix the reported issue or mark the template as unchecked via a pragma."); - throw Exceptions.handle().withSystemErrorMessage(warningError.toString()).handle(); + StringBuilder output = new StringBuilder(); + IntSummaryStatistics statistics = Sirius.getClasspath() + .find(Pattern.compile(".*.pasta")) + .map(this::resolveAsResource) + .filter(Objects::nonNull) + .map(this::compile) + .filter(this::isExpectedToCompile) + .filter(this::hasErrorsOrWarnings) + .peek(compilationContext -> reportErrors(compilationContext, output)) + .mapToInt(compilationContext -> compilationContext.getErrors().size()) + .summaryStatistics(); + + if (statistics.getCount() > 0) { + throw Exceptions.handle() + .withSystemErrorMessage( + "ReportBrokenTemplates found %s Tagliatelle template(s) with %s error(s)/warning(s):\n\n%s", + statistics.getCount(), + statistics.getSum(), + output.toString()) + .handle(); + } + } + + private Resource resolveAsResource(Matcher matchedResource) { + String templateName = "/" + matchedResource.group(0); + if (templateName.startsWith("/default/")) { + templateName = templateName.substring(8); + } + + return resources.resolve(templateName).orElse(null); + } + + private CompilationContext compile(Resource resource) { + CompilationContext compilationContext = engine.createCompilationContext(resource.getPath(), resource, null); + + try { + Compiler compiler = new Compiler(compilationContext, resource.getContentAsString()); + compiler.compile(); + } catch (CompileException e) { + // We will log all errors later anyway... + Exceptions.ignore(e); } + + return compilationContext; + } + + private boolean isExpectedToCompile(CompilationContext compilationContext) { + return !compilationContext.getTemplate().getPragma(PRAGMA_UNCHECKED).asBoolean(); + } + + private boolean hasErrorsOrWarnings(CompilationContext compilationContext) { + return !compilationContext.getErrors().isEmpty(); } - private boolean isUnchecked(Template template) { - return template.getPragma(PRAGMA_UNCHECKED).asBoolean(); + private void reportErrors(CompilationContext compilationContext, StringBuilder output) { + output.append(Strings.apply("%s:\n%s\n\n", + compilationContext.getTemplate().getName(), + compilationContext.getErrors() + .stream() + .map(ParseError::toString) + .collect(Collectors.joining("\n")))); } }