installed pty
[VSoRC/.git] / node_modules / node-pty / src / unixTerminal.test.ts
1 /**
2  * Copyright (c) 2017, Daniel Imms (MIT License).
3  * Copyright (c) 2018, Microsoft Corporation (MIT License).
4  */
5
6 import { UnixTerminal } from './unixTerminal';
7 import * as assert from 'assert';
8 import * as path from 'path';
9 import { pollUntil } from './testUtils.test';
10
11 const FIXTURES_PATH = path.normalize(path.join(__dirname, '..', 'fixtures', 'utf8-character.txt'));
12
13 if (process.platform !== 'win32') {
14   describe('UnixTerminal', () => {
15     describe('Constructor', () => {
16       it('should set a valid pts name', () => {
17         const term = new UnixTerminal('/bin/bash', [], {});
18         let regExp;
19         if (process.platform === 'linux') {
20           // https://linux.die.net/man/4/pts
21           regExp = /^\/dev\/pts\/\d+$/;
22         }
23         if (process.platform === 'darwin') {
24           // https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man4/pty.4.html
25           regExp = /^\/dev\/tty[p-sP-S][a-z0-9]+$/;
26         }
27         if (regExp) {
28           assert.ok(regExp.test((<any>term)._pty), '"' + (<any>term)._pty + '" should match ' + regExp.toString());
29         }
30       });
31     });
32
33     describe('PtyForkEncodingOption', () => {
34       it('should default to utf8', (done) => {
35         const term = new UnixTerminal('/bin/bash', [ '-c', `cat "${FIXTURES_PATH}"` ]);
36         term.on('data', (data) => {
37           assert.equal(typeof data, 'string');
38           assert.equal(data, '\u00E6');
39           done();
40         });
41       });
42       it('should return a Buffer when encoding is null', (done) => {
43         const term = new UnixTerminal('/bin/bash', [ '-c', `cat "${FIXTURES_PATH}"` ], {
44           encoding: null
45         });
46         term.on('data', (data) => {
47           assert.equal(typeof data, 'object');
48           assert.ok(data instanceof Buffer);
49           assert.equal(0xC3, data[0]);
50           assert.equal(0xA6, data[1]);
51           done();
52         });
53       });
54       it('should support other encodings', (done) => {
55         const text = 'test æ!';
56         const term = new UnixTerminal(null, ['-c', 'echo "' + text + '"'], {
57           encoding: 'base64'
58         });
59         let buffer = '';
60         term.on('data', (data) => {
61           assert.equal(typeof data, 'string');
62           buffer += data;
63         });
64         term.on('exit', () => {
65           assert.equal(Buffer.alloc(8, buffer, 'base64').toString().replace('\r', '').replace('\n', ''), text);
66           done();
67         });
68       });
69     });
70
71     describe('open', () => {
72       let term: UnixTerminal;
73
74       afterEach(() => {
75         if (term) {
76           term.slave.destroy();
77           term.master.destroy();
78         }
79       });
80
81       it('should open a pty with access to a master and slave socket', (done) => {
82         let doneCalled = false;
83         term = UnixTerminal.open({});
84
85         let slavebuf = '';
86         term.slave.on('data', (data) => {
87           slavebuf += data;
88         });
89
90         let masterbuf = '';
91         term.master.on('data', (data) => {
92           masterbuf += data;
93         });
94
95         pollUntil(() => {
96           if (masterbuf === 'slave\r\nmaster\r\n' && slavebuf === 'master\n') {
97             done();
98             return true;
99           }
100           return false;
101         }, 200, 10);
102
103         term.slave.write('slave\n');
104         term.master.write('master\n');
105       });
106     });
107   });
108 }