Skip to content

Commit

Permalink
copy attachments with same name (via #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
eroshenkoam authored Jan 12, 2023
1 parent 9184f07 commit 652a1c7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.regex.Pattern;

import static io.eroshenkoam.xcresults.export.ExportCommand.FILE_EXTENSION_HEIC;
import static io.eroshenkoam.xcresults.util.ParseUtil.parseDate;
import static io.eroshenkoam.xcresults.util.FormatUtil.getAttachmentFileName;
import static io.eroshenkoam.xcresults.util.FormatUtil.parseDate;
import static java.util.Objects.isNull;

public class Allure2ExportFormatter implements ExportFormatter {
Expand Down Expand Up @@ -239,11 +240,13 @@ private List<Attachment> getAttachments(final Iterable<JsonNode> nodes) {
final List<Attachment> attachments = new ArrayList<>();
for (JsonNode node : nodes) {
final String originalFileName = node.get(FILENAME).get(VALUE).asText();
final String fileName = FILE_EXTENSION_HEIC.equals(FilenameUtils.getExtension(originalFileName))
final String fileExtension = FilenameUtils.getExtension(originalFileName);
final String sources = getAttachmentFileName(fileExtension);
final String fileName = FILE_EXTENSION_HEIC.equals(fileExtension)
? String.format("%s.%s", FilenameUtils.getBaseName(originalFileName), "jpeg")
: originalFileName;
final Attachment attachment = new Attachment()
.setSource(fileName)
.setSource(sources)
.setName(fileName);
attachments.add(attachment);
}
Expand Down
96 changes: 49 additions & 47 deletions src/main/java/io/eroshenkoam/xcresults/export/ExportCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.qameta.allure.model.ExecutableItem;
import io.qameta.allure.model.TestResult;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import picocli.CommandLine;
Expand All @@ -11,14 +13,10 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.*;

import static io.eroshenkoam.xcresults.util.ParseUtil.parseDate;
import static io.eroshenkoam.xcresults.util.FormatUtil.getResultFilePath;
import static io.eroshenkoam.xcresults.util.FormatUtil.parseDate;

@CommandLine.Command(
name = "export", mixinStandardHelpOptions = true,
Expand Down Expand Up @@ -68,7 +66,7 @@ public class ExportCommand implements Runnable {

@CommandLine.Option(
names = {"--format"},
description = "Export format (json, allure2)"
description = "Export format (json, allure2), *deprecated"
)
protected ExportFormat format = ExportFormat.allure2;

Expand Down Expand Up @@ -134,25 +132,33 @@ private void runUnsafe() throws Exception {
}
});

System.out.println(String.format("Export information about %s test summaries...", testSummaries.size()));
System.out.printf("Export information about %s test summaries...%n", testSummaries.size());
final Map<String, String> attachmentsRefs = new HashMap<>();
testSummaries.forEach((testSummary, meta) -> {
exportTestSummary(meta, testSummary);
if (testSummary.has(ACTIVITY_SUMMARIES)) {
for (final JsonNode activity : testSummary.get(ACTIVITY_SUMMARIES).get(VALUES)) {
attachmentsRefs.putAll(getAttachmentRefs(activity));
}
}
if (testSummary.has(FAILURE_SUMMARIES)) {
for (final JsonNode failure : testSummary.get(FAILURE_SUMMARIES).get(VALUES)) {
attachmentsRefs.putAll(getAttachmentRefs(failure));
}
}
});
System.out.println(String.format("Export information about %s attachments...", attachmentsRefs.size()));
for (final Map.Entry<JsonNode, ExportMeta> entry : testSummaries.entrySet()) {
final JsonNode testSummary = entry.getKey();
final ExportMeta meta = entry.getValue();

final TestResult testResult = new Allure2ExportFormatter().format(meta, testSummary);
final Path testSummaryPath = getResultFilePath(outputPath);
mapper.writeValue(testSummaryPath.toFile(), testResult);

final Map<String, List<String>> attachmentSources = getAttachmentSources(testResult);
final List<JsonNode> summaries = new ArrayList<>();
summaries.addAll(getAttributeValues(testSummary, ACTIVITY_SUMMARIES));
summaries.addAll(getAttributeValues(testSummary, FAILURE_SUMMARIES));
summaries.forEach(summary -> {
getAttachmentRefs(summary).forEach((name, ref) -> {
if (attachmentSources.containsKey(name)) {
final List<String> sources = attachmentSources.get(name);
sources.forEach(source -> attachmentsRefs.put(source, ref));
}
});
});
}
System.out.printf("Export information about %s attachments...%n", attachmentsRefs.size());
for (Map.Entry<String, String> attachment : attachmentsRefs.entrySet()) {
final Path attachmentPath = outputPath.resolve(attachment.getKey());
final String attachmentRef = attachment.getValue();
final Path attachmentPath = outputPath.resolve(attachment.getKey());
exportReference(attachmentRef, attachmentPath);
}
}
Expand All @@ -165,31 +171,19 @@ private ExportMeta getTestMeta(final ExportMeta meta, final JsonNode testableSum
return exportMeta;
}

private void exportTestSummary(final ExportMeta meta, final JsonNode testSummary) {
Path testSummaryPath = null;
Object formattedResult = null;
final String uuid = UUID.randomUUID().toString();
switch (format) {
case json: {
final String testSummaryFilename = String.format("%s.json", uuid);
testSummaryPath = outputPath.resolve(testSummaryFilename);
formattedResult = new JsonExportFormatter().format(meta, testSummary);
break;
}
case allure2: {
final String testSummaryFilename = String.format("%s-result.json", uuid);
testSummaryPath = outputPath.resolve(testSummaryFilename);
formattedResult = new Allure2ExportFormatter().format(meta, testSummary);
break;
}
private Map<String, List<String>> getAttachmentSources(final ExecutableItem executableItem) {
final Map<String, List<String>> attachments = new HashMap<>();
if (Objects.nonNull(executableItem.getAttachments())) {
executableItem.getAttachments().forEach(a -> {
final List<String> sources = attachments.getOrDefault(a.getName(), new ArrayList<>());
sources.add(a.getSource());
attachments.put(a.getName(), sources);
});
}
try {
if (Objects.nonNull(formattedResult)) {
mapper.writeValue(testSummaryPath.toFile(), formattedResult);
}
} catch (IOException e) {
throw new RuntimeException(e);
if (Objects.nonNull(executableItem.getSteps())) {
executableItem.getSteps().forEach(s -> attachments.putAll(getAttachmentSources(s)));
}
return attachments;
}

private Map<String, String> getAttachmentRefs(final JsonNode test) {
Expand Down Expand Up @@ -230,6 +224,14 @@ private List<JsonNode> getTestSummaries(final JsonNode test) {
return summaries;
}

private List<JsonNode> getAttributeValues(final JsonNode node, final String attributeName) {
final List<JsonNode> result = new ArrayList<>();
if (node.has(attributeName) && node.get(attributeName).has(VALUES)) {
node.get(attributeName).get(VALUES).forEach(result::add);
}
return result;
}

private JsonNode readSummary() {
final ProcessBuilder builder = new ProcessBuilder();
builder.command(
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/io/eroshenkoam/xcresults/util/FormatUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.eroshenkoam.xcresults.util;

import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.UUID;

public final class FormatUtil {

private FormatUtil(){
}

public static String getResultFileName() {
final String uuid = UUID.randomUUID().toString();
return String.format("%s-result.json", uuid);
}

public static String getAttachmentFileName(final String fileExtension) {
final String uuid = UUID.randomUUID().toString();
return String.format("%s-attachment.%s", uuid, fileExtension);
}

public static Path getResultFilePath(final Path outputDir) {
return outputDir.resolve(getResultFileName());
}

@SuppressWarnings("PMD.SimpleDateFormatNeedsLocale")
public static Long parseDate(final String date) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
return format.parse(date).getTime();
} catch (ParseException e) {
return null;
}
}

}
21 changes: 0 additions & 21 deletions src/main/java/io/eroshenkoam/xcresults/util/ParseUtil.java

This file was deleted.

0 comments on commit 652a1c7

Please sign in to comment.