Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 463305b

Browse files
committed
Merge pull request #35 from frigg/feature/login-option
Add login and tty option in run
2 parents eba3f77 + e0ae53c commit 463305b

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

docker/manager.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
6060
if exc_value:
6161
raise exc_value
6262

63-
def run(self, command, working_directory='', stdin=''):
63+
def run(self, command, working_directory='', stdin='', login=True, tty=True):
6464
"""
6565
Runs the command with docker exec in the given working directory.
6666
@@ -72,6 +72,10 @@ def run(self, command, working_directory='', stdin=''):
7272
paths will become absolute paths.
7373
:type working_directory: str
7474
:type stdin: str
75+
:param login: Will add --login on the bash call.
76+
:type login: boolean
77+
:param tty: Will add -t on the bash call.
78+
:type tty: boolean
7579
:return: A ProcessResult object containing information on the result of the command.
7680
:rtype: ProcessResult
7781
"""
@@ -87,9 +91,11 @@ def run(self, command, working_directory='', stdin=''):
8791
])
8892

8993
result = execute(
90-
'docker exec -i {container} bash -c \'{command}\''.format(
94+
'docker exec -i{tty} {container} bash{login} -c \'{command}\''.format(
9195
envs=env_string,
9296
container=self.container_name,
97+
login=' --login' if login else '',
98+
tty=' -t' if tty else '',
9399
command=command_string.format(
94100
working_directory=working_directory,
95101
command=command,

tests/test_manager.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ def test_quotation_mark_handling(self, mock_run):
7070
docker.run('echo "hi there"')
7171
docker.run("echo 'hi there'")
7272
expected = (
73-
'docker exec -i {} bash -c \'cd ~/ && echo "hi there"\''.format(docker.container_name),
74-
'')
73+
'docker exec -i -t {} bash --login -c \'cd ~/ && echo "hi there"\''.format(
74+
docker.container_name
75+
),
76+
''
77+
)
7578

7679
mock_run.assert_has_calls([mock.call(*expected), mock.call(*expected)])
7780

@@ -81,8 +84,21 @@ def test_env_variables(self, mock_run):
8184
docker = Docker(env_variables={'CI': 1, 'FRIGG': 1})
8285
docker.run('ls')
8386
mock_run.assert_called_once_with(
84-
'docker exec -i {} bash -c \'cd ~/ && CI=1 FRIGG=1 ls\''.format(docker.container_name),
85-
'')
87+
'docker exec -i -t {} bash --login -c \'cd ~/ && CI=1 FRIGG=1 ls\''.format(
88+
docker.container_name
89+
),
90+
''
91+
)
92+
93+
@mock.patch('docker.manager.execute')
94+
@mock.patch('re.search', lambda *x: None)
95+
def test_no_login(self, mock_run):
96+
docker = Docker()
97+
docker.run('ls', login=False)
98+
mock_run.assert_called_once_with(
99+
'docker exec -i -t {} bash -c \'cd ~/ && ls\''.format(docker.container_name),
100+
''
101+
)
86102

87103
@mock.patch('docker.manager.execute')
88104
def test_single_port_mappping(self, mock_run):

0 commit comments

Comments
 (0)