-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtty-allocation.spec.js
More file actions
141 lines (125 loc) · 4.29 KB
/
tty-allocation.spec.js
File metadata and controls
141 lines (125 loc) · 4.29 KB
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
/*
* Tests for TTY allocation in docker exec and compose.
* @file tty-allocation.spec.js
*
* Validates that TTY is only allocated when BOTH stdin and stdout
* are TTYs. When stdout is redirected (e.g. `lando foo > file.txt`),
* TTY should NOT be allocated so that ANSI escape codes are not
* written to the file.
*
* @see https://github.com/lando/core/issues/345
* @see https://github.com/lando/drupal/issues/157
*/
'use strict';
const chai = require('chai');
const expect = chai.expect;
chai.should();
describe('TTY allocation', () => {
// Save originals
const originalStdinIsTTY = process.stdin.isTTY;
const originalStdoutIsTTY = process.stdout.isTTY;
afterEach(() => {
// Restore after each test
process.stdin.isTTY = originalStdinIsTTY;
process.stdout.isTTY = originalStdoutIsTTY;
// Clear require cache so compose.js re-evaluates
delete require.cache[require.resolve('./../lib/compose')];
delete require.cache[require.resolve('./../utils/build-docker-exec')];
});
describe('compose exec (lib/compose.js)', () => {
it('should set noTTY=true when stdout is not a TTY (output redirected)', () => {
process.stdin.isTTY = true;
process.stdout.isTTY = false;
const compose = require('./../lib/compose');
const result = compose.run(
['docker-compose.yml'],
'test_project',
{services: ['web'], cmd: ['echo', 'hello']},
);
// When noTTY is true, the -T flag should be in the command
expect(result.cmd).to.include('-T');
});
it('should set noTTY=false when both stdin and stdout are TTYs', () => {
process.stdin.isTTY = true;
process.stdout.isTTY = true;
const compose = require('./../lib/compose');
const result = compose.run(
['docker-compose.yml'],
'test_project',
{services: ['web'], cmd: ['echo', 'hello']},
);
// When both are TTY, -T should NOT be in the command
expect(result.cmd).to.not.include('-T');
});
it('should set noTTY=true when stdin is not a TTY (non-interactive)', () => {
process.stdin.isTTY = false;
process.stdout.isTTY = true;
const compose = require('./../lib/compose');
const result = compose.run(
['docker-compose.yml'],
'test_project',
{services: ['web'], cmd: ['echo', 'hello']},
);
expect(result.cmd).to.include('-T');
});
it('should set noTTY=true when neither stdin nor stdout is a TTY', () => {
process.stdin.isTTY = false;
process.stdout.isTTY = false;
const compose = require('./../lib/compose');
const result = compose.run(
['docker-compose.yml'],
'test_project',
{services: ['web'], cmd: ['echo', 'hello']},
);
expect(result.cmd).to.include('-T');
});
});
describe('docker exec (utils/build-docker-exec.js)', () => {
it('should not include --tty when stdout is not a TTY', () => {
process.stdin.isTTY = true;
process.stdout.isTTY = false;
const buildDockerExec = require('./../utils/build-docker-exec');
let capturedCmd;
const injected = {
config: {dockerBin: 'docker'},
_config: {dockerBin: 'docker'},
shell: {
sh: (cmd, opts) => {
capturedCmd = cmd;
return Promise.resolve();
},
},
};
const datum = {
id: 'test_container',
cmd: ['echo', 'hello'],
opts: {user: 'www-data', environment: {}},
};
buildDockerExec(injected, 'inherit', datum);
expect(capturedCmd).to.not.include('--tty');
});
it('should include --tty when both stdin and stdout are TTYs', () => {
process.stdin.isTTY = true;
process.stdout.isTTY = true;
const buildDockerExec = require('./../utils/build-docker-exec');
let capturedCmd;
const injected = {
config: {dockerBin: 'docker'},
_config: {dockerBin: 'docker'},
shell: {
sh: (cmd, opts) => {
capturedCmd = cmd;
return Promise.resolve();
},
},
};
const datum = {
id: 'test_container',
cmd: ['echo', 'hello'],
opts: {user: 'www-data', environment: {}},
};
buildDockerExec(injected, 'inherit', datum);
expect(capturedCmd).to.include('--tty');
});
});
});