# -*- coding: utf-8 -*-
import tempfile
from shutil import rmtree
import os
import subprocess
import pytest


class XvfbProcess(object):

    # Make sure XVFB is running with the same arguments, specifically
    # screen size and color depth.
    def __init__(self, display=None, width=1024, height=800, colors=24):
        if display is None:
            display = os.environ.get('DISPLAY')
        self.display = display
        self.width = width
        self.height = height
        self.colors = colors


class XtermProcessInfo(object):
    def __init__(self, proc, window_id):
        self.proc = proc
        self.window_id = window_id


@pytest.fixture(scope='session')
def xvfb():
    return XvfbProcess()


@pytest.yield_fixture(scope='function')
def xterm_window(xvfb):
    """
    Create an xterm window test fixture.  This fixture opens a new xterm
    window and yields its the X window id.  Upon test completion the xterm
    process is cleaned up.
    :param xvfb:
    :return:
    """
    directory = tempfile.mkdtemp()
    xterm_pipe_path = os.path.join(directory, 'xterm_pipe')
    xterm_proc = None

    try:
        os.mkfifo(xterm_pipe_path)
        xterm_proc = subprocess.Popen([
            'xterm', '-T', 'My window title', '-e',
            'echo "$WINDOWID" > "{}"; bash'.format(xterm_pipe_path)
        ])
        with open(xterm_pipe_path, 'r') as pipe:
            window_id = int(pipe.read())
        yield XtermProcessInfo(proc=xterm_proc, window_id=window_id)
    finally:
        if os.path.exists(directory):
            rmtree(directory)
        if xterm_proc:
            xterm_proc.terminate()


@pytest.fixture(scope='function')
def xdo(xvfb):
    from xdo import Xdo
    return Xdo(xvfb.display)

@@ -87,16 +87,53 @@
 process%0A
+from py._path.local import LocalPath%0A
 import p
@@ -804,24 +804,32 @@
 _window(xvfb
+, tmpdir
 ):%0A    %22%22%22%0A 
@@ -1048,85 +1048,38 @@
     
-directory = tempfile.mkdtemp()%0A    xterm_pipe_path = os.path.join(directory, 
+xterm_pipe_path = tmpdir.join(
 'xte
@@ -1087,16 +1087,24 @@
 m_pipe')
+.strpath
 %0A    xte
@@ -1493,16 +1493,16 @@
 dow_id)%0A
+
     fina
@@ -1510,76 +1510,8 @@
 ly:%0A
-        if os.path.exists(directory):%0A            rmtree(directory)%0A
     

use pytest tmpdir for test dirs