From c3beecd01ed53fbdafb8aedcb33da471da6adff6 Mon Sep 17 00:00:00 2001 From: Kavya-24 Date: Mon, 4 Jul 2022 11:27:59 +0530 Subject: [PATCH] feat: Add success, fail messages to commands --- src/commands/alias/export.js | 6 +- src/commands/alias/import.js | 2 +- src/commands/alias/list.js | 1 + src/commands/alias/setup.js | 1 - src/commands/alias/use.js | 11 +- .../FileSnapshot/FilesystemStorage.js | 1 - src/utilities/FileUtility.js | 22 ++- src/utilities/InquirerPrompts.js | 4 +- .../{MockPrompt.js => MockPrompts.js} | 1 + test/commands/add.test.js | 9 +- test/commands/delete.test.js | 8 +- test/commands/export.test.js | 6 +- test/commands/import.test.js | 4 +- test/commands/list.test.js | 146 +++++++++++++----- test/commands/use.test.js | 7 +- 15 files changed, 168 insertions(+), 61 deletions(-) rename src/utilities/{MockPrompt.js => MockPrompts.js} (99%) diff --git a/src/commands/alias/export.js b/src/commands/alias/export.js index 404739a4..c79a8b09 100644 --- a/src/commands/alias/export.js +++ b/src/commands/alias/export.js @@ -15,13 +15,13 @@ class Export extends AliasBaseCommand { return } - // Store the aliases file in the current directory + const filename = 'dataexport.json' const aliasFilePath = new FileUtil(this).getAliasFilePath() - const destFilePath = process.cwd() + '/' + 'dataexport.json' + const destFilePath = process.cwd() + '/' + filename if (new FileUtil(this).pathExists(aliasFilePath)) { const ans = new FileUtil(this).copyFileToDestination(aliasFilePath, destFilePath, 'export') - if (ans) { console.log('Export Completed') } + if (ans) { console.log(`Successfully exported aliases to the file ${filename}`) } } else { new FileUtil(this).setupIncompleteWarning() } diff --git a/src/commands/alias/import.js b/src/commands/alias/import.js index 4096e368..0d35f1ea 100644 --- a/src/commands/alias/import.js +++ b/src/commands/alias/import.js @@ -15,7 +15,7 @@ class Import extends AliasBaseCommand { const destFile = args.dest const ans = new FileUtil(this).copyFileToDestination(destFile, aliasFilePath, 'import') - if (ans) { console.log('Import Completed') } + if (ans) { console.log(`Successfully exported aliases to the file ${destFile}`) } } else { new FileUtil(this).setupIncompleteWarning() } diff --git a/src/commands/alias/list.js b/src/commands/alias/list.js index 654cfa25..22354c56 100644 --- a/src/commands/alias/list.js +++ b/src/commands/alias/list.js @@ -18,6 +18,7 @@ class List extends AliasBaseCommand { const aliasFilePath = new FileUtil(this).getAliasFilePath() if (new FileUtil(this).pathExists(aliasFilePath)) { const db = await List.storage.load(aliasFilePath) + console.log('List of the stored aliases\n') let output = 'Alias\tCommand' for (const alias in db) { diff --git a/src/commands/alias/setup.js b/src/commands/alias/setup.js index e4714d83..62ce62b5 100644 --- a/src/commands/alias/setup.js +++ b/src/commands/alias/setup.js @@ -26,7 +26,6 @@ class Setup extends AliasBaseCommand { const proceed = { move: true } new FileUtil(this).createFolderIfDoesNotExists(aliasFolderPath, proceed) - if (!proceed.move) { return } try { diff --git a/src/commands/alias/use.js b/src/commands/alias/use.js index 7381c82e..404598f4 100644 --- a/src/commands/alias/use.js +++ b/src/commands/alias/use.js @@ -13,28 +13,33 @@ class Use extends AliasBaseCommand { return } - const supposedAlias = this.argv.shift() + let supposedAlias = this.argv.shift() const aliasFilePath = new FileUtil(this).getAliasFilePath() const db = await Use.storage.load(aliasFilePath) const existUtil = new FileUtil(this).extractAlias(supposedAlias, aliasFilePath, db) let commandToRun = supposedAlias + let foundInSuggestions = true if (existUtil.index === -2) { - // Setup incomplete new FileUtil(this).setupIncompleteWarning() return } else if (existUtil.index === -1) { const exitMessage = 'Continue without using' const result = await Use.prompt.findSuggestions(exitMessage, supposedAlias, db) - if (result !== exitMessage) { + if (result === exitMessage) { + foundInSuggestions = false + } else { commandToRun = db[result] + supposedAlias = result } } else if (existUtil.index >= 0) { commandToRun = existUtil.command // + this.argv } + if (foundInSuggestions) { console.log(`Using the command ${commandToRun} from alias ${supposedAlias}`) } + new CommandUtil(this).runCommand(commandToRun, this.argv) } } diff --git a/src/utilities/FileSnapshot/FilesystemStorage.js b/src/utilities/FileSnapshot/FilesystemStorage.js index 7d496883..4408cd02 100644 --- a/src/utilities/FileSnapshot/FilesystemStorage.js +++ b/src/utilities/FileSnapshot/FilesystemStorage.js @@ -23,7 +23,6 @@ class FilesystemStorage { const aliasFolderName = 'alias' const aliasFolderPath = dataDirectory + '/' + aliasFolderName const aliasFileName = 'data.json' - return aliasFolderPath + '/' + aliasFileName } diff --git a/src/utilities/FileUtility.js b/src/utilities/FileUtility.js index fcb65ece..72e390b8 100644 --- a/src/utilities/FileUtility.js +++ b/src/utilities/FileUtility.js @@ -32,19 +32,28 @@ class FileUtility { try { const existUtil = this.extractAlias(userAlias, aliasFilePath, db) const aliasIndex = existUtil.index + let added = false + // This will never run for snapshot based memory reference if (aliasIndex === -2) { return this.setupIncompleteWarning() } else if (aliasIndex === -1) { + // no alias exists. Add is operation is Add, else show error for delete db[userAlias] = userCommand + added = true } else { if (db[userAlias] === 'null' || hasFlag) { db[userAlias] = userCommand + added = true } else { console.log(`alias already exists for command "${db[userAlias]}". Consider adding -f for overwriting`) } } + if (added) { + console.log(`Successfully created alias ${userAlias} for ${userCommand}`) + } + return db } catch (err) { console.log(err) @@ -57,19 +66,30 @@ class FileUtility { const existUtil = this.extractAlias(userAlias, aliasFilePath, db) const aliasIndex = existUtil.index + let deleted = false + + // This will never run for snapshot based memory reference if (aliasIndex === -2) { return this.setupIncompleteWarning() } else if (aliasIndex === -1) { + // no alias exists. Add is operation is Add, else show error for delete const exitMessage = 'Continue without deleting' const result = await FileUtility.prompt.findSuggestions(exitMessage, userAlias, db) if (result === exitMessage) { console.log(`${userAlias} is not a ${this.ctx.config.bin} command.`) } else { - delete db[result] + userAlias = result + delete db[userAlias] + deleted = true } } else { delete db[userAlias] + deleted = true + } + + if (deleted) { + console.log(`Successfully deleted alias ${userAlias}`) } return db diff --git a/src/utilities/InquirerPrompts.js b/src/utilities/InquirerPrompts.js index 2f544b2e..51689f2b 100644 --- a/src/utilities/InquirerPrompts.js +++ b/src/utilities/InquirerPrompts.js @@ -44,7 +44,9 @@ class InquirerPrompts { } }) - return commandIDs.slice(0, numOfSuggestions) + const suggestions = commandIDs.slice(0, numOfSuggestions) + + return suggestions } } module.exports = InquirerPrompts diff --git a/src/utilities/MockPrompt.js b/src/utilities/MockPrompts.js similarity index 99% rename from src/utilities/MockPrompt.js rename to src/utilities/MockPrompts.js index 694f8317..e48035dd 100644 --- a/src/utilities/MockPrompt.js +++ b/src/utilities/MockPrompts.js @@ -9,6 +9,7 @@ class MockPrompts { } constructSuggestions (_userAlias, _db) { + } } module.exports = MockPrompts diff --git a/test/commands/add.test.js b/test/commands/add.test.js index 3b77ea5a..0fb81e51 100644 --- a/test/commands/add.test.js +++ b/test/commands/add.test.js @@ -53,10 +53,11 @@ describe('Tests for adding alias', () => { .stub(Add, 'storage', new MemoryStorage({})) .stub(FileUtil, 'storage', new MemoryStorage({})) .command(['alias:add', 'hello', 'world']) - .it('should add the alias empty', async _ctx => { + .it('should add the alias empty', async ctx => { expect(await Add.storage.load()).to.eql({ hello: 'world' }) + expect(ctx.stdout).to.contain('Successfully created alias hello for world') }) }) @@ -66,11 +67,12 @@ describe('Tests for adding alias', () => { .stub(Add, 'storage', new MemoryStorage({ aliasName: 'aliasCommand' })) .stub(FileUtil, 'storage', new MemoryStorage({ aliasName: 'aliasCommand' })) .command(['alias:add', 'hello', 'world']) - .it('should add the alias', async _ctx => { + .it('should add the alias', async ctx => { expect(await Add.storage.load()).to.eql({ aliasName: 'aliasCommand', hello: 'world' }) + expect(ctx.stdout).to.contain('Successfully created alias hello for world') }) }) @@ -94,10 +96,11 @@ describe('Tests for adding alias', () => { .stub(Add, 'storage', new MemoryStorage({ hello: 'name' })) .stub(FileUtil, 'storage', new MemoryStorage({ hello: 'name' })) .command(['alias:add', 'hello', 'world', '-f']) - .it('should add the alias forcefully', async _ctx => { + .it('should add the alias forcefully', async ctx => { expect(await Add.storage.load()).to.eql({ hello: 'world' }) + expect(ctx.stdout).to.contain('Successfully created alias hello for world') }) }) diff --git a/test/commands/delete.test.js b/test/commands/delete.test.js index 6f001d3b..0d88a066 100644 --- a/test/commands/delete.test.js +++ b/test/commands/delete.test.js @@ -2,7 +2,7 @@ const { expect, test } = require('@oclif/test') const MemoryStorage = require('../../src/utilities/FileSnapshot/MemoryStorage.js') const Delete = require('../../src/commands/alias/delete.js') const FileUtil = require('../../src/utilities/FileUtility') -const MockPrompts = require('../../src/utilities/MockPrompt') +const MockPrompts = require('../../src/utilities/MockPrompts') describe('Tests for deleting alias', () => { describe('Before Setup', () => { @@ -41,10 +41,11 @@ describe('Tests for deleting alias', () => { .stub(Delete, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' })) .stub(FileUtil, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' })) .command(['alias:delete', 'hello']) - .it('should delete the alias', async _ctx => { + .it('should delete the alias', async ctx => { expect(await Delete.storage.load()).to.eql({ hello2: 'world2' }) + expect(ctx.stdout).to.contain('Successfully deleted alias hello') }) }) @@ -72,10 +73,11 @@ describe('Tests for deleting alias', () => { .stub(Delete, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' })) .stub(FileUtil, 'storage', new MemoryStorage({ hello: 'world', hello2: 'world2' })) .command(['alias:delete', 'he']) - .it('should show the suggestions but accepted', async _ctx => { + .it('should show the suggestions but accepted', async ctx => { expect(await Delete.storage.load()).to.eql({ hello2: 'world2' }) + expect(ctx.stdout).to.contain('Successfully deleted alias hello') }) }) }) diff --git a/test/commands/export.test.js b/test/commands/export.test.js index 6cd32a80..0be2b90f 100644 --- a/test/commands/export.test.js +++ b/test/commands/export.test.js @@ -13,7 +13,7 @@ describe('Tests for exporting alias', () => { .stub(Export, 'storage', new MemoryStorage({}, false)) .stub(FileUtil, 'storage', new MemoryStorage({}, false)) .command(['alias:export']) - .it('should throw chalk error', async _ctx => { + .it('should throw chalk error', async ctx => { expect(await FileUtil.storage.load()).to.eql({ }) @@ -57,7 +57,7 @@ describe('Tests for exporting alias', () => { expect(await FileUtil.storage.load()).to.eql({ }) - expect(ctx.stdout).to.contain('Export Completed') + expect(ctx.stdout).to.contain('Successfully exported aliases to the file dataexport.json') const fileStorage = new FilesystemStorage() const db = await fileStorage.load(path) @@ -96,7 +96,7 @@ describe('Tests for exporting alias', () => { hello: 'world', hello1: 'world2' }) - expect(ctx.stdout).to.contain('Export Completed') + expect(ctx.stdout).to.contain('Successfully exported aliases to the file dataexport.json') const fileStorage = new FilesystemStorage() const db = await fileStorage.load(path) expect(db).to.be.a('object') diff --git a/test/commands/import.test.js b/test/commands/import.test.js index cabb648e..eeffd67c 100644 --- a/test/commands/import.test.js +++ b/test/commands/import.test.js @@ -72,7 +72,7 @@ describe('Tests for importing alias', () => { expect(await FileUtil.storage.load()).to.eql({ }) - expect(ctx.stdout).to.contain('Import Completed') + expect(ctx.stdout).to.contain(`Successfully exported aliases to the file ${filename}`) }) after(async function () { @@ -111,7 +111,7 @@ describe('Tests for importing alias', () => { alist: 'alias:list', hello: 'world' }) - expect(ctx.stdout).to.contain('Import Completed') + expect(ctx.stdout).to.contain(`Successfully exported aliases to the file ${filename}`) }) after(async function () { diff --git a/test/commands/list.test.js b/test/commands/list.test.js index 1e39aa2c..eeffd67c 100644 --- a/test/commands/list.test.js +++ b/test/commands/list.test.js @@ -1,79 +1,151 @@ const { expect, test } = require('@oclif/test') const MemoryStorage = require('../../src/utilities/FileSnapshot/MemoryStorage.js') -const List = require('../../src/commands/alias/list.js') +const Import = require('../../src/commands/alias/import') const FileUtil = require('../../src/utilities/FileUtility') +const fs = require('fs') -describe('Tests for listing alias', () => { +describe('Tests for importing alias', () => { describe('Before Setup', () => { - describe('Listing an empty list', () => { + describe('Importing an empty file', () => { + const filename = 'dataexport1.json' + test .stdout() - .stub(List, 'storage', new MemoryStorage({}, false)) + .stub(Import, 'storage', new MemoryStorage({}, false)) .stub(FileUtil, 'storage', new MemoryStorage({}, false)) - .command(['alias:list']) - .it('should throw the chalk error', async _ctx => { - expect(await List.storage.load()).to.eql({ + .command(['alias:import', `${filename}`]) + .it('should throw chalk error', async _ctx => { + expect(await FileUtil.storage.load()).to.eql({ + }) + }) + }) + describe('Importing with invalid file path', () => { + test + .stdout() + .stub(Import, 'storage', new MemoryStorage({}, false, false)) + .stub(FileUtil, 'storage', new MemoryStorage({}, false, false)) + .command(['alias:import', 'hsagfhb.sahdg']) + .it('should throw error that file does not exist', async ctx => { + expect(await FileUtil.storage.load()).to.eql({ }) + expect(ctx.stdout).to.contain('alias file does not exist at the specified path') }) }) - describe('Listing with extra arguments', () => { + describe('Importing without file path', () => { test .stdout() - .stub(List, 'storage', new MemoryStorage({}, false)) + .stub(Import, 'storage', new MemoryStorage({}, false)) .stub(FileUtil, 'storage', new MemoryStorage({}, false)) - .command(['alias:list', 'hello']) - .it('should throw warning for extra argument', async ctx => { - expect(await List.storage.load()).to.eql({ + .command(['alias:import']) + .it('should throw error that file path not provided', async ctx => { + expect(await FileUtil.storage.load()).to.eql({ }) - expect(ctx.stdout).to.contain("Invalid argument 'hello' provided") + expect(ctx.stdout).to.contain('please add the path of the alias.json file') }) }) }) describe('After Setup', () => { - describe('Listing a filled list', () => { + describe('Importing an empty file', () => { + const filename = 'dataexport1.json' + const path = process.cwd() + '/' + filename + + before(async function () { + const db = { + } + + fs.writeFile(path, JSON.stringify(db), err => { + if (err) { + console.log(err) + } + }) + }) + test .stdout() - .stub(List, 'storage', new MemoryStorage({ hello: 'world', alist: 'alias:list' })) - .stub(FileUtil, 'storage', new MemoryStorage({ hello: 'world', alist: 'alias:list' })) - .command(['alias:list']) - .it('should list the filled aliases list', async ctx => { - expect(await List.storage.load()).to.eql({ - hello: 'world', - alist: 'alias:list' + .stub(Import, 'storage', new MemoryStorage({})) + .stub(FileUtil, 'storage', new MemoryStorage({})) + .command(['alias:import', `${filename}`]) + .it('import empty file', async ctx => { + expect(await FileUtil.storage.load()).to.eql({ + }) - expect(ctx.stdout).to.contain('Alias\tCommand\nhello\tworld\nalist\talias:list') + expect(ctx.stdout).to.contain(`Successfully exported aliases to the file ${filename}`) + }) + + after(async function () { + fs.unlink(path, err => { + if (err) { + console.log(err) + } }) + }) }) - describe('Listing an empty list', () => { + describe('Importing file with data', () => { + const filename = 'dataexport2.json' + const path = process.cwd() + '/' + filename + + before(async function () { + const db = { + alist: 'alias:list', + hello: 'world' + } + + fs.writeFile(path, JSON.stringify(db), err => { + if (err) { + console.log(err) + } + }) + }) + test .stdout() - .stub(List, 'storage', new MemoryStorage({})) + .stub(Import, 'storage', new MemoryStorage({})) .stub(FileUtil, 'storage', new MemoryStorage({})) - .command(['alias:list']) - .it('should list the empty aliases list', async ctx => { - expect(await List.storage.load()).to.eql({ + .command(['alias:import', `${filename}`]) + .it('import file with data', async ctx => { + expect(await FileUtil.storage.load()).to.eql({ + alist: 'alias:list', + hello: 'world' + }) + expect(ctx.stdout).to.contain(`Successfully exported aliases to the file ${filename}`) + }) + after(async function () { + fs.unlink(path, err => { + if (err) { + console.log(err) + } + }) + }) + }) + + describe('Importing with invalid file path', () => { + test + .stdout() + .stub(Import, 'storage', new MemoryStorage({}, true, false)) + .stub(FileUtil, 'storage', new MemoryStorage({}, true, false)) + .command(['alias:import', 'hsagfhb.sahdg']) + .it('should throw error that file does not exist', async ctx => { + expect(await FileUtil.storage.load()).to.eql({ }) - expect(ctx.stdout).to.contain('Alias\tCommand') + expect(ctx.stdout).to.contain('alias file does not exist at the specified path') }) }) - describe('Listing with extra arguments', () => { + describe('Importing without file path', () => { test .stdout() - .stub(List, 'storage', new MemoryStorage({ hello: 'world', alist: 'alias:list' })) - .stub(FileUtil, 'storage', new MemoryStorage({ hello: 'world', alist: 'alias:list' })) - .command(['alias:list', 'hello']) - .it('should throw warning for extra argument', async ctx => { - expect(await List.storage.load()).to.eql({ - hello: 'world', - alist: 'alias:list' + .stub(Import, 'storage', new MemoryStorage({})) + .stub(FileUtil, 'storage', new MemoryStorage({})) + .command(['alias:import']) + .it('should throw error that file path not provided', async ctx => { + expect(await FileUtil.storage.load()).to.eql({ }) - expect(ctx.stdout).to.contain("Invalid argument 'hello' provided") + expect(ctx.stdout).to.contain('please add the path of the alias.json file') }) }) }) diff --git a/test/commands/use.test.js b/test/commands/use.test.js index a0ee12a5..9a1fca54 100644 --- a/test/commands/use.test.js +++ b/test/commands/use.test.js @@ -3,7 +3,7 @@ const MemoryStorage = require('../../src/utilities/FileSnapshot/MemoryStorage.js const List = require('../../src/commands/alias/list.js') const Use = require('../../src/commands/alias/use.js') const FileUtil = require('../../src/utilities/FileUtility') -const MockPrompts = require('../../src/utilities/MockPrompt') +const MockPrompts = require('../../src/utilities/MockPrompts') describe('Tests for using alias', () => { describe('Before Setup', () => { @@ -14,7 +14,7 @@ describe('Tests for using alias', () => { .stub(List, 'storage', new MemoryStorage({}, false)) .stub(FileUtil, 'storage', new MemoryStorage({}, false)) .command(['alias:use', 'alist']) - .it('should throw the chalk error', async _ctx => { + .it('should throw the chalk error', async ctx => { expect(await Use.storage.load()).to.eql({ }) }) @@ -63,6 +63,7 @@ describe('Tests for using alias', () => { expect(await Use.storage.load()).to.eql({ alist: 'alias:list' }) + expect(ctx.stdout).to.contain('Using the command alias:list from alias alist') expect(ctx.stdout).to.contain('Alias\tCommand\nalist\talias:list') }) }) @@ -78,6 +79,7 @@ describe('Tests for using alias', () => { expect(await Use.storage.load()).to.eql({ alist: 'alias:list' }) + expect(ctx.stdout).to.contain('Using the command alias:list from alias alist') expect(ctx.stdout).to.contain('Alias\tCommand\nalist\talias:list') }) }) @@ -111,6 +113,7 @@ describe('Tests for using alias', () => { hello: 'world', hello2: 'world2' }) + expect(ctx.stdout).to.contain('Using the command world from alias hello') expect(ctx.stdout).to.contain('command world is not found') }) })