File: test.js

package info (click to toggle)
node-hook-std 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 100 kB
  • ctags: 4
  • sloc: makefile: 2; sh: 2
file content (159 lines) | stat: -rw-r--r-- 2,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import test from 'ava';
import fn from './';

const stdout = process.stdout;
const stderr = process.stderr;

function restore() {
	// This craziness is required because these properties only have getters by default.
	Object.defineProperties(process, {
		stdout: {
			configurable: true,
			writable: true,
			value: stdout
		},
		stderr: {
			configurable: true,
			writable: true,
			value: stderr
		}
	});
}

test.beforeEach(restore);
test.afterEach(restore);

test.serial.cb('hook stdout & stderr', t => {
	t.plan(2);

	let i = 0;

	const unhook = fn({silent: true}, str => {
		if (str === 'foo' || str === 'bar') {
			t.pass();
		}

		if (++i === 2) {
			unhook();
			t.end();
		}
	});

	process.stdout.write('foo');
	process.stderr.write('bar');
});

test.serial.cb('hook stdout', t => {
	t.plan(1);

	const unhook = fn.stdout({silent: true}, str => {
		t.is(str, 'foo');
		unhook();
		t.end();
	});

	process.stdout.write('foo');
});

test.serial.cb('hook stderr', t => {
	t.plan(1);

	const unhook = fn.stderr({silent: true}, str => {
		t.is(str, 'foo');
		unhook();
		t.end();
	});

	process.stderr.write('foo');
});

function loggingWrite(log, retVal) {
	return (...items) => {
		while (items[items.length - 1] === undefined) {
			items.pop();
		}

		log.push(items);

		return retVal();
	};
}

test.serial('passes through the return value of the underlying write call', t => {
	const log = [];
	let returnValue = false;

	process.stdout = {
		write: loggingWrite(log, () => returnValue)
	};

	fn.stdout(str => str);

	t.false(process.stdout.write('foo'));
	returnValue = true;
	t.true(process.stdout.write('bar'));
	t.same(log, [['foo'], ['bar']]);
});

test.serial('if silent, returns true by default', t => {
	const log = [];

	process.stdout = {
		write: () => t.fail()
	};

	fn.stdout({silent: true}, str => {
		log.push(str);
		return str;
	});

	t.true(process.stdout.write('foo'));
	t.same(log, ['foo']);
});

test.serial('if silent, callback can return a boolean', t => {
	const log = [];
	let returnValue = true;

	process.stdout = {
		write: () => t.fail()
	};

	fn.stdout({silent: true}, str => {
		log.push(str);
		return returnValue;
	});

	t.true(process.stdout.write('foo'));
	returnValue = false;
	t.false(process.stdout.write('bar'));
	t.same(log, ['foo', 'bar']);
});

test.serial('callback can return a buffer', t => {
	const log = [];

	process.stdout = {
		write: loggingWrite(log, () => true)
	};

	fn.stdout(str => new Buffer(str));

	t.true(process.stdout.write('foo'));
	t.true(process.stdout.write('bar'));
	t.same(log, [[new Buffer('foo')], [new Buffer('bar')]]);
});

test.serial('callback receives encoding type', t => {
	const log = [];

	process.stdout = {
		write: () => t.fail()
	};

	fn.stdout({silent: true}, loggingWrite(log, () => true));

	t.true(process.stdout.write('a9fe', 'hex'));
	t.true(process.stdout.write('a234', 'hex'));
	t.same(log, [['a9fe', 'hex'], ['a234', 'hex']]);
});