hansva commented on code in PR #9372:
URL: https://github.com/apache/hop/pull/9372#discussion_r4537683768


##########
plugins/transforms/surefirereport/src/main/java/org/apache/hop/pipeline/transforms/surefirereport/SurefireReportOutput.java:
##########
@@ -223,9 +224,18 @@ private void writeReport() throws HopException {
     }
 
     Path reportPath = Paths.get(filename);
-    if (meta.isCreateParentFolder() && reportPath.getParent() != null) {
+    Path parentFolder = reportPath.getParent();
+    if (parentFolder != null && !Files.isDirectory(parentFolder)) {
+      if (!meta.isCreateParentFolder()) {

Review Comment:
   With 'Create parent folder' off, a missing folder is now only detected here, 
when the report is written at the end of the stream, after every test has run. 
Before, the writer always created the folder. So an IT run with a missing 
report directory (e.g. an unset `IT_SUREFIRE_DIR`) can run for hours and then 
fail, and all collected results are lost. Could the resolved parent folder be 
checked in `init()` instead, so it fails before any work is done?



##########
plugins/transforms/surefirereport/src/main/java/org/apache/hop/pipeline/transforms/surefirereport/SurefireReportOutput.java:
##########
@@ -223,9 +224,18 @@ private void writeReport() throws HopException {
     }
 
     Path reportPath = Paths.get(filename);

Review Comment:
   Not introduced here, but the new check builds on it: the transform resolves 
and writes the report with java.nio (`Paths.get`, `Files`) rather than `HopVfs` 
like the other output transforms (e.g. `TextFileOutput.createParentFolder`). A 
filename like `file:///tmp/out/r.xml` or `s3://bucket/r.xml` becomes a relative 
`file:/tmp/out` / `s3:/bucket` path, so with the option off this now reports an 
existing folder as missing, and on Windows `Paths.get` throws an unwrapped 
`InvalidPathException`. Fine as a follow-up: moving the transform to 
`HopVfs`/`FileObject` would fix all of these.



##########
plugins/transforms/surefirereport/src/main/java/org/apache/hop/pipeline/transforms/surefirereport/SurefireReportOutput.java:
##########
@@ -223,9 +224,18 @@ private void writeReport() throws HopException {
     }
 
     Path reportPath = Paths.get(filename);
-    if (meta.isCreateParentFolder() && reportPath.getParent() != null) {
+    Path parentFolder = reportPath.getParent();
+    if (parentFolder != null && !Files.isDirectory(parentFolder)) {

Review Comment:
   `!Files.isDirectory` also matches a parent path that exists as a regular 
file. With the option off the message then says the folder doesn't exist 
although it does, as a file; with it on, `createDirectories` throws 
`FileAlreadyExistsException`, which shows up as 'Unable to write Surefire 
report ... : /data/reports' without explanation. A separate message for 'exists 
but is not a folder' would help.



##########
plugins/transforms/surefirereport/src/test/java/org/apache/hop/pipeline/transforms/surefirereport/SurefireReportOutputTest.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.pipeline.transforms.surefirereport;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.apache.hop.core.IRowSet;
+import org.apache.hop.core.exception.HopException;
+import org.apache.hop.core.logging.ILoggingObject;
+import org.apache.hop.core.row.IRowMeta;
+import org.apache.hop.core.row.RowMeta;
+import org.apache.hop.core.row.value.ValueMetaString;
+import org.apache.hop.pipeline.transforms.mock.TransformMockHelper;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class SurefireReportOutputTest {
+
+  @TempDir Path tempDir;
+
+  private TransformMockHelper<SurefireReportOutputMeta, 
SurefireReportOutputData> helper;
+
+  @BeforeEach
+  void setUp() {
+    helper =
+        new TransformMockHelper<>(
+            "surefire report output",
+            SurefireReportOutputMeta.class,
+            SurefireReportOutputData.class);
+    when(helper.logChannelFactory.create(any(), any(ILoggingObject.class)))
+        .thenReturn(helper.iLogChannel);
+    when(helper.pipeline.isRunning()).thenReturn(true);
+  }
+
+  @AfterEach
+  void tearDown() {
+    helper.cleanUp();
+  }
+
+  @Test
+  void missingFolderFailsWhenCreateParentFolderIsOff() {
+    Path report = tempDir.resolve("nofolder/a/b/r.xml");
+
+    HopException e = assertThrows(HopException.class, () -> 
runTransform(report, false));
+

Review Comment:
   Small one: this asserts on the English text 'does not exist', so it breaks 
once the key gets a translation and the build runs under another default 
locale. Asserting on the exception type, or comparing with 
`BaseMessages.getString(...)` for the key, would be more robust.



##########
plugins/transforms/surefirereport/src/main/java/org/apache/hop/pipeline/transforms/surefirereport/SurefireReportWriter.java:
##########
@@ -60,11 +64,6 @@ public static void write(Path reportFile, String suiteName, 
List<SurefireTestCas
       }
     }
 
-    Path parent = reportFile.getParent();
-    if (parent != null) {
-      Files.createDirectories(parent);
-    }
-
     Path tmp = reportFile.resolveSibling(reportFile.getFileName().toString() + 
".tmp");

Review Comment:
   Now that `write()` no longer creates the folder, a caller that skips the 
transform's check (or a folder removed in between) gets a raw 
`NoSuchFileException` naming `r.xml.tmp`. Checking the parent here and throwing 
an `IOException` that names the report folder would make the documented 
precondition clearer.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to