Skip to content

Commit

Permalink
Introduced SpecFileFilters to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-molak committed Jan 5, 2024
1 parent 6286a1e commit 5f7a4fc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_REQUIREMENT_TYPES;
import static net.thucydides.model.requirements.SpecFileFilters.cucumberFeatureFiles;
import static net.thucydides.model.requirements.SpecFileFilters.javascriptSpecFiles;

public class DefaultCapabilityTypes {

Expand All @@ -24,28 +25,19 @@ public class DefaultCapabilityTypes {

private SearchForFilesOfType cucumberFileMatcher;
private SearchForFilesOfType jbehaveFileMatcher;
private SearchForFilesOfType javaScriptSpecMatcher;
private SearchForFilesOfType javascriptSpecMatcher;

private Map<String, List<String>> requirementsCache = new HashMap<>();
public static DefaultCapabilityTypes instance() {
return INSTANCE;
}

private final static String JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN =
".*" +
"\\.(spec|test|integration|it|e2e|spec\\.e2e|spec-e2e)" + // Consider only test files...
"\\.(jsx?|mjsx?|cjsx?|tsx?|mtsx?|ctsx?)$"; // implemented in either JavaScript or TypeScript

private final static String JAVASCRIPT_SPEC_FILE_NAME_PATTERN =
"^(?!.*/(node_modules|jspm_packages|web_modules)/)" + // Ignore external dependencies
JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN;

public void clear() {
requirementsCache.clear();
defaultCapabilityTypes = null;
jbehaveFileMatcher = null;
cucumberFileMatcher = null;
javaScriptSpecMatcher = null;
javascriptSpecMatcher = null;
}

public List<String> getRequirementTypes(EnvironmentVariables environmentVariables, Optional<Path> root) {
Expand Down Expand Up @@ -120,7 +112,7 @@ private Optional<SearchForFilesOfType> getJBehaveFileMatcher(Optional<Path> root
try {
// Optional<Path> root = RootDirectory.definedIn(environmentVariables).featuresOrStoriesRootDirectory();// findResourcePath(rootRequirementsDirectory + "/stories");
if (root.isPresent()) {
jbehaveFileMatcher = new SearchForFilesOfType(root.get(), ".story");
jbehaveFileMatcher = new SearchForFilesOfType(root.get(), SpecFileFilters.jbehaveStoryFiles());
Files.walkFileTree(root.get(), jbehaveFileMatcher);
return Optional.of(jbehaveFileMatcher);
}
Expand All @@ -131,18 +123,18 @@ private Optional<SearchForFilesOfType> getJBehaveFileMatcher(Optional<Path> root
}

private Optional<SearchForFilesOfType> getJavaScriptSpecMatcher(Optional<Path> root) {
if (javaScriptSpecMatcher != null) {
return Optional.of(javaScriptSpecMatcher);
if (javascriptSpecMatcher != null) {
return Optional.of(javascriptSpecMatcher);
}

try {
if (! root.isPresent()) {
return Optional.empty();
}

javaScriptSpecMatcher = new SearchForFilesOfType(root.get(), Pattern.compile(JAVASCRIPT_SPEC_FILE_NAME_PATTERN));
Files.walkFileTree(root.get(), javaScriptSpecMatcher);
return Optional.of(javaScriptSpecMatcher);
javascriptSpecMatcher = new SearchForFilesOfType(root.get(), javascriptSpecFiles());
Files.walkFileTree(root.get(), javascriptSpecMatcher);
return Optional.of(javascriptSpecMatcher);
}
catch (IOException e) {
return Optional.empty();
Expand Down Expand Up @@ -172,7 +164,7 @@ private Optional<SearchForFilesOfType> getCucumberFileMatcher(Optional<Path> roo
try {
// Optional<Path> root = RootDirectory.definedIn(environmentVariables).featuresOrStoriesRootDirectory();// findResourcePath(rootRequirementsDirectory + "/stories");
if (root.isPresent()) {
cucumberFileMatcher = new SearchForFilesOfType(root.get(), ".feature");
cucumberFileMatcher = new SearchForFilesOfType(root.get(), cucumberFeatureFiles());
Files.walkFileTree(root.get(), cucumberFileMatcher);
return Optional.of(cucumberFileMatcher);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static net.thucydides.model.files.TheDirectoryStructure.startingAt;
import static net.thucydides.model.requirements.SpecFileFilters.*;
import static net.thucydides.model.requirements.RequirementsPath.pathElements;
import static net.thucydides.model.requirements.RequirementsPath.stripRootFromPath;
import static net.thucydides.model.util.Inflector.inflection;
Expand Down Expand Up @@ -836,25 +836,14 @@ private boolean hasFeatureStoryOrJavaScriptSpecFiles(String path) {
return false;
}

boolean hasStoryFiles = requirementDirectory.list(storyFiles()).length > 0;
boolean hasFeatureFiles = requirementDirectory.list(featureFiles()).length > 0;
boolean hasStoryFiles = requirementDirectory.list(jbehaveStoryFiles()).length > 0;
boolean hasFeatureFiles = requirementDirectory.list(cucumberFeatureFiles()).length > 0;

boolean hasJavaScriptSpecFiles = requirementDirectory.list(javascriptSpecFiles()).length > 0;

return hasStoryFiles || hasFeatureFiles || hasJavaScriptSpecFiles;
}

private FilenameFilter storyFiles() {
return (dir, name) -> name.endsWith(".story");
}

private FilenameFilter featureFiles() {
return (dir, name) -> name.endsWith(".feature");
}

private FilenameFilter javascriptSpecFiles() {
return (dir, name) -> name.matches(JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN);
}

private boolean classpathResourceExistsFor(String path) {
return getClass().getResource(resourcePathFor(path)) != null;
}
Expand Down Expand Up @@ -891,7 +880,7 @@ private boolean storyFeatureFilesOrJavaScriptSpecsExistIn(File directory) {
return startingAt(normalised(directory)).containsFiles(
thatAreStories(),
thatAreNarratives(),
thatAreJavaScriptSpecs()
thatAreJavascriptSpecFiles()
);
}

Expand All @@ -906,33 +895,6 @@ private FileFilter thatAreStories() {
};
}

private FileFilter thatAreJavaScriptSpecs() {
return file -> {
String filename = file.getName().toLowerCase();
return JAVASCRIPT_SPEC_FILE_PATTERN.matcher(filename).matches();
};
}

// todo: extract from DefaultCapabilityTypes
private final static String JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN =
".*" +
"\\.(spec|test|integration|it|e2e|spec\\.e2e|spec-e2e)" + // Consider only test files...
"\\.(jsx?|mjsx?|cjsx?|tsx?|mtsx?|ctsx?)$"; // implemented in either JavaScript or TypeScript

private final static String JAVASCRIPT_SPEC_FILE_NAME_PATTERN =
"^(?!.*/(node_modules|jspm_packages|web_modules)/)" + // Ignore external dependencies
JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN;

private final static Pattern JAVASCRIPT_SPEC_FILE_PATTERN = Pattern.compile(JAVASCRIPT_SPEC_FILE_NAME_PATTERN);


private FileFilter thatAreNarratives() {
return file -> file.getName().equalsIgnoreCase("narrative.txt")
|| file.getName().equalsIgnoreCase("narrative.md")
|| file.getName().equalsIgnoreCase("readme.md")
|| file.getName().equalsIgnoreCase("placeholder.txt");
}

private boolean isSupportedFileStoryExtension(String storyFileExtension) {
return (storyFileExtension.equalsIgnoreCase(FEATURE_EXTENSION) || storyFileExtension.equalsIgnoreCase(STORY_EXTENSION));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,40 @@
package net.thucydides.model.requirements;

import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.nio.file.FileVisitResult.CONTINUE;

public class SearchForFilesOfType extends SimpleFileVisitor<Path> {


private final Pattern pattern;
private final FilenameFilter filenameFilter;
Path root;
List<Path> matchingFiles;
int maxDepth;

public SearchForFilesOfType(Path root, String suffix) {
this(root, Pattern.compile(".*" + Pattern.quote(suffix) + "$"));
}

public SearchForFilesOfType(Path root, Pattern pattern) {
this.pattern = pattern;
public SearchForFilesOfType(Path root, FilenameFilter filenameFilter) {
this.filenameFilter = filenameFilter;
this.root = root;
this.matchingFiles = new ArrayList<>();
this.maxDepth = 0;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
Matcher matcher = pattern.matcher(file.getFileName().toString());

if (matcher.matches()) {
if (filenameFilter.accept(file.toFile(), file.getFileName().toString())) {
matchingFiles.add(file);
int depth = file.getNameCount() - root.getNameCount() - 1;
if (depth > maxDepth) {
maxDepth = depth;
}
}

return CONTINUE;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.thucydides.model.requirements;

import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

public class SpecFileFilters {
private static final String JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN =
".*" +
"\\.(spec|test|integration|it|e2e|spec\\.e2e|spec-e2e)" + // Consider only test files...
"\\.(jsx?|mjsx?|cjsx?|tsx?|mtsx?|ctsx?)$"; // implemented in either JavaScript or TypeScript

private static final String JAVASCRIPT_SPEC_FILE_NAME_PATTERN =
"^(?!.*/(node_modules|jspm_packages|web_modules)/)" + // Ignore external dependencies
JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN;

private final static Pattern JAVASCRIPT_SPEC_FILE_PATTERN = Pattern.compile(JAVASCRIPT_SPEC_FILE_NAME_PATTERN);

private static final Set<String> NARRATIVE_FILE_NAMES = new HashSet<>(Set.of(
"narrative.txt",
"narrative.md",
"readme.md",
"placeholder.txt"
));

public static FilenameFilter javascriptSpecFiles() {
return (dir, name) -> name.matches(JAVASCRIPT_SPEC_FILE_EXTENSION_PATTERN);
}

public static FilenameFilter jbehaveStoryFiles() {
return (dir, name) -> name.endsWith(".story");
}

public static FilenameFilter cucumberFeatureFiles() {
return (dir, name) -> name.endsWith(".feature");
}

/**
* Used with {@link net.thucydides.model.files.TheDirectoryStructure}
*/
public static FileFilter thatAreJavascriptSpecFiles() {
return file -> JAVASCRIPT_SPEC_FILE_PATTERN.matcher(file.getName().toLowerCase()).matches();
}

/**
* Used with {@link net.thucydides.model.files.TheDirectoryStructure}
*/
public static FileFilter thatAreNarratives() {
return file -> NARRATIVE_FILE_NAMES.contains(file.getName().toLowerCase());
}
}

0 comments on commit 5f7a4fc

Please sign in to comment.