|
| 1 | +const assert = require('assert'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +const Helper = require('@codeceptjs/helper'); |
| 6 | +const { fileExists } = require('../utils'); |
| 7 | +const { fileIncludes } = require('../assert/include'); |
| 8 | +const { fileEquals } = require('../assert/equal'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Helper for testing filesystem. |
| 12 | + * Can be easily used to check file structures: |
| 13 | + * |
| 14 | + * ```js |
| 15 | + * I.amInPath('test'); |
| 16 | + * I.seeFile('codecept.js'); |
| 17 | + * I.seeInThisFile('FileSystem'); |
| 18 | + * I.dontSeeInThisFile("WebDriver"); |
| 19 | + * ``` |
| 20 | + * |
| 21 | + * ## Configuration |
| 22 | + * |
| 23 | + * Enable helper in config file: |
| 24 | + * |
| 25 | + * ```js |
| 26 | + * helpers: { |
| 27 | + * FileSystem: {}, |
| 28 | + * } |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * ## Methods |
| 32 | + */ |
| 33 | +class FileSystem extends Helper { |
| 34 | + constructor() { |
| 35 | + super(); |
| 36 | + this.dir = global.codecept_dir; |
| 37 | + this.file = ''; |
| 38 | + } |
| 39 | + |
| 40 | + _before() { |
| 41 | + this.debugSection('Dir', this.dir); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Enters a directory In local filesystem. |
| 46 | + * Starts from a current directory |
| 47 | + * @param {string} openPath |
| 48 | + */ |
| 49 | + amInPath(openPath) { |
| 50 | + this.dir = path.join(global.codecept_dir, openPath); |
| 51 | + this.debugSection('Dir', this.dir); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Writes text to file |
| 56 | + * @param {string} name |
| 57 | + * @param {string} text |
| 58 | + */ |
| 59 | + writeToFile(name, text) { |
| 60 | + fs.writeFileSync(path.join(this.dir, name), text); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Checks that file exists |
| 65 | + * @param {string} name |
| 66 | + */ |
| 67 | + seeFile(name) { |
| 68 | + this.file = path.join(this.dir, name); |
| 69 | + this.debugSection('File', this.file); |
| 70 | + assert.ok(fileExists(this.file), `File ${name} not found in ${this.dir}`); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Waits for the file to be present in the current directory. |
| 75 | + * |
| 76 | + * ```js |
| 77 | + * I.handleDownloads('downloads/largeFilesName.txt'); |
| 78 | + * I.click('Download large File'); |
| 79 | + * I.amInPath('output/downloads'); |
| 80 | + * I.waitForFile('largeFilesName.txt', 10); // wait 10 seconds for file |
| 81 | + * ``` |
| 82 | + * @param {string} name |
| 83 | + * @param {number} [sec=1] seconds to wait |
| 84 | + */ |
| 85 | + async waitForFile(name, sec = 1) { |
| 86 | + if (sec === 0) assert.fail('Use `seeFile` instead of waiting 0 seconds!'); |
| 87 | + const waitTimeout = sec * 1000; |
| 88 | + this.file = path.join(this.dir, name); |
| 89 | + this.debugSection('File', this.file); |
| 90 | + return isFileExists(this.file, waitTimeout).catch(() => { |
| 91 | + throw new Error(`file (${name}) still not present in directory ${this.dir} after ${waitTimeout / 1000} sec`); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Checks that file with a name including given text exists in the current directory. |
| 97 | + * |
| 98 | + *```js |
| 99 | + * I.handleDownloads(); |
| 100 | + * I.click('Download as PDF'); |
| 101 | + * I.amInPath('output/downloads'); |
| 102 | + * I.seeFileNameMatching('.pdf'); |
| 103 | + * ``` |
| 104 | + * @param {string} text |
| 105 | + */ |
| 106 | + seeFileNameMatching(text) { |
| 107 | + assert.ok( |
| 108 | + this.grabFileNames().some(file => file.includes(text)), |
| 109 | + `File name which contains ${text} not found in ${this.dir}`, |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Checks that file found by `seeFile` includes a text. |
| 115 | + * @param {string} text |
| 116 | + * @param {string} [encoding='utf8'] |
| 117 | + */ |
| 118 | + seeInThisFile(text, encoding = 'utf8') { |
| 119 | + const content = getFileContents(this.file, encoding); |
| 120 | + fileIncludes(this.file).assert(text, content); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Checks that file found by `seeFile` doesn't include text. |
| 125 | + * @param {string} text |
| 126 | + * @param {string} [encoding='utf8'] |
| 127 | + */ |
| 128 | + dontSeeInThisFile(text, encoding = 'utf8') { |
| 129 | + const content = getFileContents(this.file, encoding); |
| 130 | + fileIncludes(this.file).negate(text, content); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Checks that contents of file found by `seeFile` equal to text. |
| 135 | + * @param {string} text |
| 136 | + * @param {string} [encoding='utf8'] |
| 137 | + */ |
| 138 | + seeFileContentsEqual(text, encoding = 'utf8') { |
| 139 | + const content = getFileContents(this.file, encoding); |
| 140 | + fileEquals(this.file).assert(text, content); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Checks that contents of the file found by `seeFile` equal to contents of the file at `pathToReferenceFile`. |
| 145 | + * @param {string} pathToReferenceFile |
| 146 | + * @param {string} [encoding='utf8'] |
| 147 | + * @param {string} [encodingReference='utf8'] |
| 148 | + */ |
| 149 | + seeFileContentsEqualReferenceFile(pathToReferenceFile, encoding = 'utf8', encodingReference = '') { |
| 150 | + const content = getFileContents(this.file, encoding); |
| 151 | + assert.ok(fileExists(pathToReferenceFile), `Reference file ${pathToReferenceFile} not found.`); |
| 152 | + encodingReference = encodingReference || encoding; |
| 153 | + const expectedContent = getFileContents(pathToReferenceFile, encodingReference); |
| 154 | + fileEquals(this.file).assert(expectedContent, content); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Checks that contents of file found by `seeFile` doesn't equal to text. |
| 159 | + * @param {string} text |
| 160 | + * @param {string} [encoding='utf8'] |
| 161 | + */ |
| 162 | + dontSeeFileContentsEqual(text, encoding = 'utf8') { |
| 163 | + const content = getFileContents(this.file, encoding); |
| 164 | + fileEquals(this.file).negate(text, content); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns file names in current directory. |
| 169 | + * |
| 170 | + * ```js |
| 171 | + * I.handleDownloads(); |
| 172 | + * I.click('Download Files'); |
| 173 | + * I.amInPath('output/downloads'); |
| 174 | + * const downloadedFileNames = I.grabFileNames(); |
| 175 | + * ``` |
| 176 | + */ |
| 177 | + grabFileNames() { |
| 178 | + return fs.readdirSync(this.dir) |
| 179 | + .filter(item => !fs.lstatSync(path.join(this.dir, item)).isDirectory()); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +module.exports = FileSystem; |
| 184 | + |
| 185 | +/** |
| 186 | + * @param {string} file |
| 187 | + * @param {string} [encoding='utf8'] |
| 188 | + * @private |
| 189 | + * @returns {string} |
| 190 | + */ |
| 191 | +function getFileContents(file, encoding = 'utf8') { |
| 192 | + if (!file) assert.fail('No files were opened, please use seeFile action'); |
| 193 | + if (encoding === '') assert.fail('Encoding is an empty string, please set a valid encoding'); |
| 194 | + return fs.readFileSync(file, encoding); |
| 195 | +} |
| 196 | + |
| 197 | +/** |
| 198 | + * @param {string} file |
| 199 | + * @param {number} timeout |
| 200 | + * @private |
| 201 | + * @returns {Promise<any>} |
| 202 | + */ |
| 203 | +function isFileExists(file, timeout) { |
| 204 | + return new Promise(((resolve, reject) => { |
| 205 | + const timer = setTimeout(() => { |
| 206 | + watcher.close(); |
| 207 | + reject(new Error('File did not exists and was not created during the timeout.')); |
| 208 | + }, timeout); |
| 209 | + |
| 210 | + const dir = path.dirname(file); |
| 211 | + const basename = path.basename(file); |
| 212 | + const watcher = fs.watch(dir, (eventType, filename) => { |
| 213 | + if (eventType === 'rename' && filename === basename) { |
| 214 | + clearTimeout(timer); |
| 215 | + watcher.close(); |
| 216 | + resolve(); |
| 217 | + } |
| 218 | + }); |
| 219 | + |
| 220 | + fs.access(file, fs.constants.R_OK, (err) => { |
| 221 | + if (!err) { |
| 222 | + clearTimeout(timer); |
| 223 | + watcher.close(); |
| 224 | + resolve(); |
| 225 | + } |
| 226 | + }); |
| 227 | + })); |
| 228 | +} |
0 commit comments