Skip to content

Commit 6f487e0

Browse files
aeflukekobenguyent
authored andcommitted
File system extended (#2034)
* check file exists with given substring * tests are added * deleted trailing-space * removed spaces * compatibility for node 8 * assertion message fixed * check file exists with given substring * tests are added * deleted trailing-space * removed spaces * compatibility for node 8 * assertion message fixed * get changed into grab * Examples added * trailing spaces
1 parent 8e09640 commit 6f487e0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/helper/FileSystem.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ class FileSystem extends Helper {
5959
assert.ok(fileExists(this.file), `File ${name} not found in ${this.dir}`);
6060
}
6161

62+
/**
63+
* Checks that file with a name including given text exists in the current directory.
64+
*
65+
*```js
66+
* I.handleDownloads();
67+
* I.click('Download as PDF');
68+
* I.amInPath('output/downloads');
69+
* I.seeFileNameMatching('.pdf');
70+
* ```
71+
*/
72+
seeFileNameMatching(text) {
73+
assert.ok(this.grabFileNames().some(file => file.includes(text)),
74+
`File name which contains ${text} not found in ${this.dir}`);
75+
}
76+
6277
/**
6378
* Checks that file found by `seeFile` includes a text.
6479
* @param {string} text
@@ -98,6 +113,21 @@ class FileSystem extends Helper {
98113
const content = getFileContents(this.file, encoding);
99114
fileEquals(this.file).negate(text, content);
100115
}
116+
117+
/**
118+
* Returns file names in current directory.
119+
*
120+
* ```js
121+
* I.handleDownloads();
122+
* I.click('Download Files');
123+
* I.amInPath('output/downloads');
124+
* const downloadedFileNames = I.grabFileNames();
125+
* ```
126+
*/
127+
grabFileNames() {
128+
return fs.readdirSync(this.dir)
129+
.filter(item => !fs.lstatSync(path.join(this.dir, item)).isDirectory());
130+
}
101131
}
102132

103133
function getFileContents(file, encoding) {

test/unit/helper/FileSystem_test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ describe('FileSystem', () => {
2222
fs.dir.should.eql(path.join(global.codecept_dir, '/data'));
2323
});
2424

25+
it('should see file', () => {
26+
fs.seeFile('data/fs_sample.txt');
27+
fs.amInPath('data');
28+
fs.seeFile('fs_sample.txt');
29+
fs.grabFileNames().should.contain('fs_sample.txt');
30+
fs.seeFileNameMatching('sample');
31+
});
32+
2533
it('should check file contents', () => {
2634
fs.seeFile('data/fs_sample.txt');
2735
fs.seeInThisFile('FileSystem');

0 commit comments

Comments
 (0)