Skip to content

Commit

Permalink
Improved file helper integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Nov 12, 2018
1 parent 101f814 commit 8869a77
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
29 changes: 28 additions & 1 deletion src/tests/integration/helpers/file-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe(FileHelpers.name, (): void => {
it("should get all files recursively", (): void => {
const files = FileHelpers.getFilesFromFolderRecursively(parentFolder, emptyBlackList);
const actualLength = files.length;
const expectedLength = subFolderFiles.length + subFolderFiles.length;
const expectedLength = subFolders.length * subFolderFiles.length;

expect(actualLength).toBe(expectedLength);
});
Expand Down Expand Up @@ -158,6 +158,33 @@ describe(FileHelpers.name, (): void => {
});
});

describe(FileHelpers.getFilesFromFolderRecursively.name, (): void => {
const parentFolder = "parent-folder";
const files = ["file-1", "file-2", "file-3"];

beforeEach((): void => {
mkdirSync(parentFolder);

for (const file of files) {
writeFileSync(join(parentFolder, file), "this is shit", "utf-8");
}
});

afterEach((): void => {
for (const file of files) {
unlinkSync(join(parentFolder, file));
}

rmdirSync(parentFolder);
});

it("should return only files when there are only files in a folder", (): void => {
const actual = FileHelpers.getFilesFromFolderRecursively(parentFolder, []);
const expected = files.length;
expect(actual.length).toBe(expected);
});
});

describe(FileHelpers.getFilesFromFolderRecursively.name, (): void => {
const parentFolder = "parent-folder";
const subFolders = [
Expand Down
60 changes: 31 additions & 29 deletions src/ts/helpers/file-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
import { lstatSync, readdirSync } from "fs";
import { lstatSync, readdirSync, existsSync } from "fs";
import { join } from "path";

export class FileHelpers {
public static getFilesFromFolderRecursively(folderPath: string, blackList: string[], includeFolders?: boolean, actionBeforeEachFile?: (filePath: string) => void): string[] {
try {
let result = [] as string[];
const fileNames = FileHelpers.getFileNamesFromFolder(folderPath);
let result = [] as string[];

if (!existsSync(folderPath)) {
return result;
}

const filteredFileNames = FileHelpers.filterBlackList(fileNames, blackList);
const fileNames = FileHelpers.getFileNamesFromFolder(folderPath);
const filteredFileNames = FileHelpers.filterBlackList(fileNames, blackList);

for (const fileName of filteredFileNames) {
try {
const filePath = join(folderPath, fileName);
for (const fileName of filteredFileNames) {
try {
const filePath = join(folderPath, fileName);

if (actionBeforeEachFile !== undefined) {
actionBeforeEachFile(filePath);
}
if (actionBeforeEachFile !== undefined) {
actionBeforeEachFile(filePath);
}

const stats = lstatSync(filePath);
const stats = lstatSync(filePath);
const isDirecotry = stats.isDirectory();
const isFile = stats.isFile();

if (stats.isDirectory()) {
// treat .app folder as a file because going recursively through the app folder on macOS would cause the scan to take insanely long
if (filePath.endsWith(".app")) {
if (isDirecotry && !isFile) {
// treat .app folder as a file because going recursively through the app folder on macOS would cause the scan to take insanely long
if (filePath.endsWith(".app")) {
result.push(filePath);
} else {
if (includeFolders !== undefined && includeFolders) {
result.push(filePath);
} else {
if (includeFolders !== undefined && includeFolders) {
result.push(filePath);
}
result = result.concat(FileHelpers.getFilesFromFolderRecursively(filePath, blackList, includeFolders, actionBeforeEachFile));
}
} else if (stats.isFile()) {
result.push(filePath);
result = result.concat(FileHelpers.getFilesFromFolderRecursively(filePath, blackList, includeFolders, actionBeforeEachFile));
}
} catch (error) {
continue;
}
if (!isDirecotry && isFile) {
result.push(filePath);
}
} catch (error) {
continue;
}

return result;

} catch (error) {
return [];
}

return result;
}

public static getFilesFromFolder(folderPath: string): string[] {
Expand Down

0 comments on commit 8869a77

Please sign in to comment.