tmux-python / libtmux

⚙️ Python API / wrapper for tmux
https://libtmux.git-pull.com
MIT License
1.01k stars 104 forks source link

tmux cant find sessions when testing in docker #265

Open FergusFettes opened 4 years ago

FergusFettes commented 4 years ago

Steps to reproduce: test_tmux.py

import libtmux
import pytest

@pytest.fixture
def server():
    server = libtmux.Server()
    return server

def test_can_start_tmux_session(server):
    server.new_session("test")
    assert server.has_session("test")
    server.kill_session("test")

Dockerfile:

FROM ubuntu:18.04

RUN apt-get update && apt install -y tmux python-pip
RUN pip install libtmux pytest

COPY test_tmux.py .
RUN pytest test_tmux.py

then docker build . fails with

        if 'session_id' not in kwargs:
>           raise ValueError('Session requires a `session_id`')
E           ValueError: Session requires a `session_id`

Interestingly, replacing 'ubuntu:18.04' with 'python:3.7-slim' works. I thought this might mean it was an issue with the apt version of pip (which I know is dodgy), but adding a 'pip install --upgrade pip' didn't help.

FergusFettes commented 4 years ago

For a little context, I'm using tmux in my docker image to make it easy to send keys to and occasionally kill a badly-behaving python2 process that I have given up try to treat respectfully. It works wonderfully, but I just tried to unit test it and the tests fail on the first step, as demonstrated above. Not a major issue since the whole thing has been working without tests for a few months now but I though I should bring it to your attention.

samiraguiar commented 4 years ago

I was also having this issue although I was not running inside docker. In my case the locale was wrong and my LANG environment variable was missing the encoding.

I had this:

$ echo $LANG
en_GB
$ python3 -c "import libtmux ; print(libtmux.Server().list_sessions())"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/samir/.local/lib/python3.6/site-packages/libtmux/server.py", line 183, in list_sessions
    return [Session(server=self, **s) for s in self._sessions]
  File "/home/samir/.local/lib/python3.6/site-packages/libtmux/server.py", line 183, in <listcomp>
    return [Session(server=self, **s) for s in self._sessions]
  File "/home/samir/.local/lib/python3.6/site-packages/libtmux/session.py", line 61, in __init__
    raise ValueError('Session requires a `session_id`')
ValueError: Session requires a `session_id`

And managed to fix by correctly setting the LANG variable, e.g.:

$ export LANG=en_GB.UTF-8
$ python3 -c "import libtmux ; print(libtmux.Server().list_sessions())"
[Session($0 0), Session($8 8), Session($9 dev)]

libtmux uses tab characters (\t) as delimiters when parsing the output of tmux list-sessions, and those characters were being replaced by underscores when the encoding was missing.