diff --git a/core/src/main/java/org/jsmart/zerocode/core/domain/builders/ZeroCodeIoWriteBuilder.java b/core/src/main/java/org/jsmart/zerocode/core/domain/builders/ZeroCodeIoWriteBuilder.java index 7d4c5e26..92fd0fdb 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/domain/builders/ZeroCodeIoWriteBuilder.java +++ b/core/src/main/java/org/jsmart/zerocode/core/domain/builders/ZeroCodeIoWriteBuilder.java @@ -16,6 +16,7 @@ import java.util.concurrent.Executors; import static org.jsmart.zerocode.core.constants.ZeroCodeReportConstants.TARGET_REPORT_DIR; +import static org.jsmart.zerocode.core.utils.SmartUtils.sanitizeReportFileName; import static org.slf4j.LoggerFactory.getLogger; public class ZeroCodeIoWriteBuilder { @@ -60,6 +61,7 @@ public synchronized void printToFile(String fileName) { final ObjectMapper mapper = new ObjectMapperProvider().get(); + fileName = sanitizeReportFileName(fileName); File file = new File(TARGET_REPORT_DIR + fileName); file.getParentFile().mkdirs(); mapper.writeValue(file, this.built); diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java index 8e8a8e2b..966f1476 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java +++ b/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java @@ -87,6 +87,10 @@ public Map readJsonStringAsMap(String json) throws IOException { return map; } + public static String sanitizeReportFileName(String fileName) { + return fileName.replaceAll("[^A-Za-z0-9 \\-_.]", "_"); + } + public static List getAllEndPointFiles(String packageName) { if(isValidAbsolutePath(packageName)){ return retrieveScenariosByAbsPath(packageName); diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java index 247d04ef..a2b7421b 100644 --- a/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java +++ b/core/src/test/java/org/jsmart/zerocode/core/utils/SmartUtilsTest.java @@ -23,6 +23,7 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; @@ -236,6 +237,13 @@ public void testSuiteFolder_symAbsolutePath() { assertThat(allScenarios.get(0), containsString("cherry_pick_tests/folder_b/test_case_2.json")); } + @Test + public void testSanitizeReportFile() { + String orig = "file !#$%&'()*+,-./09:;<=>?@AZ[]^_`az{|}~\"\\"; + String dest = "file ___________-._09_______AZ_____az______"; + assertThat(SmartUtils.sanitizeReportFileName(orig), equalTo(dest)); + } + // Move this to File Util class private static File createCascadeIfNotExisting(String fileName) { try {