From 2184d03604811d7474d3d40c940ffdfa40d4cac5 Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Mon, 30 Nov 2015 22:50:15 +0100 Subject: [PATCH 1/6] test_appdirs: fix tests on windows --- tests/unit/test_appdirs.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/unit/test_appdirs.py b/tests/unit/test_appdirs.py index 4c0710dd4a3..11a5919eabd 100644 --- a/tests/unit/test_appdirs.py +++ b/tests/unit/test_appdirs.py @@ -25,12 +25,14 @@ def _get_win_folder(base): assert _get_win_folder.calls == [pretend.call("CSIDL_LOCAL_APPDATA")] def test_user_cache_dir_osx(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") assert appdirs.user_cache_dir("pip") == "/home/test/Library/Caches/pip" def test_user_cache_dir_linux(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.delenv("XDG_CACHE_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -38,6 +40,7 @@ def test_user_cache_dir_linux(self, monkeypatch): assert appdirs.user_cache_dir("pip") == "/home/test/.cache/pip" def test_user_cache_dir_linux_override(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("XDG_CACHE_HOME", "/home/test/.other-cache") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -45,6 +48,7 @@ def test_user_cache_dir_linux_override(self, monkeypatch): assert appdirs.user_cache_dir("pip") == "/home/test/.other-cache/pip" def test_user_cache_dir_linux_home_slash(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) # Verify that we are not affected by http://bugs.python.org/issue14768 monkeypatch.delenv("XDG_CACHE_HOME") monkeypatch.setenv("HOME", "/") @@ -76,6 +80,7 @@ def _get_win_folder(base): assert _get_win_folder.calls == [pretend.call("CSIDL_COMMON_APPDATA")] def test_site_config_dirs_osx(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") @@ -83,6 +88,7 @@ def test_site_config_dirs_osx(self, monkeypatch): ["/Library/Application Support/pip"] def test_site_config_dirs_linux(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.delenv("XDG_CONFIG_DIRS") monkeypatch.setattr(sys, "platform", "linux2") @@ -92,6 +98,7 @@ def test_site_config_dirs_linux(self, monkeypatch): ] def test_site_config_dirs_linux_override(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("XDG_CONFIG_DIRS", "/spam:/etc:/etc/xdg") monkeypatch.setattr(sys, "platform", "linux2") @@ -142,6 +149,7 @@ def _get_win_folder(base): assert _get_win_folder.calls == [pretend.call("CSIDL_APPDATA")] def test_user_data_dir_osx(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") @@ -149,6 +157,7 @@ def test_user_data_dir_osx(self, monkeypatch): "/home/test/Library/Application Support/pip") def test_user_data_dir_linux(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.delenv("XDG_DATA_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -156,6 +165,7 @@ def test_user_data_dir_linux(self, monkeypatch): assert appdirs.user_data_dir("pip") == "/home/test/.local/share/pip" def test_user_data_dir_linux_override(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("XDG_DATA_HOME", "/home/test/.other-share") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -163,6 +173,7 @@ def test_user_data_dir_linux_override(self, monkeypatch): assert appdirs.user_data_dir("pip") == "/home/test/.other-share/pip" def test_user_data_dir_linux_home_slash(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) # Verify that we are not affected by http://bugs.python.org/issue14768 monkeypatch.delenv("XDG_DATA_HOME") monkeypatch.setenv("HOME", "/") @@ -210,6 +221,7 @@ def _get_win_folder(base): assert _get_win_folder.calls == [pretend.call("CSIDL_APPDATA")] def test_user_config_dir_osx(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") @@ -217,6 +229,7 @@ def test_user_config_dir_osx(self, monkeypatch): "/home/test/Library/Application Support/pip") def test_user_config_dir_linux(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.delenv("XDG_CONFIG_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -224,6 +237,7 @@ def test_user_config_dir_linux(self, monkeypatch): assert appdirs.user_config_dir("pip") == "/home/test/.config/pip" def test_user_config_dir_linux_override(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setenv("XDG_CONFIG_HOME", "/home/test/.other-config") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -231,6 +245,7 @@ def test_user_config_dir_linux_override(self, monkeypatch): assert appdirs.user_config_dir("pip") == "/home/test/.other-config/pip" def test_user_config_dir_linux_home_slash(self, monkeypatch): + monkeypatch.setattr(appdirs, "WINDOWS", False) # Verify that we are not affected by http://bugs.python.org/issue14768 monkeypatch.delenv("XDG_CONFIG_HOME") monkeypatch.setenv("HOME", "/") From 57c6d55a47e0e8d36f187881da12581308bbe8a2 Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Mon, 30 Nov 2015 23:30:39 +0100 Subject: [PATCH 2/6] Monkeypatch os.path --- tests/unit/test_appdirs.py | 40 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/unit/test_appdirs.py b/tests/unit/test_appdirs.py index 11a5919eabd..e562dc274dd 100644 --- a/tests/unit/test_appdirs.py +++ b/tests/unit/test_appdirs.py @@ -1,3 +1,6 @@ +import ntpath +import os +import posixpath import sys import pretend @@ -19,13 +22,15 @@ def _get_win_folder(base): raising=False, ) monkeypatch.setattr(appdirs, "WINDOWS", True) + monkeypatch.setattr(os, "path", ntpath) - assert (appdirs.user_cache_dir("pip").replace("/", "\\") == + assert (appdirs.user_cache_dir("pip") == "C:\\Users\\test\\AppData\\Local\\pip\\Cache") assert _get_win_folder.calls == [pretend.call("CSIDL_LOCAL_APPDATA")] def test_user_cache_dir_osx(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") @@ -33,6 +38,7 @@ def test_user_cache_dir_osx(self, monkeypatch): def test_user_cache_dir_linux(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_CACHE_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -41,6 +47,7 @@ def test_user_cache_dir_linux(self, monkeypatch): def test_user_cache_dir_linux_override(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_CACHE_HOME", "/home/test/.other-cache") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -49,6 +56,7 @@ def test_user_cache_dir_linux_override(self, monkeypatch): def test_user_cache_dir_linux_home_slash(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) # Verify that we are not affected by http://bugs.python.org/issue14768 monkeypatch.delenv("XDG_CACHE_HOME") monkeypatch.setenv("HOME", "/") @@ -71,16 +79,14 @@ def _get_win_folder(base): raising=False, ) monkeypatch.setattr(appdirs, "WINDOWS", True) + monkeypatch.setattr(os, "path", ntpath) - result = [ - e.replace("/", "\\") - for e in appdirs.site_config_dirs("pip") - ] - assert result == ["C:\\ProgramData\\pip"] + assert appdirs.site_config_dirs("pip") == ["C:\\ProgramData\\pip"] assert _get_win_folder.calls == [pretend.call("CSIDL_COMMON_APPDATA")] def test_site_config_dirs_osx(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") @@ -89,6 +95,7 @@ def test_site_config_dirs_osx(self, monkeypatch): def test_site_config_dirs_linux(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_CONFIG_DIRS") monkeypatch.setattr(sys, "platform", "linux2") @@ -99,6 +106,7 @@ def test_site_config_dirs_linux(self, monkeypatch): def test_site_config_dirs_linux_override(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_CONFIG_DIRS", "/spam:/etc:/etc/xdg") monkeypatch.setattr(sys, "platform", "linux2") @@ -124,8 +132,9 @@ def _get_win_folder(base): raising=False, ) monkeypatch.setattr(appdirs, "WINDOWS", True) + monkeypatch.setattr(os, "path", ntpath) - assert (appdirs.user_data_dir("pip").replace("/", "\\") == + assert (appdirs.user_data_dir("pip") == "C:\\Users\\test\\AppData\\Local\\pip") assert _get_win_folder.calls == [pretend.call("CSIDL_LOCAL_APPDATA")] @@ -141,15 +150,17 @@ def _get_win_folder(base): raising=False, ) monkeypatch.setattr(appdirs, "WINDOWS", True) + monkeypatch.setattr(os, "path", ntpath) assert ( - appdirs.user_data_dir("pip", roaming=True).replace("/", "\\") == + appdirs.user_data_dir("pip", roaming=True) == "C:\\Users\\test\\AppData\\Roaming\\pip" ) assert _get_win_folder.calls == [pretend.call("CSIDL_APPDATA")] def test_user_data_dir_osx(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") @@ -158,6 +169,7 @@ def test_user_data_dir_osx(self, monkeypatch): def test_user_data_dir_linux(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_DATA_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -166,6 +178,7 @@ def test_user_data_dir_linux(self, monkeypatch): def test_user_data_dir_linux_override(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_DATA_HOME", "/home/test/.other-share") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -174,6 +187,7 @@ def test_user_data_dir_linux_override(self, monkeypatch): def test_user_data_dir_linux_home_slash(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) # Verify that we are not affected by http://bugs.python.org/issue14768 monkeypatch.delenv("XDG_DATA_HOME") monkeypatch.setenv("HOME", "/") @@ -196,9 +210,10 @@ def _get_win_folder(base): raising=False, ) monkeypatch.setattr(appdirs, "WINDOWS", True) + monkeypatch.setattr(os, "path", ntpath) assert ( - appdirs.user_config_dir("pip", roaming=False).replace("/", "\\") == + appdirs.user_config_dir("pip", roaming=False) == "C:\\Users\\test\\AppData\\Local\\pip" ) assert _get_win_folder.calls == [pretend.call("CSIDL_LOCAL_APPDATA")] @@ -215,13 +230,15 @@ def _get_win_folder(base): raising=False, ) monkeypatch.setattr(appdirs, "WINDOWS", True) + monkeypatch.setattr(os, "path", ntpath) - assert (appdirs.user_config_dir("pip").replace("/", "\\") == + assert (appdirs.user_config_dir("pip") == "C:\\Users\\test\\AppData\\Roaming\\pip") assert _get_win_folder.calls == [pretend.call("CSIDL_APPDATA")] def test_user_config_dir_osx(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "darwin") @@ -230,6 +247,7 @@ def test_user_config_dir_osx(self, monkeypatch): def test_user_config_dir_linux(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_CONFIG_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -238,6 +256,7 @@ def test_user_config_dir_linux(self, monkeypatch): def test_user_config_dir_linux_override(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_CONFIG_HOME", "/home/test/.other-config") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -246,6 +265,7 @@ def test_user_config_dir_linux_override(self, monkeypatch): def test_user_config_dir_linux_home_slash(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) + monkeypatch.setattr(os, "path", posixpath) # Verify that we are not affected by http://bugs.python.org/issue14768 monkeypatch.delenv("XDG_CONFIG_HOME") monkeypatch.setenv("HOME", "/") From 9ed3c1463e6e575469c38197f319bc2fe46fee95 Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Mon, 30 Nov 2015 23:44:31 +0100 Subject: [PATCH 3/6] prefer os.path.join to os.sep.join([]) --- pip/utils/appdirs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pip/utils/appdirs.py b/pip/utils/appdirs.py index 163c92201fa..60ae76ec32d 100644 --- a/pip/utils/appdirs.py +++ b/pip/utils/appdirs.py @@ -156,7 +156,7 @@ def site_config_dirs(appname): xdg_config_dirs = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg') if xdg_config_dirs: pathlist = [ - os.sep.join([expanduser(x), appname]) + os.path.join(expanduser(x), appname) for x in xdg_config_dirs.split(os.pathsep) ] else: From 9cf0342de397351d262c29dc4c0136a3e7be7db5 Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Tue, 1 Dec 2015 00:01:49 +0100 Subject: [PATCH 4/6] set os.pathsep --- tests/unit/test_appdirs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/test_appdirs.py b/tests/unit/test_appdirs.py index e562dc274dd..fe44c1377ee 100644 --- a/tests/unit/test_appdirs.py +++ b/tests/unit/test_appdirs.py @@ -107,6 +107,7 @@ def test_site_config_dirs_linux(self, monkeypatch): def test_site_config_dirs_linux_override(self, monkeypatch): monkeypatch.setattr(appdirs, "WINDOWS", False) monkeypatch.setattr(os, "path", posixpath) + monkeypatch.setattr(os, "pathsep", ':') monkeypatch.setenv("XDG_CONFIG_DIRS", "/spam:/etc:/etc/xdg") monkeypatch.setattr(sys, "platform", "linux2") From b939c3d6d29116bf3e0b1590469eb0de47ba2bc3 Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Tue, 1 Dec 2015 12:38:39 +0100 Subject: [PATCH 5/6] fix test_unsupported_hashes for windows --- tests/unit/test_req.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_req.py b/tests/unit/test_req.py index aa8dddefcf8..47067230ea4 100644 --- a/tests/unit/test_req.py +++ b/tests/unit/test_req.py @@ -161,6 +161,9 @@ def test_unsupported_hashes(self, data): 'file', 2))[0]) finder = PackageFinder([data.find_links], [], session=PipSession()) + sep = os.path.sep + if sep == '\\': + sep = '\\\\' # This needs to be escaped for the regex assert_raises_regexp( HashErrors, r"Can't verify hashes for these requirements because we don't " @@ -169,7 +172,8 @@ def test_unsupported_hashes(self, data): r"\(line 1\)\)\n" r"Can't verify hashes for these file:// requirements because they " r"point to directories:\n" - r" file:///.*/data/packages/FSPkg \(from -r file \(line 2\)\)", + r" file://.*{sep}data{sep}packages{sep}FSPkg " + "\(from -r file \(line 2\)\)".format(sep=sep), reqset.prepare_files, finder) From d459969b0342cd3ade9ec42373430dc5aa1c7251 Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Tue, 1 Dec 2015 13:07:19 +0100 Subject: [PATCH 6/6] fix test_unexisting_path on windows --- tests/unit/test_req.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_req.py b/tests/unit/test_req.py index 47067230ea4..91604eeb929 100644 --- a/tests/unit/test_req.py +++ b/tests/unit/test_req.py @@ -452,7 +452,8 @@ def test_extras_for_editable_url_requirement(self): def test_unexisting_path(self): with pytest.raises(InstallationError) as e: - InstallRequirement.from_line('/this/path/does/not/exist') + InstallRequirement.from_line( + os.path.join('this', 'path', 'does', 'not', 'exist')) err_msg = e.value.args[0] assert "Invalid requirement" in err_msg assert "It looks like a path. Does it exist ?" in err_msg