Skip to content

Commit

Permalink
feat: tests for scanDIr file callback
Browse files Browse the repository at this point in the history
  • Loading branch information
xabg2 committed Nov 27, 2024
1 parent 7d13488 commit d85b51c
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,90 @@ describe('scanDir', () => {
}).timeout(15000);

// TODO: Write tests for file_callback
describe('scanDir with fileCb', () => {
it('should call fileCb for each file when scanRecursively is true', async () => {
let fileCbCallCount = 0;
const fileCb = (err, file, isInfected, viruses) => {
fileCbCallCount += 1;
expect(err).to.be.null;
expect(file).to.be.a('string');
expect(isInfected).to.be.a('boolean');
expect(viruses).to.be.an('array');
};

clamscan.settings.scanRecursively = true;

const { goodFiles, badFiles, viruses } = await clamscan.scanDir(goodScanDir, null, fileCb);

expect(fileCbCallCount).to.equal(goodFiles.length + badFiles.length);
expect(goodFiles).to.be.an('array').that.is.not.empty;
expect(badFiles).to.be.an('array').that.is.empty;
expect(viruses).to.be.an('array').that.is.empty;
});

it('should work with fileCb and detect bad files correctly', (done) => {
eicarGen.writeFile();

let fileCbCallCount = 0;
const fileCb = (err, file, isInfected, viruses) => {
fileCbCallCount += 1;
expect(file).to.be.a('string');
expect(isInfected).to.be.a('boolean');
if (isInfected) {
expect(viruses).to.include.members(['EICAR_TEST_FILE']);
}
};

clamscan.settings.scanRecursively = true;

clamscan.scanDir(
badScanDir,
(err, goodFiles, badFiles, viruses) => {
check(done, () => {
expect(err).to.be.null;
expect(goodFiles).to.be.an('array').that.is.empty;
expect(badFiles).to.be.an('array').that.includes(badScanFile);
expect(viruses).to.include.members(['EICAR_TEST_FILE']);

expect(fileCbCallCount).to.equal(goodFiles.length + badFiles.length);
if (fs.existsSync(badScanFile)) fs.unlinkSync(badScanFile);
});
},
fileCb
);
});

it('should handle errors in fileCb gracefully', async () => {
const fileCb = (err, file, isInfected, viruses) => {
if (file === badScanFile) {
throw new Error('Intentional error in fileCb');
}
};

eicarGen.writeFile();

clamscan.settings.scanRecursively = true;

try {
await clamscan.scanDir(badScanDir, null, fileCb);
} catch (err) {
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('Intentional error in fileCb');
} finally {
if (fs.existsSync(badScanFile)) fs.unlinkSync(badScanFile);
}
});

it('should not fail if fileCb is not provided', async () => {
clamscan.settings.scanRecursively = true;

const { goodFiles, badFiles, viruses } = await clamscan.scanDir(goodScanDir);

expect(goodFiles).to.be.an('array').that.is.not.empty;
expect(badFiles).to.be.an('array').that.is.empty;
expect(viruses).to.be.an('array').that.is.empty;
});
});

describe('edge cases', () => {
it('should work when falling back to local scan and there is a file with consecutive spaces in it', async () => {
Expand Down

0 comments on commit d85b51c

Please sign in to comment.