Task 4.
Clamscan is an open source Node.js tool to scan files and directories for viruses. You can see a code fragment related to scanDir() function, which asynchronously scans a directory and its subdirectories for viruses. Upon a call to scanDir(), Line 10 creates and returns a promise(let's call it p) that will be resolved with the scanned files. The callback function for p tries collecting the names of all files in the given path (line 12), and then passes the filenames to scanFiles() function (line 13). scanFiles() scans an array of files, and returns a promise that is resolved with an object, containing an array of goodFiles and viruses. If there is a problem in acquiring filenames, or scanning files, p will be rejected with an error, indicating the problem and error message (lines 15 to 18). We also provided with the test suite related to this code fragment. It contains two tests, testing success and failure scenarios respectively. However, when executing the test suite, the first test case (success scenario) fails with no logs about the assertions, indicating that none of the expect() statements have been executed. Can you explain what could have caused the issues? * * It is ensured that scanFiles() and getAllFilenames() functions work as expected and are properly tested. |
|
Source Code:
|
|
/** * Scans an entire directory. * * @param {string} path - The directory to scan files of * @returns {Promise |
|
Tests:
|
|
describe('scanDir', () => { before(async () => { // initializes following directory structure: // - good_dir/ // - sub_dir1/ // - good_file1.txt // - good_file2.txt // - sub_dir2/ // - good_file3.txt initializeDirectories() }); it('should support scanning a directory', () => { scanDir(`${__dirname}/good_dir`).then((data) => { expect(data.to.be.an('object')) expect(data.goodFiles.to.be.an('array').that.is.not.empty) expect(data.badFiles.to.be.an('array').that.is.empty) }); }); it('should return error if directory not found', () => { expect(scanDir(`${__dirname}/missing_dir`)).to.be.rejectedWith(Error); }); }); |