#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pytest
import os
import sys
import shutil
import tempfile

from crossprocess.process import run_script
from crossprocess.objects import SimpleObject


class TestCrossprocess(object):

    def get_script(self, name, extension='.py'):
        """
        helper method to get a fixture script
        :param name: script name without extension
        :param extension: script extension
        """
        return os.path.join(
            os.path.dirname(__file__),
            'fixtures',
            name + extension
        )

    def read_from_stdout(self, result):
        """
        helper method to extract the stdout string
        :param result: the result tuple returned from run_script
        """
        return result[0].decode().strip()

    def copy_script(self, script):
        """
        helper method to create a temporary copy of a given script
        :param script: script to copy
        """
        tmpfile = tempfile.NamedTemporaryFile()
        shutil.copy(script, tmpfile.name)

        return tmpfile

    def test_run_script(self):
        """
        run a simple script
        """
        assert self.get_script('simple') == self.read_from_stdout(
            run_script(self.get_script('simple')))

    def test_run_script_handle_fails(self):
        """
        ensure failing script is handled
        """
        with pytest.raises(Exception):
            run_script(self.get_script('failing'))

    def test_run_script_keeps_sys_path(self):
        """
        cross process sys path
        """
        script = self.get_script('syspath')
        expected = [os.path.dirname(script)] + sys.path
        assert "\n".join(expected).strip() == self.read_from_stdout(
            run_script(script))

    def test_run_script_permits_transient_module(self):
        """
        project module is accessible from a subprocess
        """
        expected = 'transient_module'
        script = self.get_script(expected)

        assert expected == self.read_from_stdout(run_script(script))

    def test_run_copy_of_script(self):
        """
        project module is accessible from a subprocess even for copy
        """
        fscript = self.copy_script(self.get_script('transient_module'))
        expected = os.path.basename(fscript.name)
        assert expected == self.read_from_stdout(run_script(fscript.name))

    def test_run_script_persistence(self):
        """
        object internal persistence
        """
        script = self.get_script('persistent')
        obj = SimpleObject('persistent')
        assert obj.get_name() == self.read_from_stdout(
            run_script(script, obj))

@@ -1593,99 +1593,180 @@
     
-script = self.get_script('syspath')%0A        expected = %5Bos.path.dirname(
+expected = os.path.join(%0A            os.path.dirname(__file__),%0A            'mydir'%0A        )%0A        sys.path.append(expected)%0A        script = self.get_
 script
-)%5D + 
+('
 sys
-.
 path
+')%0A
 %0A   
@@ -1781,38 +1781,19 @@
 ert 
-%22%5Cn%22.join(expected).strip() ==
+expected in
  sel
@@ -1807,37 +1807,24 @@
 from_stdout(
-%0A            
 run_script(s

fix just check one path still exists in sys.path in run script