Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serenity-bdd): auto-detect requirements hierarchy root directory
when calling serenity-bdd run Related tickets: re #1147
- Loading branch information
Showing
7 changed files
with
84 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
serenity.project.name=Serenity/JS with Playwright | ||
serenity.compress.filenames=false | ||
#serenity.test.root=spec | ||
#serenity.requirements.dir=spec | ||
serenity.features.directory=spec | ||
#serenity.estimated.tests.per.requirement=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/serenity-bdd/spec/stage/crew/SpecDirectory.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { expect } from '@integration/testing-tools'; | ||
import { FileSystem, Path } from '@serenity-js/core/lib/io'; | ||
import { describe, it } from 'mocha'; | ||
import { given } from 'mocha-testdata'; | ||
import type { JSONObject } from 'tiny-types'; | ||
|
||
import { SpecDirectory } from '../../../src/stage/crew/serenity-bdd-reporter/SpecDirectory'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { memfs } = require('memfs'); // Typings incorrectly assume the presence of "dom" lib | ||
|
||
describe('SerenityBDDReporter', () => { | ||
|
||
const cwd = Path.from(process.cwd()); | ||
|
||
describe('factory method', () => { | ||
|
||
given([ | ||
`features`, | ||
`spec`, | ||
`tests`, | ||
`test`, | ||
`src`, | ||
]). | ||
it('auto-detects the spec directory ', (specDirectory) => { | ||
const guessedSpecDirectory = new SpecDirectory( | ||
fileSystem({ '/home/alice/my-project': { [`${ specDirectory }`]: {} } }) | ||
).guessLocation(); | ||
|
||
expect(guessedSpecDirectory).to.equal(cwd.resolve(Path.from(`/home/alice/my-project/${ specDirectory }`))); | ||
}) | ||
|
||
it('defaults to the current working directory when no custom specDirectory is provided', () => { | ||
const guessedSpecDirectory = new SpecDirectory( | ||
fileSystem({ '/home/alice/my-project': { } }) | ||
).guessLocation(); | ||
|
||
expect(guessedSpecDirectory).to.equal(cwd.resolve(Path.from(`/home/alice/my-project`))); | ||
}); | ||
}); | ||
}); | ||
|
||
function fileSystem(fakeDirectoryStructure: JSONObject) { | ||
const cwd = Path.from(Object.keys(fakeDirectoryStructure)[0]); | ||
return new FileSystem(cwd, memfs(fakeDirectoryStructure).fs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/serenity-bdd/src/stage/crew/serenity-bdd-reporter/SpecDirectory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { FileSystem, Path } from '@serenity-js/core/lib/io'; | ||
|
||
export class SpecDirectory { | ||
|
||
private static readonly specDirectoryCandidates = [ | ||
`features`, | ||
`spec`, | ||
`tests`, | ||
`test`, | ||
`src`, | ||
]; | ||
|
||
constructor(private readonly fileSystem: FileSystem) { | ||
} | ||
|
||
guessLocation(): Path { | ||
for (const candidate of SpecDirectory.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('.')); | ||
} | ||
} |