"""
Testcase for checking argv_emulation
"""
import sys
if (sys.version_info[0] == 2 and sys.version_info[:2] >= (2,7)) or \
        (sys.version_info[0] == 3 and sys.version_info[:2] >= (3,2)):
    import unittest
else:
    import unittest2 as unittest

import subprocess
import shutil
import time
import os
import signal
import py2app

DIR_NAME=os.path.dirname(os.path.abspath(__file__))

if sys.version_info[0] == 2:
    def B(value):
        return value

else:
    def B(value):
        return value.encode('latin1')




class TestArgvEmulation (unittest.TestCase):
    py2app_args = []
    setup_file = "setup.py"
    open_argument = '/usr/bin/ssh'
    app_dir = os.path.join(DIR_NAME, 'argv_app')

    # Basic setup code
    #
    # The code in this block needs to be moved to
    # a base-class.
    @classmethod
    def setUpClass(cls):
        env=os.environ.copy()
        pp = os.path.dirname(os.path.dirname(py2app.__file__))
        if 'PYTHONPATH' in env:
            env['PYTHONPATH'] = pp + ':' + env['PYTHONPATH']
        else:
            env['PYTHONPATH'] = pp

        p = subprocess.Popen([
                sys.executable,
                    cls.setup_file, 'py2app'] + cls.py2app_args,
            cwd = cls.app_dir,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            close_fds=True,
            env=env)
        lines = p.communicate()[0]
        if p.wait() != 0:
            print (lines)
            raise AssertionError("Creating basic_app bundle failed")

    @classmethod
    def tearDownClass(cls):
        if os.path.exists(os.path.join(cls.app_dir, 'build')):
            shutil.rmtree(os.path.join(cls.app_dir, 'build'))

        if os.path.exists(os.path.join(cls.app_dir, 'dist')):
            shutil.rmtree(os.path.join(cls.app_dir, 'dist'))

    #
    # End of setup code
    # 

    def test_basic_start(self):
        self.maxDiff = None
        path = os.path.join( self.app_dir, 'dist/BasicApp.app')

        p = subprocess.Popen(["/usr/bin/open",
            '-a', path])
        exit = p.wait()

        self.assertEqual(exit, 0)

        path = os.path.join( self.app_dir, 'dist/argv.txt')
        for x in range(70):     # Argv emulation can take up-to 60 seconds
            time.sleep(1)
            if os.path.exists(path):
                break

        self.assertTrue(os.path.isfile(path))

        fp = open(path)
        data = fp.read().strip()
        fp.close()

        self.assertEqual(data.strip(), repr([os.path.join(self.app_dir, 'dist/BasicApp.app/Contents/Resources/main.py')]))

    def test_start_with_args(self):
        self.maxDiff = None
        path = os.path.join( self.app_dir, 'dist/BasicApp.app')

        p = subprocess.Popen(["/usr/bin/open",
            '-a', path, self.open_argument])
        exit = p.wait()

        self.assertEqual(exit, 0)

        path = os.path.join( self.app_dir, 'dist/argv.txt')
        for x in range(70):     # Argv emulation can take up-to 60 seconds
            time.sleep(1)
            if os.path.exists(path):
                break

        self.assertTrue(os.path.isfile(path))

        fp = open(path, 'r')
        data = fp.read().strip()
        fp.close()

        self.assertEqual(data.strip(), repr([os.path.join(self.app_dir, 'dist/BasicApp.app/Contents/Resources/main.py'), self.open_argument]))

class TestArgvEmulationWithUrL (TestArgvEmulation):
    py2app_args = []
    setup_file = "setup-with-urlscheme.py"
    open_argument = 'myurl:mycommand'
    app_dir = os.path.join(DIR_NAME, 'argv_app')


if __name__ == "__main__":
    unittest.main()


@@ -3348,16 +3348,925 @@
 ent%5D))%0A%0A
+    def test_start_with_two_args(self):%0A        if not self.open_argument.startswith('/'):%0A            unittest.skip(%22Only relevant for base class%22)%0A%0A        self.maxDiff = None%0A        path = os.path.join( self.app_dir, 'dist/BasicApp.app')%0A%0A        p = subprocess.Popen(%5B%22/usr/bin/open%22,%0A                '-a', path, %22--args%22, %22one%22, %22two%22, %22three%22%5D)%0A        exit = p.wait()%0A%0A        self.assertEqual(exit, 0)%0A%0A        path = os.path.join( self.app_dir, 'dist/argv.txt')%0A        for x in range(70):     # Argv emulation can take up-to 60 seconds%0A            time.sleep(1)%0A            if os.path.exists(path):%0A                break%0A%0A        self.assertTrue(os.path.isfile(path))%0A%0A        fp = open(path, 'r')%0A        data = fp.read().strip()%0A        fp.close()%0A%0A        self.assertEqual(data.strip(), repr(%5Bos.path.join(self.app_dir, 'dist/BasicApp.app/Contents/Resources/main.py'), %22one%22, %22two%22, %22three%22%5D))%0A%0A
 class Te
@@ -4285,17 +4285,17 @@
 ionWithU
-r
+R
 L (TestA

Add test case from issue #41