Python test.test_support.temp_cwd() Examples

The following are 30 code examples of test.test_support.temp_cwd(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module test.test_support , or try the search function .
Example #1
Source File: test_ntpath.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_relpath(self):
        tester('ntpath.relpath("a")', 'a')
        tester('ntpath.relpath(os.path.abspath("a"))', 'a')
        tester('ntpath.relpath("a/b")', 'a\\b')
        tester('ntpath.relpath("../a/b")', '..\\a\\b')
        with test_support.temp_cwd(test_support.TESTFN) as cwd_dir:
            currentdir = os.path.basename(cwd_dir)
            tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
            tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
        tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
        tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
        tester('ntpath.relpath("a", "a")', '.')
        tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
        tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
        tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
        tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
        tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
        tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
        tester('ntpath.relpath("/", "/")', '.')
        tester('ntpath.relpath("/a", "/a")', '.')
        tester('ntpath.relpath("/a/b", "/a/b")', '.')
        tester('ntpath.relpath("c:/foo", "C:/FOO")', '.') 
Example #2
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_system_no_site_import(self):
        # If not importing site (-S), importing traceback previously
        # would fail with an import error due to creating a circular
        # import chain. This root cause is because the os module
        # imports the subprocess module for the system function; but
        # the subprocess module imports from os. Verifies that this is
        # managed by making the import late; also verify the
        # monkeypatching optimization is successful by calling
        # os.system twice.
        with test_support.temp_cwd() as temp_cwd:
            self.assertEqual(
                subprocess.check_output(
                    [sys.executable, "-S", "-c",
                     "import traceback; import os; os.system('echo 42'); os.system('echo 47')"])\
                .replace("\r", ""),  # in case of running on Windows
                "42\n47\n") 
Example #3
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_env(self):
        with test_support.temp_cwd(name=u"tempcwd-中文"):
            # os.environ is constructed with FS-encoded values (as in CPython),
            # but it will accept unicode additions.
            newenv = os.environ.copy()
            newenv["TEST_HOME"] = expected = u"首页"
            # Environment passed as UTF-16 String[] by Java, arrives FS-encoded.
            for encoding in ('utf-8', 'gbk'):
                # Emit the value of TEST_HOME explicitly encoded.
                p = subprocess.Popen(
                        [sys.executable, "-c",
                                'import sys, os;' \
                                'sys.stdout.write(os.getenv("TEST_HOME")' \
                                '.decode(sys.getfilesystemencoding())' \
                                '.encode("%s"))' \
                                % encoding],
                        stdout=subprocess.PIPE,
                        env=newenv)
                # Decode with chosen encoding 
                self.assertEqual(p.stdout.read().decode(encoding), u"首页") 
Example #4
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_env_naively(self):
        with test_support.temp_cwd(name=u"tempcwd-中文"):
            # os.environ is constructed with FS-encoded values (as in CPython),
            # but it will accept unicode additions.
            newenv = os.environ.copy()
            newenv["TEST_HOME"] = expected = u"首页"
            # Environment passed as UTF-16 String[] by Java, arrives FS-encoded.
            # However, emit TEST_HOME without thinking about the encoding.
            p = subprocess.Popen(
                    [sys.executable, "-c",
                            'import sys, os;' \
                            'sys.stdout.write(os.getenv("TEST_HOME"))'],
                    stdout=subprocess.PIPE,
                    env=newenv)
            # Decode with default encoding utf-8 (because ... ?)
            self.assertEqual(p.stdout.read().decode('utf-8'), expected) 
Example #5
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_env(self):
        with test_support.temp_cwd(name=u"tempcwd-中文"):
            # os.environ is constructed with FS-encoded values (as in CPython),
            # but it will accept unicode additions.
            newenv = os.environ.copy()
            newenv["TEST_HOME"] = expected = u"首页"
            # Environment passed as UTF-16 String[] by Java, arrives FS-encoded.
            for encoding in ('utf-8', 'gbk'):
                # Emit the value of TEST_HOME explicitly encoded.
                p = subprocess.Popen(
                        [sys.executable, "-c",
                                'import sys, os;' \
                                'sys.stdout.write(os.getenv("TEST_HOME")' \
                                '.decode(sys.getfilesystemencoding())' \
                                '.encode("%s"))' \
                                % encoding],
                        stdout=subprocess.PIPE,
                        env=newenv)
                # Decode with chosen encoding 
                self.assertEqual(p.stdout.read().decode(encoding), u"首页") 
Example #6
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_env_naively(self):
        with test_support.temp_cwd(name=u"tempcwd-中文"):
            # os.environ is constructed with FS-encoded values (as in CPython),
            # but it will accept unicode additions.
            newenv = os.environ.copy()
            newenv["TEST_HOME"] = expected = u"首页"
            # Environment passed as UTF-16 String[] by Java, arrives FS-encoded.
            # However, emit TEST_HOME without thinking about the encoding.
            p = subprocess.Popen(
                    [sys.executable, "-c",
                            'import sys, os;' \
                            'sys.stdout.write(os.getenv("TEST_HOME"))'],
                    stdout=subprocess.PIPE,
                    env=newenv)
            # Decode with default encoding utf-8 (because ... ?)
            self.assertEqual(p.stdout.read().decode('utf-8'), expected) 
Example #7
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_system_uses_os_environ(self):
        """Writing to os.environ is made available as env vars in os.system subprocesses"""
        # This test likely requires additional customization for
        # environments like AS/400, but I do not have current access.
        # Verifies fix for http://bugs.jython.org/issue2416
        if os._name == "nt":
            echo_command = 'echo %TEST_ENVVAR%'
        else:
            echo_command = 'echo $TEST_ENVVAR'
        with test_support.temp_cwd() as temp_cwd:
            self.assertEqual(
                subprocess.check_output(
                    [sys.executable, "-c",
                     "import os; os.environ['TEST_ENVVAR'] = 'works on 2.7.1+'; os.system('%s')" % echo_command])\
                .replace("\r", ""),  # in case of running on Windows
                "works on 2.7.1+\n") 
Example #8
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_system_no_site_import(self):
        # If not importing site (-S), importing traceback previously
        # would fail with an import error due to creating a circular
        # import chain. This root cause is because the os module
        # imports the subprocess module for the system function; but
        # the subprocess module imports from os. Verifies that this is
        # managed by making the import late; also verify the
        # monkeypatching optimization is successful by calling
        # os.system twice.
        with test_support.temp_cwd() as temp_cwd:
            self.assertEqual(
                subprocess.check_output(
                    [sys.executable, "-S", "-c",
                     "import traceback; import os; os.system('echo 42'); os.system('echo 47')"])\
                .replace("\r", ""),  # in case of running on Windows
                "42\n47\n") 
Example #9
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_system_uses_os_environ(self):
        """Writing to os.environ is made available as env vars in os.system subprocesses"""
        # This test likely requires additional customization for
        # environments like AS/400, but I do not have current access.
        # Verifies fix for http://bugs.jython.org/issue2416
        if os._name == "nt":
            echo_command = 'echo %TEST_ENVVAR%'
        else:
            echo_command = 'echo $TEST_ENVVAR'
        with test_support.temp_cwd() as temp_cwd:
            self.assertEqual(
                subprocess.check_output(
                    [sys.executable, "-c",
                     "import os; os.environ['TEST_ENVVAR'] = 'works on 2.7.1+'; os.system('%s')" % echo_command])\
                .replace("\r", ""),  # in case of running on Windows
                "works on 2.7.1+\n") 
Example #10
Source File: test_genericpath.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_abspath_issue3426(self):
        # Check that abspath returns unicode when the arg is unicode
        # with both ASCII and non-ASCII cwds.
        abspath = self.pathmodule.abspath
        for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
            self.assertIsInstance(abspath(path), unicode)

        unicwd = u'\xe7w\xf0'
        try:
            fsencoding = test_support.TESTFN_ENCODING or "ascii"
            unicwd.encode(fsencoding)
        except (AttributeError, UnicodeEncodeError):
            # FS encoding is probably ASCII
            pass
        else:
            with test_support.temp_cwd(unicwd):
                for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
                    self.assertIsInstance(abspath(path), unicode) 
Example #11
Source File: test_genericpath.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_abspath_issue3426(self):
        # Check that abspath returns unicode when the arg is unicode
        # with both ASCII and non-ASCII cwds.
        abspath = self.pathmodule.abspath
        for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
            self.assertIsInstance(abspath(path), unicode)

        unicwd = u'\xe7w\xf0'
        try:
            fsencoding = test_support.TESTFN_ENCODING or "ascii"
            unicwd.encode(fsencoding)
        except (AttributeError, UnicodeEncodeError):
            # FS encoding is probably ASCII
            pass
        else:
            with test_support.temp_cwd(unicwd):
                for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
                    self.assertIsInstance(abspath(path), unicode) 
Example #12
Source File: test_genericpath.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_abspath_issue3426(self):
        # Check that abspath returns unicode when the arg is unicode
        # with both ASCII and non-ASCII cwds.
        abspath = self.pathmodule.abspath
        for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
            self.assertIsInstance(abspath(path), unicode)

        unicwd = u'\xe7w\xf0'
        try:
            fsencoding = test_support.TESTFN_ENCODING or "ascii"
            unicwd.encode(fsencoding)
        except (AttributeError, UnicodeEncodeError):
            # FS encoding is probably ASCII
            pass
        else:
            with test_support.temp_cwd(unicwd):
                for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
                    self.assertIsInstance(abspath(path), unicode) 
Example #13
Source File: test_genericpath.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_abspath_issue3426(self):
        # Check that abspath returns unicode when the arg is unicode
        # with both ASCII and non-ASCII cwds.
        abspath = self.pathmodule.abspath
        for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
            self.assertIsInstance(abspath(path), unicode)

        unicwd = u'\xe7w\xf0'
        try:
            fsencoding = test_support.TESTFN_ENCODING or "ascii"
            unicwd.encode(fsencoding)
        except (AttributeError, UnicodeEncodeError):
            # FS encoding is probably ASCII
            pass
        else:
            with test_support.temp_cwd(unicwd):
                for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
                    self.assertIsInstance(abspath(path), unicode) 
Example #14
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_getcwd(self):
        with test_support.temp_cwd(name=u"tempcwd-中文") as temp_cwd:
            p = subprocess.Popen([sys.executable, "-c",
                                  'import sys,os;' \
                                  'sys.stdout.write(os.getcwd().encode("utf-8"))'],
                                 stdout=subprocess.PIPE)
            self.assertEqual(p.stdout.read().decode("utf-8"), temp_cwd) 
Example #15
Source File: test_compile_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_compileall(self):
        with temp_cwd():
            PACKAGE = os.path.realpath("./greetings")
            PYC_GREETER = os.path.join(PACKAGE, "greeter.pyc")
            PYCLASS_GREETER = os.path.join(PACKAGE, "greeter$py.class")
            PYCLASS_TEST = os.path.join(PACKAGE, "test$py.class")            

            os.mkdir(PACKAGE)
            self.write_code(
                PACKAGE, "greeter.py",
                """
                def greet():
                    print 'Hello world!'
                """)
            self.write_code(
                PACKAGE, "test.py",
                """
                from greeter import greet
                greet()
                """)

            # pretend we have a Python bytecode compiler by touching this file
            open(PYC_GREETER, "a").close()
            
            compileall.compile_dir(PACKAGE, quiet=True)
            self.assertTrue(os.path.exists(PYC_GREETER))     # still exists
            self.assertTrue(os.path.exists(PYCLASS_TEST))    # along with these new compiled files
            self.assertTrue(os.path.exists(PYCLASS_GREETER))

            # verify we can work with just compiled files
            os.unlink(os.path.join(PACKAGE, "greeter.py"))
            self.assertEqual(
                subprocess.check_output([sys.executable, os.path.join(PACKAGE, "test.py")]).rstrip(),
                "Hello world!") 
Example #16
Source File: test_site_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_options_no_site_import(self):
        with test_support.temp_cwd() as temp_cwd:
            self.assertEqual(
                subprocess.check_output(
                    [sys.executable, "-S", "-c",
                     "import sys; print sorted(sys.modules.keys())"]).strip(),
                "['__builtin__', '__main__', 'exceptions', 'sys']") 
Example #17
Source File: test_site_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_property_no_site_import(self):
        # only the minimal set of modules are imported
        with test_support.temp_cwd() as temp_cwd:
            self.assertEqual(
                subprocess.check_output(
                    [sys.executable, "-Dpython.import.site=false", "-c",
                     "import sys; print sorted(sys.modules.keys())"]).strip(),
                "['__builtin__', '__main__', 'exceptions', 'sys']") 
Example #18
Source File: test_site_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_bad_python_home(self):
        # http://bugs.jython.org/issue2283
        with test_support.temp_cwd() as temp_cwd:
            os.makedirs(os.path.join(temp_cwd, "Lib"))
            with self.assertRaises(subprocess.CalledProcessError) as cm:
                subprocess.check_output(
                    [sys.executable, "-Dpython.home=%s" % temp_cwd, "-c",
                     "print 42"],
                    stderr=subprocess.STDOUT)
            self.assertIn(
                'Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site',
                cm.exception.output) 
Example #19
Source File: test_site_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_empty_python_home(self):
        # http://bugs.jython.org/issue2283
        with test_support.temp_cwd() as temp_cwd:
            # using a new directory ensures no Lib/ directory is available
            self.assertEqual(
                subprocess.check_output(
                    [sys.executable, "-Dpython.home=", "-c",
                     "import os; os.system('echo 42'); os.system('echo 47')"])\
                .replace("\r", ""),  # in case of running on Windows
                "42\n47\n") 
Example #20
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_bad_symlink(self):
        with test_support.temp_cwd() as new_cwd:
            target = os.path.join(new_cwd, "target")
            with open(target, "w") as f:
                f.write("TARGET")
            source = os.path.join(new_cwd, "source")
            with self.assertRaises(OSError) as cm:
                os.symlink(source, target)  # reversed args!
            self.assertEqual(cm.exception.errno, errno.EEXIST) 
Example #21
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_link(self):
        with test_support.temp_cwd() as new_cwd:
            target = os.path.join(new_cwd, "target")
            with open(target, "w") as f:
                f.write("TARGET")
            source = os.path.join(new_cwd, "source")
            os.link(target, source)
            with open(source, "r") as f:
                self.assertEqual(f.read(), "TARGET") 
Example #22
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_bad_symlink(self):
        with test_support.temp_cwd() as new_cwd:
            target = os.path.join(new_cwd, "target")
            with open(target, "w") as f:
                f.write("TARGET")
            source = os.path.join(new_cwd, "source")
            with self.assertRaises(OSError) as cm:
                os.symlink(source, target)  # reversed args!
            self.assertEqual(cm.exception.errno, errno.EEXIST) 
Example #23
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_readlink(self):
        with test_support.temp_cwd() as new_cwd:
            target = os.path.join(new_cwd, "target")
            with open(target, "w") as f:
                f.write("TARGET")
            source = os.path.join(new_cwd, "source")
            os.symlink(target, source)
            self.assertEqual(os.readlink(source), target)
            self.assertEqual(os.readlink(unicode(source)), unicode(target))
            self.assertIsInstance(os.readlink(unicode(source)), unicode) 
Example #24
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_readlink_non_symlink(self):
        """os.readlink of a non symbolic link should raise an error"""
        # test for http://bugs.jython.org/issue2292
        with test_support.temp_cwd() as new_cwd:
            target = os.path.join(new_cwd, "target")
            with open(target, "w") as f:
                f.write("TARGET")
            with self.assertRaises(OSError) as cm:
                os.readlink(target)
            self.assertEqual(cm.exception.errno, errno.EINVAL)
            self.assertEqual(cm.exception.filename, target) 
Example #25
Source File: test_os_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_readlink_nonexistent(self):
        with test_support.temp_cwd() as new_cwd:
            nonexistent_file = os.path.join(new_cwd, "nonexistent-file")
            with self.assertRaises(OSError) as cm:
                os.readlink(nonexistent_file)
            self.assertEqual(cm.exception.errno, errno.ENOENT)
            self.assertEqual(cm.exception.filename, nonexistent_file) 
Example #26
Source File: test_genericpath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_nonascii_abspath(self):
        # Test non-ASCII, non-UTF8 bytes in the path.
        with test_support.temp_cwd('\xe7w\xf0'):
            self.test_abspath() 
Example #27
Source File: test_genericpath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_nonascii_abspath(self):
        # Test non-ASCII, non-UTF8 bytes in the path.
        with test_support.temp_cwd('\xe7w\xf0'):
            self.test_abspath() 
Example #28
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_readlink_nonexistent(self):
        with test_support.temp_cwd() as new_cwd:
            nonexistent_file = os.path.join(new_cwd, "nonexistent-file")
            with self.assertRaises(OSError) as cm:
                os.readlink(nonexistent_file)
            self.assertEqual(cm.exception.errno, errno.ENOENT)
            self.assertEqual(cm.exception.filename, nonexistent_file) 
Example #29
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_readlink_non_symlink(self):
        """os.readlink of a non symbolic link should raise an error"""
        # test for http://bugs.jython.org/issue2292
        with test_support.temp_cwd() as new_cwd:
            target = os.path.join(new_cwd, "target")
            with open(target, "w") as f:
                f.write("TARGET")
            with self.assertRaises(OSError) as cm:
                os.readlink(target)
            self.assertEqual(cm.exception.errno, errno.EINVAL)
            self.assertEqual(cm.exception.filename, target) 
Example #30
Source File: test_os_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_readlink(self):
        with test_support.temp_cwd() as new_cwd:
            target = os.path.join(new_cwd, "target")
            with open(target, "w") as f:
                f.write("TARGET")
            source = os.path.join(new_cwd, "source")
            os.symlink(target, source)
            self.assertEqual(os.readlink(source), target)
            self.assertEqual(os.readlink(unicode(source)), unicode(target))
            self.assertIsInstance(os.readlink(unicode(source)), unicode)