Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 5 additions & 37 deletions lib/helper/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,18 @@ class FileSystem extends Helper {
*/
async waitForFile(name, sec = 1) {
if (sec === 0) assert.fail('Use `seeFile` instead of waiting 0 seconds!')
const waitTimeout = sec * 1000
this.file = path.join(this.dir, name)
try {
this.debugSection('File', this.file)
} catch (e) {
// Fallback debug for ESM transition
console.log(`[File] ${this.file}`)
}
return isFileExists(this.file, waitTimeout).catch(() => {
throw new Error(`file (${name}) still not present in directory ${this.dir} after ${waitTimeout / 1000} sec`)
})
const endTime = Date.now() + sec * 1000
while (!fileExists(this.file)) {
if (endTime < Date.now()) throw new Error(`file (${name}) still not present in directory ${this.dir} after ${sec} sec`)
await new Promise(resolve => setTimeout(resolve, 200))
}
}

/**
Expand Down Expand Up @@ -213,36 +214,3 @@ function getFileContents(file, encoding = 'utf8') {
if (encoding === '') assert.fail('Encoding is an empty string, please set a valid encoding')
return fs.readFileSync(file, encoding)
}

/**
* @param {string} file
* @param {number} timeout
* @private
* @returns {Promise<any>}
*/
function isFileExists(file, timeout) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
watcher.close()
reject(new Error('File did not exists and was not created during the timeout.'))
}, timeout)

const dir = path.dirname(file)
const basename = path.basename(file)
const watcher = fs.watch(dir, (eventType, filename) => {
if (eventType === 'rename' && filename === basename) {
clearTimeout(timer)
watcher.close()
resolve()
}
})

fs.access(file, fs.constants.R_OK, err => {
if (!err) {
clearTimeout(timer)
watcher.close()
resolve()
}
})
})
}
29 changes: 29 additions & 0 deletions test/unit/helper/FileSystem_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,33 @@ describe('FileSystem', () => {
fs.seeFile(outputFilePath)
fs.seeInThisFile(text)
})

describe('#waitForFile', () => {
const waitDir = path.join('data', 'output', 'wait_for_file')

beforeEach(() => {
nodeFs.rmSync(path.join(global.codecept_dir, waitDir), { recursive: true, force: true })
fs.amInPath(waitDir)
})

it('should wait for a file in a directory that does not exist yet', async () => {
setTimeout(() => {
nodeFs.mkdirSync(fs.dir, { recursive: true })
nodeFs.writeFileSync(path.join(fs.dir, 'file.txt'), 'content')
}, 300)
await fs.waitForFile('file.txt', 3)
})

it('should fail after the timeout if the directory does not exist', async () => {
const start = Date.now()
let error
try {
await fs.waitForFile('file.txt', 1)
} catch (err) {
error = err
}
expect(error?.message).to.include('file (file.txt) still not present')
expect(Date.now() - start).to.be.at.least(1000)
})
})
})
Loading