Skip to content

Commit

Permalink
Removed debug mode on some tests. Added recursive parameter to `get…
Browse files Browse the repository at this point in the history
…Files` utility method. Added some additional items to the gitignore.
  • Loading branch information
kylefarris committed Jul 23, 2024
1 parent 8fcaaac commit f4cb743
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ node_modules

npm-debug.log
tests/clamscan-log
tests/infected/*
tests/bad_scan_dir/*
tests/infected
tests/bad_scan_dir
tests/mixed_scan_dir
tests/good_files_list_tmp.txt
tests/output

Expand Down
27 changes: 21 additions & 6 deletions lib/getFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@
const { resolve } = require('path');
const { readdir } = require('fs').promises;

const getFiles = async (dir) => {
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
/**
* Gets a listing of all files (no directories) within a given path.
* By default, it will retrieve files recursively.
*
* @param {string} dir - The directory to get all files of
* @param {boolean} [recursive=true] - If true (default), get all files recursively; False: only get files directly in path
* @returns {Array} - List of all requested path files
*/
const getFiles = async (dir, recursive = true) => {
const items = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(items.map((item) => {
const res = resolve(dir, item.name);
if (!recursive) {
if (!item.isDirectory()) return res;
return new Promise((resolve) => resolve(null));
}
return item.isDirectory() ? getFiles(res) : res;
}));
return files.flat();
return files.filter(Boolean).flat();

// @todo change to this when package required Node 20+
// const files = await fs.readdir(dir, { recursive: true });
}

module.exports = getFiles;
8 changes: 5 additions & 3 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ describe('isInfected', () => {

it('should be okay when scanning a file with consecutive (or any) spaces in it while doing a local scan', async () => {
// Make sure we're forced to scan locally
clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: true, quarantineInfected: false });
clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: false, quarantineInfected: false });

// Write virus file with spaces in its name
eicarGen.writeFileSpaced();
Expand Down Expand Up @@ -1144,7 +1144,7 @@ describe('scanFiles', () => {
describe('edge cases', () => {
it('should be fine when one of the filenames has consecutive spaces in it when locally scanning', async () => {
// Make sure we're forced to scan locally
clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: true, quarantineInfected: false });
clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: false, quarantineInfected: false });

eicarGen.writeFile();
eicarGen.writeFileSpaced();
Expand Down Expand Up @@ -1255,8 +1255,10 @@ describe('scanDir', () => {
});

it('should supply badFiles array with scanned path when directory has infected files', (done) => {
clamscan.settings.scanRecursively = true;
eicarGen.writeFile();
clamscan.scanDir(badScanDir, (err, goodFiles, badFiles) => {
// if (err) console.error(err);
check(done, () => {
expect(err).to.not.be.instanceof(Error);
expect(badFiles).to.be.an('array');
Expand Down Expand Up @@ -1333,7 +1335,7 @@ describe('scanDir', () => {
describe('edge cases', () => {
it('should work when falling back to local scan and there is a file with consecutive spaces in it', async () => {
// Make sure we're forced to scan locally
clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: true, quarantineInfected: false });
clamscan = await resetClam({ clamdscan: { host: null, port: null, socket: null, localFallback: true }, debugMode: false, quarantineInfected: false });

eicarGen.writeFile();
eicarGen.writeFileSpaced();
Expand Down

0 comments on commit f4cb743

Please sign in to comment.