• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const Path = require('path');
3const { createServer } = require('net');
4
5const { test } = require('tap');
6
7const startCLI = require('./start-cli');
8
9test('launch CLI w/o args', (t) => {
10  const cli = startCLI([]);
11  return cli.quit()
12    .then((code) => {
13      t.equal(code, 1, 'exits with non-zero exit code');
14      t.match(cli.output, /^Usage:/, 'Prints usage info');
15    });
16});
17
18test('launch w/ invalid host:port', (t) => {
19  const cli = startCLI(['localhost:914']);
20  return cli.quit()
21    .then((code) => {
22      t.match(
23        cli.output,
24        'failed to connect',
25        'Tells the user that the connection failed');
26      t.equal(code, 1, 'exits with non-zero exit code');
27    });
28});
29
30test('launch w/ unavailable port', async(t) => {
31  const blocker = createServer((socket) => socket.end());
32  const port = await new Promise((resolve, reject) => {
33    blocker.on('error', reject);
34    blocker.listen(0, '127.0.0.1', () => resolve(blocker.address().port));
35  });
36
37  try {
38    const script = Path.join('examples', 'three-lines.js');
39    const cli = startCLI([`--port=${port}`, script]);
40    const code = await cli.quit();
41
42    t.notMatch(
43      cli.output,
44      'report this bug',
45      'Omits message about reporting this as a bug');
46    t.match(
47      cli.output,
48      `waiting for 127.0.0.1:${port} to be free`,
49      'Tells the user that the port wasn\'t available');
50    t.equal(code, 1, 'exits with non-zero exit code');
51  } finally {
52    blocker.close();
53  }
54});
55