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
67 changes: 66 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
// write code here
/* eslint-disable no-console */
'use strict';

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

const [, , source, dest] = process.argv;

if (!source) {
console.error('Please provide source path');
}

if (!dest) {
console.error('Please provide destination path');
}

if (source === dest) {
process.exit(0);
}

(async () => {
try {
const srcStat = await fs.stat(source);

if (!srcStat.isFile()) {
console.error('Source is not a file');
}

let target = dest;

if (dest.endsWith('/')) {
const dirStat = await fs.stat(dest);

if (!dirStat.isDirectory()) {
console.error('Destination directory does not exist');
}

target = path.join(dest, path.basename(source));
} else {
try {
const destStat = await fs.stat(dest);

if (destStat.isDirectory()) {
target = path.join(dest, path.basename(source));
}
} catch {
const parentDir = path.dirname(dest);

try {
const parentStat = await fs.stat(parentDir);

if (!parentStat.isDirectory()) {
console.error('Destination directory does not exist');
}
} catch {
console.error('Destination directory does not exist');
}
}
}

await fs.rename(source, target);
console.log(`File moved from "${source}" to "${target}"`);
} catch (err) {
console.error(err.message);
}
})();
3 changes: 3 additions & 0 deletions tests/commercial/authentic_drafty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Numquam universe avarus. Cariosus articulus aliquid. Terra defendo caelestis trado una cupiditas absconditus agnitio depopulo ubi.
Sophismata coniuratio bellicus comptus confido thymbra voluptatibus. Spiritus solum quo cicuta curo terga abutor cupiditas accedo. Vulnero velociter coepi coaegresco itaque atrox sursum colo thesis terra.
Bonus consequatur comptus adstringo asporto tardus. Vis xiphias argentum veritatis beatae attonbitus labore causa terror cursus. Verbera suspendo patior degusto ago articulus.
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