Skip to content

Commit

Permalink
Fix issue with test-hash after filename
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Mar 1, 2024
1 parent 9fed152 commit 7d3fc8b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,28 @@ public List<File> findAllTests() throws Exception {

public List<File> findTestsByFiles(List<File> files) throws Exception {

List<PathMatcher> patterns = new Vector<PathMatcher>();
List<TestFilePattern> patterns = new Vector<TestFilePattern>();
for (File file: files) {
patterns.add(fileToPathMatcher(file));
}

List<File> results = new Vector<File>();
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;
}
Expand Down Expand Up @@ -155,7 +160,7 @@ public void buildGraph(String ... ignoreGlobs) throws Exception {

public void buildGraph(List<String> ignoreGlobs) throws Exception {

List<PathMatcher> ignorePatterns = new Vector<PathMatcher>();
List<TestFilePattern> ignorePatterns = new Vector<TestFilePattern>();
ignorePatterns.add(fileToPathMatcher(".nf-test/**"));
ignorePatterns.add(fileToPathMatcher("src/**"));
ignorePatterns.add(fileToPathMatcher("target/**"));
Expand All @@ -178,7 +183,7 @@ public void buildGraph(List<String> ignoreGlobs) throws Exception {
@Override
public void accept(Path path) {

if (matches(path, ignorePatterns)) {
if (matches(path, ignorePatterns) != null) {
//log.warn("Ignored file " + path);
return;
}
Expand Down Expand Up @@ -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<PathMatcher> ignorePatterns) {
public TestFilePattern matches(Path path, List<TestFilePattern> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 7d3fc8b

Please sign in to comment.