Skip to content

Commit af5fcb7

Browse files
committed
[HASH]: refactor and closes issue #4
Signed-off-by: ashish <[email protected]>
1 parent f4c857e commit af5fcb7

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

src/commands/hash.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@ export default class Hash extends Command {
1919

2020
static args = [{name: 'string'}]
2121

22+
// only 2 parameters required HASH_TYPE and INPUT_STRING
2223
async run() {
2324
const {args, flags} = this.parse(Hash)
2425

25-
// only 2 parameters required HASH_TYPE and INPUT_STRING
2626
flags.type = this.getHashType(flags) //by default let it be sha1
27-
args.string = this.getInputString(flags,args) // from either -s,-f or args
27+
args.string = Hash.getInputString(this,flags,args) // from either -s,-f or args
2828

29+
//check params after evaluating all
30+
this.checkParameters(flags, args)
2931
this.calculateHash(flags, args)
3032
}
3133

34+
// to check required parameters passed or not
35+
private checkParameters(flags: any, args: any) {
36+
if(args.string == undefined || args.string =="" )
37+
Logger.error(this, 'Input string is empty or undefined')
38+
39+
}
40+
3241
private calculateHash(flags: any, args:any) {
3342
const hashObject = this.getHashObject(flags)
3443
let hashed: string = hashObject.hex(args.string)
@@ -57,16 +66,14 @@ export default class Hash extends Command {
5766
return flags.type || 'sha1'
5867
}
5968

60-
public getInputString(flags: any, args:any) {
69+
static getInputString(thisRef: any ,flags: any, args:any) { //need to make it static so Crypto can use this
6170
// if -s or -f is not passed we will take it from args
62-
let str=''
6371
if (flags.string) //if -s given
64-
str = flags.string
72+
return flags.string
6573
else if (flags.file) {
66-
Logger.info(this, `reading file: ${flags.file}`)
67-
str = Utilities.getStringFromFile(this, flags.file)
74+
Logger.info(thisRef, `reading file: ${flags.file}`)
75+
return Utilities.getStringFromFile(thisRef, flags.file)
6876
} else
69-
str = args.string
70-
return str;
77+
return args.string
7178
}
7279
}

src/utilities/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class Logger {
1111
}
1212
public static error(thisRef: any, message: string) {
1313
signale.error(`${message}`)
14-
thisRef.exit() //added to exit command
14+
thisRef.exit(0) //added to exit command
1515
}
1616

1717
// public static logSuccess(thisRef: any, message: string) {

src/utilities/Utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class Utilities {
55
public static getStringFromFile(thisRef: any, filePath: string) {
66
let fileStr = ''
77
if (!fs.existsSync(filePath)) {
8-
Logger.error(thisRef,`Couldn't find: ${filePath}`) // this will output error and exit command
8+
Logger.error(thisRef,`Could not find file: ${filePath}`) // this will output error and exit command
99
} else {
1010
fileStr = fs.readFileSync(filePath, 'utf8')
1111

test/commands/hash.test.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,93 @@ describe('hash', () => {
44
test
55
.stdout()
66
.command(['hash', 'ashish'])
7-
.it("cdt hash 'ashish'", ctx => {
7+
.it("Check default type -> cdt hash 'ashish'", ctx => {
88
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
99
})
1010

11+
//invalid type
12+
test
13+
.stdout()
14+
.command(['hash', '-t', 'invalid', 'ashish'])
15+
.exit(0)
16+
.it("Invalid type -> cdt hash -t invalid 'ashish'", ctx => {
17+
expect(ctx.stdout).to.contain('Invalid Or Unsupported hash type')
18+
})
19+
20+
//empty string
21+
test
22+
.stdout()
23+
.command(['hash', '-t', 'md5'])
24+
.exit(0)
25+
.it("Empty Input String -> cdt hash -t md5", ctx => {
26+
expect(ctx.stdout).to.contain('Input string is empty or undefined')
27+
})
28+
29+
1130
// passing sha1 as option
1231
test
1332
.stdout()
1433
.command(['hash', 'ashish' , '-t', 'sha1'])
15-
.it("cdt hash 'ashish' -t 'sha1'", ctx => {
34+
.it("Passing Default type:sha -> cdt hash 'ashish' -t 'sha1'", ctx => {
1635
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
1736
})
1837

1938
//overriding string with option -s
2039
test
2140
.stdout()
2241
.command(['hash', '-s', 'ashish'])
23-
.it("cdt hash -s 'ashish'", ctx => {
42+
.it("Passing string with -s flag -> cdt hash -s 'ashish'", ctx => {
2443
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
2544
})
2645

2746
// if both passed then need to take flag value of -s
2847
test
2948
.stdout()
3049
.command(['hash', 'patel', '-s', 'ashish'])
31-
.it("cdt hash 'patel' -s 'ashish'", ctx => {
50+
.it("Overriding argument string with -s flag -> cdt hash 'patel' -s 'ashish'", ctx => {
3251
expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984')
3352
})
3453

3554
//sha256
3655
test
3756
.stdout()
3857
.command(['hash', '-t', 'sha256', 'ashish'])
39-
.it("cdt hash -t sha256 'ashish'", ctx => {
58+
.it("Hash Sha256 -> cdt hash -t sha256 'ashish'", ctx => {
4059
expect(ctx.stdout).to.contain('05d08de271d2773a504b3a30f98df26cccda55689a8dc3514f55d3f247553d2b')
4160
})
4261

4362
//md5
4463
test
4564
.stdout()
4665
.command(['hash', '--type', 'md5', 'ashish'])
47-
.it("cdt hash --type md5 'ashish'", ctx => {
66+
.it("Hash Md5 -> cdt hash --type md5 'ashish'", ctx => {
4867
expect(ctx.stdout).to.contain('7b69ad8a8999d4ca7c42b8a729fb0ffd')
4968
})
5069

5170
//sha512
5271
test
5372
.stdout()
5473
.command(['hash', '--type', 'sha512', 'ashish'])
55-
.it("cdt hash --type sha512 'ashish'", ctx => {
74+
.it("Hash Sha512 -> cdt hash --type sha512 'ashish'", ctx => {
5675
expect(ctx.stdout).to.contain('2e076fcbd0e5dcb29d30da42a554de919864bbc29eaefe6d32c4f6bcb219d77ce5e48dada485dae92c918b10a03f42008c43ca721f06da6efa5fa9a223401907')
5776
})
5877

5978
//rmd160
6079
test
6180
.stdout()
6281
.command(['hash', '--type', 'rmd160', 'ashish'])
63-
.it("cdt hash --type rmd160 'ashish'", ctx => {
82+
.it("Hash rmd160 -> cdt hash --type rmd160 'ashish'", ctx => {
6483
expect(ctx.stdout).to.contain('a85a72b0a240abecdf27f127aa75fd8663d6d5be')
6584
})
6685

6786
//file input - file not found TODO: solve issue #4
68-
/*
6987
test
70-
.stderr()
88+
.stdout()
7189
.command(['hash', '-f', 'test/resources/filenotfound.txt'])
90+
.exit(0)
7291
.it("cdt hash -f 'test/resources/filenotfound.txt'", ctx => {
73-
expect(ctx.stderr).to.contain('Error: reading File')
92+
expect(ctx.stdout).to.contain('Could not find file')
7493
})
75-
*/
7694

7795
//file input
7896
test

0 commit comments

Comments
 (0)