Skip to content
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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
109 changes: 108 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,108 @@
// write code here
/* eslint-disable no-console */
'use strict';

/* I am not mentor to write tests for a plain task with README,
plz notify mentor if you have any suggestions about tests.
Also, writing tests is going out of the README scope. */

const fs = require('fs');
const path = require('path');

function fail(msg) {
console.error(msg);
}
Comment on lines +11 to +13
Copy link

@Anton-Kuchmasov Anton-Kuchmasov Jan 11, 2026

Choose a reason for hiding this comment

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

what's purpose of this function?


function isExistingDir(p) {
return fs.existsSync(p) && fs.statSync(p).isDirectory();
}

function isExistingFile(p) {
return fs.existsSync(p) && fs.statSync(p).isFile();
}

function app() {
const args = process.argv.slice(2);

if (args.length !== 2) {
fail('Expected 2 arguments');

return;
}

const [src, dest] = args;

if (src === dest) {
return;
}

if (!fs.existsSync(src)) {
fail(`Source file does not exist: ${src}`);

return;
}

if (!fs.statSync(src).isFile()) {
fail(`Source must be a file: ${src}`);

return;
}

const srcBase = path.basename(src);

if (dest.endsWith('/')) {
if (!isExistingDir(dest)) {
fail('Wrong destination directory');

return;
}

const finalDest = path.join(dest, srcBase);

if (isExistingFile(finalDest)) {
fs.unlinkSync(finalDest);
}

fs.renameSync(src, finalDest);

return;
}

if (fs.existsSync(dest)) {
const dstStat = fs.statSync(dest);

if (dstStat.isDirectory()) {
const finalDest = path.join(dest, srcBase);

if (isExistingFile(finalDest)) {
fs.unlinkSync(finalDest);
}

fs.renameSync(src, finalDest);

return;
}

if (dstStat.isFile()) {
fs.unlinkSync(dest);
fs.renameSync(src, dest);

return;
}

fail('Wrong destination directory');

return;
}

const parent = path.dirname(dest);

if (!isExistingDir(parent)) {
fail('Wrong destination directory');

return;
}

fs.renameSync(src, dest);
}

app();
61 changes: 41 additions & 20 deletions tests/moveFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,41 @@ describe('File Move Tests', () => {
});

describe('without params', () => {
test('should throw error', async() => {
test('should throw error', async () => {
const { stderr } = await execAsync(basePath);

expect(stderr.length).toBeGreaterThan(0);
});
});

describe('with one param', () => {
test('should throw error', async() => {
test('should throw error', async () => {
const { stderr } = await execAsync(`${basePath} ${testFilePath}`);

expect(stderr.length).toBeGreaterThan(0);
});
});

describe('with two params', () => {
test('if source file does not exist, should throw error', async() => {
const nonExistingFile = path.join(tempDir, faker.system.commonFileName('txt'));
test('if source file does not exist, should throw error', async () => {
const nonExistingFile = path.join(
tempDir,
faker.system.commonFileName('txt'),
);

const { stderr } = await execAsync(`${basePath} ${nonExistingFile} ${testFilePath}`);
const { stderr } = await execAsync(
`${basePath} ${nonExistingFile} ${testFilePath}`,
);

expect(stderr.length).toBeGreaterThan(0);
});

test('should rename a file, if destination is a new filename', async() => {
test('should rename a file, if destination is a new filename', async () => {
const newFilePath = path.join(tempDir, faker.lorem.word());

const { stderr } = await execAsync(`${basePath} ${testFilePath} ${newFilePath}`);
const { stderr } = await execAsync(
`${basePath} ${testFilePath} ${newFilePath}`,
);

expect(stderr).toBeFalsy();

Expand All @@ -73,8 +80,10 @@ describe('File Move Tests', () => {
expect(content).toBe(testContent);
});

test('should do nothing if source and destination are the same', async() => {
const { stderr } = await execAsync(`${basePath} ${testFilePath} ${testFilePath}`);
test('should do nothing if source and destination are the same', async () => {
const { stderr } = await execAsync(
`${basePath} ${testFilePath} ${testFilePath}`,
);

const content = fs.readFileSync(testFilePath, 'utf-8');

Expand All @@ -83,19 +92,23 @@ describe('File Move Tests', () => {
expect(content).toBe(testContent);
});

test('should move file, if passed destination is a file without extension', async() => {
test('should move file, if passed destination is a file without extension', async () => {
const newFilePath = path.join(tempDir, faker.lorem.word());
const { stderr } = await execAsync(`${basePath} ${testFilePath} ${newFilePath}`);
const { stderr } = await execAsync(
`${basePath} ${testFilePath} ${newFilePath}`,
);

expect(stderr).toBeFalsy();
expect(fs.existsSync(newFilePath)).toBe(true);
expect(fs.existsSync(testFilePath)).toBe(false);
});

test('should move file, if passed destination is a directory', async() => {
test('should move file, if passed destination is a directory', async () => {
fs.mkdirSync(testDir);

const { stderr } = await execAsync(`${basePath} ${testFilePath} ${testDir}`);
const { stderr } = await execAsync(
`${basePath} ${testFilePath} ${testDir}`,
);

expect(stderr).toBeFalsy();

Expand All @@ -107,31 +120,39 @@ describe('File Move Tests', () => {
expect(content).toBe(testContent);
});

test('should throw error if destination directory does not exist', async() => {
const nonExistingDir = path.join(tempDir, 'nonExistingDir', faker.word.noun());
test('should throw error if destination directory does not exist', async () => {
const nonExistingDir = path.join(
tempDir,
'nonExistingDir',
faker.word.noun(),
);

const { stderr } = await execAsync(
`${basePath} ${testFilePath} ${nonExistingDir}`
`${basePath} ${testFilePath} ${nonExistingDir}`,
);

expect(stderr.length).toBeGreaterThan(0);
expect(fs.existsSync(nonExistingDir)).toBe(false);
expect(fs.existsSync(testFilePath)).toBe(true);
});

test('should throw error if destination is non-existed directory with fileName', async() => {
const nonExistingDir = path.join(tempDir, 'nonExistingDir', faker.word.noun());
test('should throw error if destination is non-existed directory with fileName', async () => {
const nonExistingDir = path.join(
tempDir,
'nonExistingDir',
faker.word.noun(),
);

const { stderr } = await execAsync(
`${basePath} ${testFilePath} ${nonExistingDir}`
`${basePath} ${testFilePath} ${nonExistingDir}`,
);

expect(stderr.length).toBeGreaterThan(0);
expect(fs.existsSync(nonExistingDir)).toBe(false);
expect(fs.existsSync(testFilePath)).toBe(true);
});

test('should move file to directory path ending with "/" with the same filename', async() => {
test('should move file to directory path ending with "/" with the same filename', async () => {
fs.mkdirSync(testDir);

const newPath = path.join(testDir, '/');
Expand Down