@serenity-js/core
Version:
The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure
72 lines • 2.89 kB
JavaScript
import { ConfigurationError } from '../errors/index.js';
import { CapabilityTag, FeatureTag, Tag, ThemeTag } from '../model/index.js';
import { Path } from './Path.js';
export class RequirementsHierarchy {
fileSystem;
userDefinedSpecDirectory;
root;
static specDirectoryCandidates = [
`features`,
`specs`,
`spec`,
`tests`,
`test`,
`src`,
];
constructor(fileSystem, userDefinedSpecDirectory) {
this.fileSystem = fileSystem;
this.userDefinedSpecDirectory = userDefinedSpecDirectory;
}
requirementTagsFor(pathToSpec, featureName) {
const [fileBasedFeatureName, capabilityName, ...themeNames] = this.hierarchyFor(pathToSpec).reverse().filter(segment => !['.', '..'].includes(segment));
const themeTags = themeNames.reverse().map(themeName => Tag.humanReadable(ThemeTag, themeName));
const capabilityTag = capabilityName && Tag.humanReadable(CapabilityTag, capabilityName);
const featureTag = featureName
? new FeatureTag(featureName)
: Tag.humanReadable(FeatureTag, fileBasedFeatureName);
return [
...themeTags,
capabilityTag,
featureTag
].filter(Boolean);
}
hierarchyFor(pathToSpec) {
const relative = this.rootDirectory().relative(pathToSpec);
return relative.split().map((segment, i, segments) => {
// return all the segments as-is, except for the last one
if (i < segments.length - 1) {
return segment;
}
// Strip the extension, like `.feature` or `.spec.ts`
const firstDotIndex = segment.indexOf('.');
return firstDotIndex === -1
? segment
: segment.slice(0, firstDotIndex);
});
}
rootDirectory() {
if (!this.root) {
this.root = this.userDefinedSpecDirectory
? this.resolve(this.userDefinedSpecDirectory)
: this.guessRootDirectory();
}
return this.root;
}
guessRootDirectory() {
for (const candidate of RequirementsHierarchy.specDirectoryCandidates) {
const candidateSpecDirectory = Path.from(candidate);
if (this.fileSystem.exists(Path.from(candidate))) {
return this.fileSystem.resolve(candidateSpecDirectory);
}
}
// default to current working directory
return this.fileSystem.resolve(Path.from('.'));
}
resolve(userDefinedRootDirectory) {
if (!this.fileSystem.exists(userDefinedRootDirectory)) {
throw new ConfigurationError(`Configured specDirectory \`${userDefinedRootDirectory}\` does not exist`);
}
return this.fileSystem.resolve(userDefinedRootDirectory);
}
}
//# sourceMappingURL=RequirementsHierarchy.js.map