Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

added comments for readability #279

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
56 changes: 36 additions & 20 deletions test/unit/directories.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import chalk from "chalk";
import fsExtra from "fs-extra";
import path from "path";
import tempy from "tempy";
import chalk from "chalk"; // Library for colored terminal output
import fsExtra from "fs-extra"; // Extra utilities for file system operations
import path from "path"; // Module for working with file and directory paths
import tempy from "tempy"; // Temporary directory and file creation

import { isDirectoryEmpty, isDirectoryWriteable } from "../../src/helpers/directories";
import { isDirectoryEmpty, isDirectoryWriteable } from "../../src/helpers/directories"; // Importing helper functions for directory checks

// This has to be in sync with the valid files defined "helpers/directories".
// List of valid files and directories to be in sync with 'helpers/directories'.
// Each entry is an array containing a file/directory name and its type ("file" or "directory").
const filesDirsTable: string[][] = [
[".DS_Store", "file"],
[".git", "directory"],
Expand All @@ -28,103 +29,118 @@ const filesDirsTable: string[][] = [
];

describe("directories", function () {
const appName: string = "my-eth-app";
let testDirPath: string;
const appName: string = "my-eth-app"; // Application name used for testing purposes
let testDirPath: string; // Variable to hold the path of the temporary test directory

beforeEach(function () {
testDirPath = tempy.directory();
testDirPath = tempy.directory(); // Create a temporary directory before each test
});

// Test when the directory is empty
describe("when the directory is empty", function () {
test("returns true", function () {
// Expect that an empty directory returns true
expect(isDirectoryEmpty(testDirPath, appName)).toBe(true);
});
});

// Test when the directory is not empty and contains valid files or directories
describe("when the directory is not empty", function () {
// Dynamically test each file/directory in the filesDirsTable
describe.each(filesDirsTable)('when it contains a "%s" %s', function (name: string, type: string) {
beforeEach(async function () {
if (type === "file") {
// If the type is 'file', create an empty file in the directory
await fsExtra.open(path.join(testDirPath, name), "w");
} else if (type === "directory") {
// If the type is 'directory', create a directory
await fsExtra.mkdir(path.join(testDirPath, name));
}
});

test("returns true", async function () {
// Expect that a non-empty directory returns true
expect(isDirectoryEmpty(testDirPath, appName)).toBe(true);
});
});

// Test when the directory contains no valid files or directories
describe("when it contains no valid directories or files", function () {
beforeEach(function () {
console.log = jest.fn();
console.log = jest.fn(); // Mock console.log to check if it gets called
});

// Test when it contains an invalid 'foo' directory
describe("when it contains a foo directory", function () {
beforeEach(async function () {
await fsExtra.mkdir(path.join(testDirPath, "foo"));
await fsExtra.mkdir(path.join(testDirPath, "foo")); // Create a 'foo' directory
});

test("returns conflicts", async function () {
const result: boolean = isDirectoryEmpty(testDirPath, appName);
const result: boolean = isDirectoryEmpty(testDirPath, appName); // Check for conflicts
expect(console.log).toHaveBeenCalledWith(
"The directory " + chalk.green(appName) + " contains files that could conflict:",
);
expect(console.log).toHaveBeenCalledWith();
expect(console.log).toHaveBeenCalledWith(` ${chalk.blue("foo")}/`);
expect(console.log).toHaveBeenCalledWith(` ${chalk.blue("foo")}/`); // Expect 'foo' to be logged as conflicting
expect(console.log).toHaveBeenCalledWith();
expect(console.log).toHaveBeenCalledWith(
"Either try using a new directory name, or remove the files listed above.",
);
expect(console.log).toHaveBeenCalledWith();
expect(result).toBe(false);
expect(result).toBe(false); // Expect that the result is false due to conflicts
});
});

// Test when it contains an invalid 'bar' file
describe("when it contains a bar file", function () {
beforeEach(async function () {
await fsExtra.open(path.join(testDirPath, "bar"), "w");
await fsExtra.open(path.join(testDirPath, "bar"), "w"); // Create a 'bar' file
});

test("returns conflicts", async function () {
const result: boolean = isDirectoryEmpty(testDirPath, appName);
const result: boolean = isDirectoryEmpty(testDirPath, appName); // Check for conflicts
expect(console.log).toHaveBeenCalledWith(
"The directory " + chalk.green(appName) + " contains files that could conflict:",
);
expect(console.log).toHaveBeenCalledWith();
expect(console.log).toHaveBeenCalledWith(" bar");
expect(console.log).toHaveBeenCalledWith(" bar"); // Expect 'bar' to be logged as conflicting
expect(console.log).toHaveBeenCalledWith();
expect(console.log).toHaveBeenCalledWith(
"Either try using a new directory name, or remove the files listed above.",
);
expect(console.log).toHaveBeenCalledWith();
expect(result).toBe(false);
expect(result).toBe(false); // Expect that the result is false due to conflicts
});
});
});
});

// Test when the directory is not writable
describe("when the directory is not writable", function () {
beforeEach(async function () {
await fsExtra.chmod(testDirPath, 0o444);
await fsExtra.chmod(testDirPath, 0o444); // Set directory permissions to read-only
});

test("returns false", async function () {
// Expect the directory to be non-writable
await expect(isDirectoryWriteable(testDirPath)).resolves.toBe(false);
});
});

// Test when the directory is writable
describe("when the directory is writable", function () {
beforeEach(async function () {
await fsExtra.chmod(testDirPath, 0o777);
await fsExtra.chmod(testDirPath, 0o777); // Set directory permissions to writable
});

test("returns true", async function () {
// Expect the directory to be writable
await expect(isDirectoryWriteable(testDirPath)).resolves.toBe(true);
});
});

// Cleanup: Remove the temporary test directory after each test
afterEach(function () {
fsExtra.removeSync(testDirPath);
});
Expand Down