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

// something new x2

function main() {
const fs = require('fs');
const path = require('path');

if (process.argv.slice(2).length !== 2) {
console.error('Usage: node app.js <file> <newFile>');
// throw new Error('Usage: node app.js <file> <newFile>');

return;
}

const [file, newFile] = process.argv.slice(2);

if (!fs.existsSync(file)) {
console.error(`${file} doesn't exist`);
// throw new Error(`${file} doesn't exist`);

return;
}

const srcStats = fs.statSync(file);

if (!srcStats.isFile()) {
console.error(`${file} must be a File`);
// throw new Error(`${file} must be a File`);

return;
Comment on lines +18 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The code checks that the source path exists but does not verify that the source is a file (not a directory). The task requires the app to support only moving of files, so add a check such as fs.statSync(file).isFile() and error out if it's not a regular file.

Comment on lines +27 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You already call fs.statSync(file) and check isFile(), which is correct. However, this branch currently uses console.error and return. Per checklist items #11 and #12 you must throw an Error when the source is not a regular file. Replace the console.error/return with a thrown Error (you may log first then throw to keep test compatibility).

Comment on lines +27 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When the source exists but is not a regular file you currently console.error(...) and return. Per the checklist you must both log and throw an error so tests can observe the failure. Replace the return with throw new Error('Source is not a regular file: ' + file) (or uncomment the existing commented throw) after the console.error.

}

const destExists = fs.existsSync(newFile);
const stats = destExists ? fs.statSync(newFile) : null;
const fileName = path.basename(file);
const parentDir = path.dirname(newFile);

if (newFile.endsWith('/')) {
if (!destExists) {
console.error(`${newFile} doesn't exist`);
// throw new Error(`${newFile} doesn't exist`);

return;
Comment on lines +39 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When the destination ends with / you require that directory to exist — currently you console.error and return if it doesn't. Per checklist items #3, #10 and #13 this must throw an Error (for example: throw new Error('Destination directory does not exist: ' + newFile)). Consider logging then throwing if tests assert on console output.

Comment on lines +40 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When newFile ends with / and the destination directory does not exist you currently console.error(...) and return. The requirement is to treat missing destination directories as hard failures. After the console.error you should throw new Error('Destination directory does not exist: ' + newFile) (instead of returning).

}

const newPath = path.join(newFile, fileName);

fs.renameSync(file, newPath);

return;
}

if (stats && stats.isDirectory()) {
const newPath = path.join(newFile, fileName);

fs.renameSync(file, newPath);
Comment on lines +54 to +57

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Inside the stats && stats.isDirectory() branch you check parentDir existence (path.dirname(newFile)) which is not the correct condition and is unnecessary because destExists is already true. This can produce misleading errors. Remove or replace this check with clearer logic (you already know newFile exists and is a directory, so you can directly compute the destination path and rename).


return;
}

if (!fs.existsSync(parentDir)) {
console.error(`Invalid destination path: ${newFile}`);
// throw new Error(`Invalid destination path: ${newFile}`);

return;
Comment on lines +62 to +66

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For an invalid parent directory you console.error and return. This case also requires a thrown Error (see checklist #3 and #10). Replace the console.error/return with a thrown Error (or console.error(...) followed by throw new Error(...) to preserve console output for tests).

Comment on lines +62 to +66

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When validating the parent directory of the destination you currently console.error(...) and return if it doesn't exist. According to the requirements this must log and then throw an Error (e.g. throw new Error('Invalid destination path: ' + newFile)) so callers/tests receive a thrown error.

}

fs.renameSync(file, newFile);
}

main();
3 changes: 3 additions & 0 deletions tests/angoratimpani_sympathetic_yahoo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Adulescens cultura defetiscor creptio vestigium velociter dolore. Voveo balbus territo vallum dicta tunc. Celo capio cunctatio undique carmen surgo.
Fugit pectus trucido suus. Cibo votum turba astrum animi angelus copia auditor aut. Tristis nostrum defluo audax substantia.
Vulariter adsum condico corona strenuus ceno cauda arx vallum consuasor. Barba confugo rem cornu barba sonitus corpus. Sordeo constans appello clarus desipio tepidus commodo altus incidunt voluptatum.
3 changes: 3 additions & 0 deletions tests/intentionshallow_circa.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Vesper bene totam angustus aestas. Tracto theologus pecco harum ipsa. Voluptates volo arx.
Capillus molestiae abeo. Tui via curis. Ars corrumpo acceptus curia suffoco praesentium abeo.
Decumbo concido demo absorbeo quidem corrumpo eveniet magnam crebro. Adinventitias cultura utique absens tardus confido asper recusandae. Circumvenio viscus vere deleo barba nulla ventus vetus.
3 changes: 3 additions & 0 deletions tests/publishinglittle.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Veritatis totidem cruentus crinis turba acies cubicularis amplus excepturi. Absque repudiandae thorax maiores ipsum. Facere atqui adimpleo artificiose crebro constans.
Defetiscor talus sperno tristis vesco adeptio summisse aperio. Arbor tolero sopor supellex adipisci nemo cado. Casus adeptio contra tondeo conservo collum in catena.
Dolorem terga auctor desparatus universe succurro umbra sortitus. Balbus valeo animi rerum carcer teres patria alo spiculum. Tendo cohors tribuo trans.
3 changes: 3 additions & 0 deletions tests/sulfurshakily_both.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Combibo taceo dedecor ager a civitas. Crastinus facilis balbus valde coerceo vicissitudo bonus varius viscus. Caveo temptatio vae rerum aestivus denuo decipio tempora deleo.
Angelus vulpes alveus tandem allatus carpo. Curia odit comparo audeo comburo adulescens vita aut. Appello uxor odio demulceo apto ciminatio admoveo.
Spoliatio tergum cenaculum cunabula subito alioqui vespillo teneo amissio. Possimus venustas tametsi copiose crux spero. Verto ipsum aeger.
Loading