From 9f4e6f308e94113e9bc88e3ca772f5e09ae40e20 Mon Sep 17 00:00:00 2001 From: Saphal Patro Date: Fri, 20 Sep 2019 06:25:03 +0530 Subject: [PATCH 1/4] wrote unit tests for read --- output/items.json | 24 ++++++++- package.json | 1 + src/fileSystem.js | 106 +++++++++++++++++++-------------------- tests/fileSystem.test.js | 20 ++++++-- 4 files changed, 94 insertions(+), 57 deletions(-) diff --git a/output/items.json b/output/items.json index ac6907a4..d81a5d9b 100644 --- a/output/items.json +++ b/output/items.json @@ -1 +1,23 @@ -[{"item_id":1,"name":"Bread","description":"something about the item","quantity":50,"purchase":false},{"item_id":2,"name":"Milk","description":"something about the item","quantity":200,"purchase":false},{"item_id":2,"name":"Cheese","description":"something about the item","quantity":200,"purchase":false}] \ No newline at end of file +[ + { + "item_id": 1, + "name": "Bread", + "description": "something about the item", + "quantity": 50, + "purchase": false + }, + { + "item_id": 2, + "name": "Milk", + "description": "something about the item", + "quantity": 200, + "purchase": false + }, + { + "item_id": 2, + "name": "Cheese", + "description": "something about the item", + "quantity": 200, + "purchase": false + } +] diff --git a/package.json b/package.json index 1c998ce2..b73e3635 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "eslint-plugin-json": "^1.4.0", "fs": "^0.0.2", "is-valid-path": "^0.1.1", + "jshint": "^2.10.2", "lodash": "^4.17.11", "path": "^0.12.7", "path-exists": "^4.0.0", diff --git a/src/fileSystem.js b/src/fileSystem.js index 86ac1898..1017c58f 100644 --- a/src/fileSystem.js +++ b/src/fileSystem.js @@ -7,10 +7,10 @@ import { existsSync, readdirSync, statSync, - readFile, -} from 'fs'; -import isValid from 'is-valid-path'; -import { stripSymbols, getFileName } from './writeFile'; + readFile +} from "fs"; +import isValid from "is-valid-path"; +import { stripSymbols, getFileName } from "./writeFile"; /** * Write in file @@ -20,55 +20,57 @@ import { stripSymbols, getFileName } from './writeFile'; */ // @TODO cover a test case, when dataStr is not an array. // we can just pass a string there -const write = (path, data) => new Promise((resolve) => { - if (!isValid(path)) { - console.log('path is not valid'); - } - - let dataStr; +const write = (path, data) => + new Promise(resolve => { + if (!isValid(path)) { + console.log("path is not valid"); + } - if (typeof data === 'string') { - dataStr = data; - } else { - dataStr = stripSymbols(data); - } + let dataStr; - writeFile(path, dataStr, (err) => { - if (err) { - console.error(err); - resolve(false); + if (typeof data === "string") { + dataStr = data; } else { - console.info(`${path} file generated successfully!`); - resolve(true); + dataStr = stripSymbols(data); } + + writeFile(path, dataStr, err => { + if (err) { + console.error(err); + resolve(false); + } else { + console.info(`${path} file generated successfully!`); + resolve(true); + } + }); }); -}); /** * read() * @param {string} absolutePath * */ -const read = (absolutePath) => new Promise((resolve, reject) => { - console.log(absolutePath); - if (!isValid(absolutePath)) { - console.log('path is invalid'); - } - let dataStr; - readFile(absolutePath, (err, data) => { - if (!err) { - if (data === '') { - console.log(`${absolutePath} returned empty`); - } - console.log(data); - dataStr = JSON.parse(data); - resolve(dataStr); - } else { - console.log(err); - reject(err); +const read = absolutePath => + new Promise((resolve, reject) => { + console.log(absolutePath); + if (!isValid(absolutePath)) { + console.log("path is invalid"); } + let dataStr; + readFile(absolutePath, "utf8", (err, data) => { + if (!err) { + if (data === "") { + console.log(`${absolutePath} returned empty`); + } + dataStr = JSON.parse(data); + console.log(dataStr); + resolve(dataStr); + } else { + console.log(err); + reject(err); + } + }); }); -}); /** * @param {String} folderNamePath @@ -81,7 +83,7 @@ const read = (absolutePath) => new Promise((resolve, reject) => { // there should be another way const save = (folderNamePath, file, fileData, flag) => { if (!isValid(folderNamePath)) { - console.log('path is not valid'); + console.log("path is not valid"); } const fileDataLength = fileData.length; let success = true; @@ -97,16 +99,16 @@ const save = (folderNamePath, file, fileData, flag) => { const result = write(elementPath, fileData[i]); if (!result) { console.log( - `${fileName} is the filename, ` - + `${elementPath} is the elementPath ` - + 'and success is false', + `${fileName} is the filename, ` + + `${elementPath} is the elementPath ` + + "and success is false" ); } success = success && result; } - return new Promise((resolve) => { + return new Promise(resolve => { resolve(success); }); }; @@ -116,19 +118,19 @@ const save = (folderNamePath, file, fileData, flag) => { * @param {string} folderNamePath * */ -const isFolderExists = (folderNamePath) => existsSync(folderNamePath); +const isFolderExists = folderNamePath => existsSync(folderNamePath); /** * @param {string} path * */ -const dirSync = (filepath) => readdirSync(filepath); +const dirSync = filepath => readdirSync(filepath); /** * @param {string} path * */ -const syncStats = (filepath) => statSync(filepath); +const syncStats = filepath => statSync(filepath); /** * @param {String} path @@ -136,9 +138,9 @@ const syncStats = (filepath) => statSync(filepath); */ const makeFolder = (path, file) => { if (!isValid(path)) { - console.log('path is not valid'); + console.log("path is not valid"); } - const suffix = '_elements'; + const suffix = "_elements"; const folderName = file.slice(0, -5) + suffix; const folderNamePath = path + folderName; @@ -148,6 +150,4 @@ const makeFolder = (path, file) => { return folderNamePath; }; -export { - write, read, save, makeFolder, isFolderExists, dirSync, syncStats, -}; +export { write, read, save, makeFolder, isFolderExists, dirSync, syncStats }; diff --git a/tests/fileSystem.test.js b/tests/fileSystem.test.js index 1677c3b7..886a4488 100644 --- a/tests/fileSystem.test.js +++ b/tests/fileSystem.test.js @@ -1,4 +1,4 @@ -import fs from 'fs'; +import fs, { readdirSync } from 'fs'; import { write, save, @@ -15,7 +15,7 @@ const testFullPath = testFolder + testFile; const testFileContent = [{ name: 'Test' }]; // @TODO next step for us will be to extend our test with real cases. -// i.e. instead of passing simple array into test, +// i.e. instead of passing simple array into test, // we can actually run tests with our real files / objects describe('testing function write()', () => { @@ -71,7 +71,7 @@ describe('testing function isFolderExists()', () => { test('test isFolderExists() returns false', async () => { // @TODO lets make a const variable for path to folder and use - // some important folders(few), like `src` or + // some important folders(few), like `src` or // `dist`(sometimes dist are actually removed, cleaned up) const result = isFolderExists('./nofolderxxx'); expect(result).toBe(false); @@ -92,4 +92,18 @@ describe('testing function read()', () => { // removed test file fs.unlinkSync(testFullPath); }); + + test('testing with output', async () => { + const outputFiles = readdirSync('./output/'); + let testPath; let content; let + result; + for (const file of outputFiles) { + if (file !== 'undefined.json') { + testPath = `./output/${file}`; + content = JSON.parse(fs.readFileSync(testPath)); + result = await read(testPath); + expect(result).toStrictEqual(content); + } + } + }); }); From edd93634a33c33a9e131278021956e0fab3b6c0f Mon Sep 17 00:00:00 2001 From: Saphal Patro Date: Fri, 20 Sep 2019 18:41:02 +0530 Subject: [PATCH 2/4] fixed single quote errors --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index b73e3635..1c998ce2 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,6 @@ "eslint-plugin-json": "^1.4.0", "fs": "^0.0.2", "is-valid-path": "^0.1.1", - "jshint": "^2.10.2", "lodash": "^4.17.11", "path": "^0.12.7", "path-exists": "^4.0.0", From 55cb3685f4b0e8d87e4ccc67aad1c0f3b7fe8cc4 Mon Sep 17 00:00:00 2001 From: Saphal Patro Date: Fri, 20 Sep 2019 18:43:00 +0530 Subject: [PATCH 3/4] fixed double quote errors in fileSystem.js --- src/fileSystem.js | 106 +++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/src/fileSystem.js b/src/fileSystem.js index 1017c58f..140c5f91 100644 --- a/src/fileSystem.js +++ b/src/fileSystem.js @@ -7,10 +7,10 @@ import { existsSync, readdirSync, statSync, - readFile -} from "fs"; -import isValid from "is-valid-path"; -import { stripSymbols, getFileName } from "./writeFile"; + readFile, +} from 'fs'; +import isValid from 'is-valid-path'; +import { stripSymbols, getFileName } from './writeFile'; /** * Write in file @@ -20,57 +20,55 @@ import { stripSymbols, getFileName } from "./writeFile"; */ // @TODO cover a test case, when dataStr is not an array. // we can just pass a string there -const write = (path, data) => - new Promise(resolve => { - if (!isValid(path)) { - console.log("path is not valid"); - } +const write = (path, data) => new Promise((resolve) => { + if (!isValid(path)) { + console.log('path is not valid'); + } - let dataStr; + let dataStr; - if (typeof data === "string") { - dataStr = data; + if (typeof data === 'string') { + dataStr = data; + } else { + dataStr = stripSymbols(data); + } + + writeFile(path, dataStr, (err) => { + if (err) { + console.error(err); + resolve(false); } else { - dataStr = stripSymbols(data); + console.info(`${path} file generated successfully!`); + resolve(true); } - - writeFile(path, dataStr, err => { - if (err) { - console.error(err); - resolve(false); - } else { - console.info(`${path} file generated successfully!`); - resolve(true); - } - }); }); +}); /** * read() * @param {string} absolutePath * */ -const read = absolutePath => - new Promise((resolve, reject) => { - console.log(absolutePath); - if (!isValid(absolutePath)) { - console.log("path is invalid"); - } - let dataStr; - readFile(absolutePath, "utf8", (err, data) => { - if (!err) { - if (data === "") { - console.log(`${absolutePath} returned empty`); - } - dataStr = JSON.parse(data); - console.log(dataStr); - resolve(dataStr); - } else { - console.log(err); - reject(err); +const read = (absolutePath) => new Promise((resolve, reject) => { + console.log(absolutePath); + if (!isValid(absolutePath)) { + console.log('path is invalid'); + } + let dataStr; + readFile(absolutePath, 'utf8', (err, data) => { + if (!err) { + if (data === '') { + console.log(`${absolutePath} returned empty`); } - }); + dataStr = JSON.parse(data); + console.log(dataStr); + resolve(dataStr); + } else { + console.log(err); + reject(err); + } }); +}); /** * @param {String} folderNamePath @@ -83,7 +81,7 @@ const read = absolutePath => // there should be another way const save = (folderNamePath, file, fileData, flag) => { if (!isValid(folderNamePath)) { - console.log("path is not valid"); + console.log('path is not valid'); } const fileDataLength = fileData.length; let success = true; @@ -99,16 +97,16 @@ const save = (folderNamePath, file, fileData, flag) => { const result = write(elementPath, fileData[i]); if (!result) { console.log( - `${fileName} is the filename, ` + - `${elementPath} is the elementPath ` + - "and success is false" + `${fileName} is the filename, ` + + `${elementPath} is the elementPath ` + + 'and success is false', ); } success = success && result; } - return new Promise(resolve => { + return new Promise((resolve) => { resolve(success); }); }; @@ -118,19 +116,19 @@ const save = (folderNamePath, file, fileData, flag) => { * @param {string} folderNamePath * */ -const isFolderExists = folderNamePath => existsSync(folderNamePath); +const isFolderExists = (folderNamePath) => existsSync(folderNamePath); /** * @param {string} path * */ -const dirSync = filepath => readdirSync(filepath); +const dirSync = (filepath) => readdirSync(filepath); /** * @param {string} path * */ -const syncStats = filepath => statSync(filepath); +const syncStats = (filepath) => statSync(filepath); /** * @param {String} path @@ -138,9 +136,9 @@ const syncStats = filepath => statSync(filepath); */ const makeFolder = (path, file) => { if (!isValid(path)) { - console.log("path is not valid"); + console.log('path is not valid'); } - const suffix = "_elements"; + const suffix = '_elements'; const folderName = file.slice(0, -5) + suffix; const folderNamePath = path + folderName; @@ -150,4 +148,6 @@ const makeFolder = (path, file) => { return folderNamePath; }; -export { write, read, save, makeFolder, isFolderExists, dirSync, syncStats }; +export { + write, read, save, makeFolder, isFolderExists, dirSync, syncStats, +}; From 29b27a60455d2c9de3bd88abac679560146ab840 Mon Sep 17 00:00:00 2001 From: Arthur Tkachenko Date: Fri, 20 Sep 2019 19:44:47 +0300 Subject: [PATCH 4/4] Update fileSystem.test.js --- tests/fileSystem.test.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/fileSystem.test.js b/tests/fileSystem.test.js index 886a4488..91477401 100644 --- a/tests/fileSystem.test.js +++ b/tests/fileSystem.test.js @@ -94,12 +94,15 @@ describe('testing function read()', () => { }); test('testing with output', async () => { + // @ODO later we can use consts from outside for cases like this. const outputFiles = readdirSync('./output/'); - let testPath; let content; let - result; + let testPath, content, result; + for (const file of outputFiles) { if (file !== 'undefined.json') { + // @ODO later we can use consts from outside for cases like this. testPath = `./output/${file}`; + // @TODO long line content = JSON.parse(fs.readFileSync(testPath)); result = await read(testPath); expect(result).toStrictEqual(content);