From 7d3fc8b5d3a4a2be98c08c04b41dd334ecbe11f6 Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Fri, 1 Mar 2024 15:04:05 +0100 Subject: [PATCH] Fix issue with test-hash after filename --- .../lang/dependencies/DependencyResolver.java | 44 +++++++------ .../lang/dependencies/TestFilePattern.java | 65 +++++++++++++++++++ 2 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/askimed/nf/test/lang/dependencies/TestFilePattern.java diff --git a/src/main/java/com/askimed/nf/test/lang/dependencies/DependencyResolver.java b/src/main/java/com/askimed/nf/test/lang/dependencies/DependencyResolver.java index 15515c55..001a5bac 100644 --- a/src/main/java/com/askimed/nf/test/lang/dependencies/DependencyResolver.java +++ b/src/main/java/com/askimed/nf/test/lang/dependencies/DependencyResolver.java @@ -52,7 +52,7 @@ public List findAllTests() throws Exception { public List findTestsByFiles(List files) throws Exception { - List patterns = new Vector(); + List patterns = new Vector(); for (File file: files) { patterns.add(fileToPathMatcher(file)); } @@ -60,15 +60,20 @@ public List findTestsByFiles(List files) throws Exception { List results = new Vector(); for (IMetaFile metaFile: graph.getFiles()) { if (metaFile.getType() == IMetaFile.MetaFileType.TEST_FILE) { - File file = new File(metaFile.getFilename()); - if (matches(file.toPath(), patterns)) { - results.add(file); + TestFilePattern matchedPattern = matches(file.toPath(), patterns); + if (matchedPattern != null) { + if (matchedPattern.hasTestId()) { + results.add(new File(file + "@" + matchedPattern.getTestId())); + } else { + results.add(file); + } } } } log.info("Found {} tests.", results.size()); + log.debug("Found tests: " + results); return results; } @@ -155,7 +160,7 @@ public void buildGraph(String ... ignoreGlobs) throws Exception { public void buildGraph(List ignoreGlobs) throws Exception { - List ignorePatterns = new Vector(); + List ignorePatterns = new Vector(); ignorePatterns.add(fileToPathMatcher(".nf-test/**")); ignorePatterns.add(fileToPathMatcher("src/**")); ignorePatterns.add(fileToPathMatcher("target/**")); @@ -178,7 +183,7 @@ public void buildGraph(List ignoreGlobs) throws Exception { @Override public void accept(Path path) { - if (matches(path, ignorePatterns)) { + if (matches(path, ignorePatterns) != null) { //log.warn("Ignored file " + path); return; } @@ -215,30 +220,27 @@ public void accept(Path path) { } - public PathMatcher fileToPathMatcher(String glob) { - return FileSystems.getDefault().getPathMatcher("glob:" + baseDir.getAbsolutePath() + "/" + glob); + public TestFilePattern fileToPathMatcher(String glob) { + return new TestFilePattern(baseDir, glob); } - public PathMatcher fileToPathMatcher(File file) { + public TestFilePattern fileToPathMatcher(File file) { + return new TestFilePattern(file); + } - String pattern = ""; - if (file.isDirectory()) { - pattern = "glob:" + file.toPath().toAbsolutePath().normalize() + "/**"; - } else { - pattern = "glob:" + file.toPath().toAbsolutePath().normalize(); - } + + public PathMatcher pathMatcher(String pattern) { return FileSystems.getDefault().getPathMatcher(pattern); } - public boolean matches(Path path, List ignorePatterns) { + public TestFilePattern matches(Path path, List ignorePatterns) { PathMatcher pathMatcher; - for (PathMatcher pattern : ignorePatterns) { - pathMatcher = pattern; - if (pathMatcher.matches(path)) { - return true; + for (TestFilePattern pattern : ignorePatterns) { + if (pattern.matches(path)) { + return pattern; } } - return false; + return null; } } diff --git a/src/main/java/com/askimed/nf/test/lang/dependencies/TestFilePattern.java b/src/main/java/com/askimed/nf/test/lang/dependencies/TestFilePattern.java new file mode 100644 index 00000000..45de6568 --- /dev/null +++ b/src/main/java/com/askimed/nf/test/lang/dependencies/TestFilePattern.java @@ -0,0 +1,65 @@ +package com.askimed.nf.test.lang.dependencies; + +import java.io.File; +import java.nio.file.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class TestFilePattern { + + private PathMatcher pathMatcher; + + private String testId = null; + + public TestFilePattern (File baseDir, String glob) { + pathMatcher = pathMatcher("glob:" + baseDir.getAbsolutePath() + "/" + glob); + } + + public TestFilePattern (File file) { + String path = file.toPath().toAbsolutePath().normalize().toString(); + testId = extractTestId(path); + if (testId != null) { + path = removeTestId(path); + } + if (file.isDirectory()) { + pathMatcher = pathMatcher("glob:" + path + "/**"); + } else { + pathMatcher = pathMatcher("glob:" + path); + } + } + + public PathMatcher pathMatcher(String pattern) { + return FileSystems.getDefault().getPathMatcher(pattern); + } + + public String removeTestId(String string) { + String regex = "@[a-fA-F0-9]{8}\\b"; + return string.replaceAll(regex, ""); + } + + private String extractTestId(String path) { + String regex = "@[a-fA-F0-9]{8}\\b"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(path); + + if (matcher.find()) { + // Extract hash without the "@" character + return matcher.group().substring(1); + } else { + return null; + } + } + + public boolean matches(Path path) { + return pathMatcher.matches(path); + } + + public String getTestId() { + return testId; + } + + public boolean hasTestId() { + return testId != null; + } + + }