Debian Codegraph @

hasha/test.js

raw
1 import fs from 'fs';
2 import {Writable as WritableStream} from 'stream';
3 import test from 'ava';
4 import isStream from 'is-stream';
5 import proxyquire from 'proxyquire';
6 import hasha from '.';
7
8 test('hasha()', t => {
9 const fixture = Buffer.from('unicorn');
10 t.is(hasha(fixture).length, 128);
11 t.is(hasha('unicorn').length, 128);
12 t.is(hasha(['foo', 'bar']).length, 128);
13 t.is(hasha(['foo', Buffer.from('bar')]), hasha('foobar'));
14 t.true(Buffer.isBuffer(hasha(fixture, {encoding: 'buffer'})));
15 t.is(hasha(fixture, {algorithm: 'md5'}).length, 32);
16 });
17
18 test('hasha.async()', async t => {
19 t.is((await hasha.async(Buffer.from('unicorn'))).length, 128);
20 t.is((await hasha.async('unicorn')).length, 128);
21 t.is((await hasha.async(['foo', 'bar'])).length, 128);
22 t.is(await hasha.async(['foo', Buffer.from('bar')]), hasha('foobar'));
23 t.true(Buffer.isBuffer(await hasha.async(Buffer.from('unicorn'), {encoding: 'buffer'})));
24 t.is((await hasha.async(Buffer.from('unicorn'), {algorithm: 'md5'})).length, 32);
25 });
26
27 test('hasha.stream()', t => {
28 t.true(isStream(hasha.stream()));
29 });
30
31 test('hasha.fromStream()', async t => {
32 t.is((await hasha.fromStream(fs.createReadStream('test.js'))).length, 128);
33 });
34
35 test('crypto error', async t => {
36 const proxied = proxyquire('.', {
37 crypto: {
38 createHash: () => {
39 const stream = new WritableStream();
40 stream._write = function () {
41 this.emit('error', new Error('some crypto error'));
42 };
43
44 stream.setEncoding = () => {};
45 return stream;
46 }
47 }
48 });
49
50 await t.throwsAsync(proxied.fromStream(fs.createReadStream('test.js')), 'some crypto error');
51 });
52
53 test('hasha.fromFile()', async t => {
54 t.is((await hasha.fromFile('test.js')).length, 128);
55 });
56
57 test('hasha.fromFile(non-existent)', async t => {
58 await t.throwsAsync(hasha.fromFile('non-existent-file.txt'), {code: 'ENOENT'});
59 });
60
61 test('hasha.fromFileSync()', t => {
62 t.is(hasha.fromFileSync('test.js').length, 128);
63 });