2 * Copyright (c) 2017, Daniel Imms (MIT License).
3 * Copyright (c) 2018, Microsoft Corporation (MIT License).
6 import { UnixTerminal } from './unixTerminal';
7 import * as assert from 'assert';
8 import * as path from 'path';
9 import { pollUntil } from './testUtils.test';
11 const FIXTURES_PATH = path.normalize(path.join(__dirname, '..', 'fixtures', 'utf8-character.txt'));
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', [], {});
19 if (process.platform === 'linux') {
20 // https://linux.die.net/man/4/pts
21 regExp = /^\/dev\/pts\/\d+$/;
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]+$/;
28 assert.ok(regExp.test((<any>term)._pty), '"' + (<any>term)._pty + '" should match ' + regExp.toString());
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');
42 it('should return a Buffer when encoding is null', (done) => {
43 const term = new UnixTerminal('/bin/bash', [ '-c', `cat "${FIXTURES_PATH}"` ], {
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]);
54 it('should support other encodings', (done) => {
55 const text = 'test æ!';
56 const term = new UnixTerminal(null, ['-c', 'echo "' + text + '"'], {
60 term.on('data', (data) => {
61 assert.equal(typeof data, 'string');
64 term.on('exit', () => {
65 assert.equal(Buffer.alloc(8, buffer, 'base64').toString().replace('\r', '').replace('\n', ''), text);
71 describe('open', () => {
72 let term: UnixTerminal;
77 term.master.destroy();
81 it('should open a pty with access to a master and slave socket', (done) => {
82 let doneCalled = false;
83 term = UnixTerminal.open({});
86 term.slave.on('data', (data) => {
91 term.master.on('data', (data) => {
96 if (masterbuf === 'slave\r\nmaster\r\n' && slavebuf === 'master\n') {
103 term.slave.write('slave\n');
104 term.master.write('master\n');