From 35a08e23ffd6e0fc827b452e8b033c7172230360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Thu, 7 Mar 2024 22:32:08 -0300 Subject: [PATCH] impl. removal of buffers and corrupted artifacts --- .../pirilampo/core/compilers/Compiler.java | 19 +++++++--- .../core/compilers/FeatureToHTMLCompiler.java | 11 ++++-- .../core/compilers/FeatureToPDFCompiler.java | 19 ++++------ .../core/compilers/FolderToHTMLCompiler.java | 11 ++++-- .../core/compilers/FolderToPDFCompiler.java | 19 ++++------ .../clagomess/pirilampo/core/Common.java | 8 +++++ .../compilers/FeatureToHTMLCompilerTest.java | 29 ++++++++++++--- .../compilers/FeatureToPDFCompilerTest.java | 35 +++++++++++++++--- .../compilers/FolderToHTMLCompilerTest.java | 32 ++++++++++++----- .../compilers/FolderToPDFCompilerTest.java | 36 ++++++++++++++++--- .../test/resources/feature_error/yyy.feature | 11 ++++++ 11 files changed, 174 insertions(+), 56 deletions(-) create mode 100644 core/src/test/resources/feature_error/yyy.feature diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java index 9c9f290..b87caf8 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java @@ -2,6 +2,7 @@ import com.github.clagomess.pirilampo.core.dto.FeatureMetadataDto; import com.github.clagomess.pirilampo.core.dto.ParametersDto; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.input.BOMInputStream; @@ -9,10 +10,7 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.time.Instant; -import java.util.Arrays; -import java.util.LinkedHashSet; -import java.util.Set; -import java.util.TreeSet; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -158,4 +156,17 @@ protected void startTimer(){ protected void stopTimer(){ log.info("Compilation time: {}ms", Instant.now().toEpochMilli() - initTimer); } + + @Getter + private final List tempFiles = new LinkedList<>(); + protected File createTempFile() throws IOException { + File bufferHtml = File.createTempFile("pirilampo-buffer-", ".tmp"); + tempFiles.add(bufferHtml); + log.info("Created buffer file: {}", bufferHtml); + return bufferHtml; + } + + protected void deleteAllTempFiles(){ + tempFiles.forEach(File::delete); + } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java index 61d20fd..36ab644 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompiler.java @@ -29,8 +29,10 @@ public void build() throws Exception { startTimer(); GherkinDocumentParser gherkinDocumentParser = new GherkinDocumentParser(parameters, feature); + File outArtifact = getOutArtifact(parameters); + try ( - FileOutputStream fos = new FileOutputStream(getOutArtifact(parameters)); + FileOutputStream fos = new FileOutputStream(outArtifact); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8)); PrintWriter out = new PrintWriter(bw); ){ @@ -54,8 +56,11 @@ public void build() throws Exception { gherkinDocumentParser.build(out); out.print(""); + } catch (Throwable e){ + outArtifact.delete(); + throw e; + } finally { + stopTimer(); } - - stopTimer(); } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java index 8949736..3e221ac 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompiler.java @@ -35,8 +35,7 @@ public FeatureToPDFCompiler(ParametersDto parameters) { public void build() throws Exception { startTimer(); - File bufferHtml = File.createTempFile("pirilampo-buffer-", ".html"); - log.info("Created buffer file: {}", bufferHtml); + File bufferHtml = createTempFile(); File outArtifact = getOutArtifact(parameters); try ( @@ -68,23 +67,19 @@ public void build() throws Exception { new GherkinDocumentParser(parameters, feature).build(out); out.print(""); - } catch (Throwable e){ - bufferHtml.delete(); - throw e; - } finally { - if(bufferHtml.exists()){ - pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath())); - } + } - bufferHtml.delete(); + if(bufferHtml.exists()){ + pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath())); } pdfParser.closeDocument(); } catch (Throwable e){ outArtifact.delete(); + deleteAllTempFiles(); throw e; + } finally { + stopTimer(); } - - stopTimer(); } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java index 674ac50..774cd06 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java @@ -136,8 +136,10 @@ public void build() throws Exception { Set arquivos = listFolder(parameters.getProjectSource()); if(arquivos.isEmpty()) return; + File outArtifact = getOutArtifact(parameters); + try ( - FileOutputStream fos = new FileOutputStream(getOutArtifact(parameters)); + FileOutputStream fos = new FileOutputStream(outArtifact); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8)); PrintWriter out = new PrintWriter(bw); ){ @@ -219,8 +221,11 @@ public void build() throws Exception { writeResourceToOut("htmlTemplate/dist/feature-pasta-angular.min.js", out); out.print("\n"); out.print(""); + } catch (Throwable e){ + outArtifact.delete(); + throw e; + } finally { + stopTimer(); } - - stopTimer(); } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java index d9a566f..3eb6a8b 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompiler.java @@ -46,8 +46,7 @@ public void build() throws Exception { pdfParser.initDocument(fosPDF); for (File feature : arquivos) { - File bufferHtml = File.createTempFile("pirilampo-buffer-", ".html"); - log.info("Created buffer file: {}", bufferHtml); + File bufferHtml = createTempFile(); try ( FileOutputStream fosHTML = new FileOutputStream(bufferHtml); @@ -80,24 +79,20 @@ public void build() throws Exception { } out.print(""); - } catch (Throwable e){ - bufferHtml.delete(); - throw e; - } finally { - if(bufferHtml.exists()){ - pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath())); //@TODO: not closeable - } + } - bufferHtml.delete(); + if(bufferHtml.exists()){ + pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath())); //@TODO: not closeable } } pdfParser.closeDocument(); } catch (Throwable e){ outArtifact.delete(); + deleteAllTempFiles(); throw e; + } finally { + stopTimer(); } - - stopTimer(); } } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/Common.java b/core/src/test/java/com/github/clagomess/pirilampo/core/Common.java index 2d613ce..47b3236 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/Common.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/Common.java @@ -15,4 +15,12 @@ public abstract class Common { protected final File featureFile = new File(Objects.requireNonNull(Thread.currentThread() .getContextClassLoader() .getResource("feature/xxx.Feature")).getFile()); + + protected final File featureErrorFolder = new File(Objects.requireNonNull(Thread.currentThread() + .getContextClassLoader() + .getResource("feature_error")).getFile()); + + protected final File featureErrorFile = new File(Objects.requireNonNull(Thread.currentThread() + .getContextClassLoader() + .getResource("feature_error/yyy.feature")).getFile()); } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompilerTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompilerTest.java index 36c9979..0dae81b 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompilerTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToHTMLCompilerTest.java @@ -4,19 +4,30 @@ import com.github.clagomess.pirilampo.core.dto.ParametersDto; import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum; import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum; +import com.github.clagomess.pirilampo.core.exception.FeatureException; import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.File; +import java.util.Arrays; import static org.junit.jupiter.api.Assertions.*; public class FeatureToHTMLCompilerTest extends Common { + private final File targetFile = new File("target/FeatureToHTMLCompilerTest"); + + @BeforeEach + public void setup(){ + if(!targetFile.isDirectory()){ + assertTrue(targetFile.mkdir()); + }else{ + Arrays.stream(targetFile.listFiles()).forEach(File::delete); + } + } + @Test public void build() throws Exception { - File targetFile = new File("target/FeatureToHTMLCompilerTest"); - if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir()); - ParametersDto parameters = new ParametersDto(); parameters.setProjectSource(featureFile); parameters.setProjectTarget(targetFile); @@ -46,5 +57,15 @@ public void build() throws Exception { */ } - // @TODO: impl unit for test remove buffer on error + @Test + public void checkDeletedBuffersOnError() { + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(featureErrorFile); + parameters.setProjectTarget(targetFile); + parameters.setCompilationType(CompilationTypeEnum.FEATURE); + parameters.setCompilationArtifact(CompilationArtifactEnum.HTML); + + assertThrowsExactly(FeatureException.class, () -> new FeatureToHTMLCompiler(parameters).build()); + assertFalse(new File(targetFile, "yyy.html").exists()); + } } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompilerTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompilerTest.java index 7b5333b..05deb1a 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompilerTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FeatureToPDFCompilerTest.java @@ -4,27 +4,40 @@ import com.github.clagomess.pirilampo.core.dto.ParametersDto; import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum; import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum; +import com.github.clagomess.pirilampo.core.exception.FeatureException; +import lombok.val; import org.apache.pdfbox.Loader; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.graphics.PDXObject; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.text.PDFTextStripper; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.stream.StreamSupport; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class FeatureToPDFCompilerTest extends Common { + private final File targetFile = new File("target/FeatureToPDFCompilerTest"); + + @BeforeEach + public void setup(){ + if(!targetFile.isDirectory()){ + assertTrue(targetFile.mkdir()); + }else{ + Arrays.stream(targetFile.listFiles()).forEach(File::delete); + } + } + @Test @Timeout(8) public void build() throws Exception { - File targetFile = new File("target/FeatureToPDFCompilerTest"); - if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir()); - ParametersDto parameters = new ParametersDto(); parameters.setProjectName("_AA_"); parameters.setProjectVersion("_BB_"); @@ -63,5 +76,17 @@ public void build() throws Exception { // @TODO: also, validate html } - // @TODO: impl unit for test remove buffer on error + @Test + public void checkDeletedBuffersOnError() { + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(featureErrorFile); + parameters.setProjectTarget(targetFile); + parameters.setCompilationType(CompilationTypeEnum.FEATURE); + parameters.setCompilationArtifact(CompilationArtifactEnum.PDF); + + val compiler = new FeatureToPDFCompiler(parameters); + assertThrowsExactly(FeatureException.class, compiler::build); + assertFalse(new File(targetFile, "yyy.pdf").exists()); + Assertions.assertThat(compiler.getTempFiles()).noneMatch(File::exists); + } } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompilerTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompilerTest.java index ead1651..08fc222 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompilerTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompilerTest.java @@ -2,26 +2,36 @@ import com.github.clagomess.pirilampo.core.Common; import com.github.clagomess.pirilampo.core.dto.ParametersDto; -import com.github.clagomess.pirilampo.core.compilers.FolderToHTMLCompiler; +import com.github.clagomess.pirilampo.core.exception.FeatureException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.File; +import java.util.Arrays; import static org.junit.jupiter.api.Assertions.*; @Slf4j public class FolderToHTMLCompilerTest extends Common { + private final File targetFile = new File("target/FolderToHTMLCompilerTest"); + + @BeforeEach + public void setup(){ + if(!targetFile.isDirectory()){ + assertTrue(targetFile.mkdir()); + }else{ + Arrays.stream(targetFile.listFiles()).forEach(File::delete); + } + } + @Test public void build() throws Exception { File logoFile = new File(Thread.currentThread() .getContextClassLoader() .getResource("logo_xxx.png").getFile()); - File targetFile = new File("target/FolderToHTMLCompilerTest"); - if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir()); - ParametersDto parameters = new ParametersDto(); parameters.setMenuColor("#666"); parameters.setProjectLogo(logoFile); @@ -45,9 +55,6 @@ public void build() throws Exception { @Test public void build_master() throws Exception { - File targetFile = new File("target/FolderToHTMLCompilerTest"); - if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir()); - ParametersDto parameters = new ParametersDto(); parameters.setProjectSource(featureFolder); parameters.setProjectMasterSource(featureMasterFolder); @@ -62,5 +69,14 @@ public void build_master() throws Exception { assertTrue(htmlString.contains("YYY_MASTER_YYY")); } - // @TODO: impl unit for test remove buffer on error + @Test + public void checkDeletedBuffersOnError() { + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(featureErrorFolder); + parameters.setProjectTarget(targetFile); + + assertThrowsExactly(FeatureException.class, () -> new FolderToHTMLCompiler(parameters).build()); + + assertFalse(new File(targetFile,"html/index.html").exists()); + } } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompilerTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompilerTest.java index 7fa0571..3d5ca6c 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompilerTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/compilers/FolderToPDFCompilerTest.java @@ -4,23 +4,36 @@ import com.github.clagomess.pirilampo.core.dto.ParametersDto; import com.github.clagomess.pirilampo.core.enums.CompilationArtifactEnum; import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum; +import com.github.clagomess.pirilampo.core.exception.FeatureException; +import lombok.val; import org.apache.pdfbox.Loader; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import java.io.File; +import java.util.Arrays; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class FolderToPDFCompilerTest extends Common { + private final File targetFile = new File("target/FolderToPDFCompilerTest"); + + @BeforeEach + public void setup(){ + if(!targetFile.isDirectory()){ + assertTrue(targetFile.mkdir()); + }else{ + Arrays.stream(targetFile.listFiles()).forEach(File::delete); + } + } + @Test @Timeout(8) public void build() throws Exception { - File targetFile = new File("target/FolderToPDFCompilerTest"); - if(!targetFile.isDirectory()) assertTrue(targetFile.mkdir()); - ParametersDto parameters = new ParametersDto(); parameters.setProjectName("_AA_"); parameters.setProjectVersion("_BB_"); @@ -43,5 +56,18 @@ public void build() throws Exception { } // @TODO: validate content html - // @TODO: impl unit for test remove buffer on error + + @Test + public void checkDeletedBuffersOnError() { + ParametersDto parameters = new ParametersDto(); + parameters.setProjectSource(featureErrorFolder); + parameters.setProjectTarget(targetFile); + parameters.setCompilationType(CompilationTypeEnum.FOLDER); + parameters.setCompilationArtifact(CompilationArtifactEnum.PDF); + + val compiler = new FolderToPDFCompiler(parameters); + assertThrowsExactly(FeatureException.class, compiler::build); + assertFalse(new File(targetFile, "html/index.pdf").exists()); + Assertions.assertThat(compiler.getTempFiles()).noneMatch(File::exists); + } } diff --git a/core/src/test/resources/feature_error/yyy.feature b/core/src/test/resources/feature_error/yyy.feature new file mode 100644 index 0000000..ed71319 --- /dev/null +++ b/core/src/test/resources/feature_error/yyy.feature @@ -0,0 +1,11 @@ +# language: pt +# encoding: utf-8 +Funciona lidade: XX + +XXX + +Contexto: XXX + +Dado XXX +E Teste +| Ibagem |