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: diff --git a/tests/unit/test_appdirs.py b/tests/unit/test_appdirs.py index 4c0710dd4a3..fe44c1377ee 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,18 +22,23 @@ 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") 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.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_CACHE_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -38,6 +46,8 @@ 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.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_CACHE_HOME", "/home/test/.other-cache") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -45,6 +55,8 @@ 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) + 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", "/") @@ -67,15 +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") @@ -83,6 +94,8 @@ 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.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_CONFIG_DIRS") monkeypatch.setattr(sys, "platform", "linux2") @@ -92,6 +105,9 @@ 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") @@ -117,8 +133,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")] @@ -134,14 +151,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") @@ -149,6 +169,8 @@ 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.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_DATA_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -156,6 +178,8 @@ 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.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_DATA_HOME", "/home/test/.other-share") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -163,6 +187,8 @@ 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) + 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", "/") @@ -185,9 +211,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")] @@ -204,12 +231,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") @@ -217,6 +247,8 @@ 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.setattr(os, "path", posixpath) monkeypatch.delenv("XDG_CONFIG_HOME") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -224,6 +256,8 @@ 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.setattr(os, "path", posixpath) monkeypatch.setenv("XDG_CONFIG_HOME", "/home/test/.other-config") monkeypatch.setenv("HOME", "/home/test") monkeypatch.setattr(sys, "platform", "linux2") @@ -231,6 +265,8 @@ 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) + 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", "/") diff --git a/tests/unit/test_req.py b/tests/unit/test_req.py index aa8dddefcf8..91604eeb929 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) @@ -448,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