Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

375, race condition error, while counting files #400

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 20 additions & 12 deletions src/mvnw.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,13 @@ function stop() {
}

/**
* Prints mvnw execution status.
* Recursively calculates number of files under a directory.
* @param {String} dir - Directory where to count.
* @param {Integer} curr - Current counter.
* @return {Integer} Total number files.
*/
function print() {
const duration = Date.now() - beginning;
/**
* Recursively calculates number of files under a directory.
* @param {String} dir - Directory where to count.
* @param {Integer} curr - Current counter.
* @return {Integer} Total number files.
*/
function count(dir, curr) {
module.exports.count = function count(dir, curr) {
try {
if (fs.existsSync(dir)) {
for (const f of fs.readdirSync(dir)) {
const next = path.join(dir, f);
Expand All @@ -174,8 +170,21 @@ function print() {
}
}
}
return curr;
} catch (err) {
if (err.code === 'ENOENT') {
console.info(`Directory or file is not found: ${dir}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack exception swallowing doesn't look like a solution to me. Instead, let's try to find the root cause of the problem and fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 After looking through mvnw.js, I could only trace a possible problem with calling start before closing spawn, but I'm not at all sure about this. The questions are:

  • Do you have any ideas about what we should pay attention to?
  • Could this be a bug outside of mvnw.js?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack are you sure the tests you created reproduce the problem? Do you see the error when you run them (without your fix)?

} else {
throw err;
}
}
return curr;
};

/**
* Prints mvnw execution status.
*/
function print() {
const duration = Date.now() - beginning;
let elapsed;
if (duration < 1000) {
elapsed = `${duration}ms`;
Expand All @@ -190,4 +199,3 @@ function print() {
readline.clearLine(process.stdout, 1);
readline.cursorTo(process.stdout, 0);
}

81 changes: 81 additions & 0 deletions test/test_count.js
Original file line number Diff line number Diff line change
@@ -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': {}}); // Пустая директория
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack please, use ONLY English in comments

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'));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trapvpack it's better not to use empty lines inside code blocks: https://www.yegor256.com/2014/11/03/empty-line-code-smell.html

const result = count('mainDir', 0);
assert.ok(
result === 5,
`Expected to find 5 files (handling deletion), but found ${result}`
);
});
});
Loading