From a26302f8ce41a79d56f5c341b953d788e831ff65 Mon Sep 17 00:00:00 2001 From: ostapenkovitalij Date: Thu, 28 Nov 2024 21:07:49 +0300 Subject: [PATCH] fix(#375): added tests for count function --- package-lock.json | 10 ++++++ package.json | 3 +- test/test_count.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 test/test_count.js diff --git a/package-lock.json b/package-lock.json index 1100405..fb6879f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "commander": "12.1.0", "eo2js": "0.0.7", "fast-xml-parser": "4.5.0", + "mock-fs": "^5.4.1", "node": "23.2.0", "relative": "3.0.2", "semver": "7.6.3", @@ -2959,6 +2960,15 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, + "node_modules/mock-fs": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/mock-fs/-/mock-fs-5.4.1.tgz", + "integrity": "sha512-sz/Q8K1gXXXHR+qr0GZg2ysxCRr323kuN10O7CtQjraJsFDJ4SJ+0I5MzALz7aRp9lHk8Cc/YdsT95h9Ka1aFw==", + "license": "MIT", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", diff --git a/package.json b/package.json index 459b9df..0f2a50d 100644 --- a/package.json +++ b/package.json @@ -31,9 +31,10 @@ ], "dependencies": { "colors": "1.4.0", - "eo2js": "0.0.7", "commander": "12.1.0", + "eo2js": "0.0.7", "fast-xml-parser": "4.5.0", + "mock-fs": "^5.4.1", "node": "23.2.0", "relative": "3.0.2", "semver": "7.6.3", diff --git a/test/test_count.js b/test/test_count.js new file mode 100644 index 0000000..62260d5 --- /dev/null +++ b/test/test_count.js @@ -0,0 +1,81 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +const assert = require('assert'); +const mock = require('mock-fs'); +const {count} = require('../src/mvnw.js'); +const fs = require('fs'); +const path = require('path'); + +describe('count', function() { + beforeEach(() => { + mock({ + 'mainDir': { + 'testDir': { + 'file1.txt': 'content1', + 'folder1': { + 'file2.txt': 'content2', + 'folder2': { + 'file3.txt': 'content3', + 'folder3': { + 'file4.txt': 'content4' + } + } + }, + 'folder4': { + 'file5.txt': 'content5' + } + }, + 'flatDir': { + 'file1.txt': 'content1', + 'file2.txt': 'content2' + } + } + }); + }); + + afterEach(() => { + mock.restore(); + }); + + it('counts all files in a directory tree', () => { + const result = count('mainDir', 0); + assert.strictEqual(result, 7, `Expected to find 5 files, but found ${result}`); + }); + it('returns 0 for an empty directory', () => { + mock({'emptyDir': {}}); // Пустая директория + const result = count('emptyDir', 0); + assert.strictEqual(result, 0, `Expected to find 0 files, but found ${result}`); + }); + it('handles files being deleted during count', async () => { + await fs.promises.unlink(path.join('mainDir', 'testDir', 'folder1', 'file2.txt')); + await fs.promises.unlink(path.join('mainDir', 'flatDir', 'file1.txt')); + + const result = count('mainDir', 0); + assert.ok( + result === 5, + `Expected to find 5 files (handling deletion), but found ${result}` + ); + }); +});