Repository: pypa/setuptools

Issue:
False positive when checking for inconsistent setup script path on Windows
```
> python setup.py develop
running develop
Checking .pth file support in C:\project\venv\Lib\site-packages\
C:\project\venv\Scripts\pythonw.exe -E -c pass
TEST PASSED: C:\project\venv\Lib\site-packages\ appears to support .pth files
error: ("Can't get a consistent path to setup script from installation directory", 'c:\\project\\', 'c:\\project')
```

I am not sure what exactly is the cause (I did not have this problem previously, but suddenly sees it when coming back to work on an old project), but it seems to come from a quirk of `realpath()`:

```pycon
>>> import os
>>> os.path.realpath('.')
'C:\\Users\\uranusjr\\'
>>> os.path.realpath('../')
'C:\\Users\\'
>>> os.path.realpath('..')
'C:\\Users'
```


Discussion:
@uranusjr Can you provide a [minimal reproducing example](http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports) for this? It seems that the error is being raised from [here](https://github.com/pypa/setuptools/blob/e0433cf32dfdf39c0c14a3740c3920c496f09486/setuptools/command/develop.py#L91), based on the error message, but without a failing test or a minimal reproducing example, it's hard to tell how to fix it.
Of course!

```
> mkdir project
> cd project
> mkdir src
> [create setup.py]
```

```python
from setuptools import find_packages, setup

setup(
    name='project',
    version='0.0.0',
    package_dir={'': 'src'},
    packages=find_packages('src'),
)
```

```
> python setup.py develop
running develop
Checking .pth file support in C:\Users\uranusjr\Documents\playground\project\.venv\Lib\site-packages\
C:\Users\uranusjr\Documents\playground\project\.venv\Scripts\pythonw.exe -E -c pass
TEST PASSED: C:\Users\uranusjr\Documents\playground\project\.venv\Lib\site-packages\ appears to support .pth files
error: ("Can't get a consistent path to setup script from installation directory", 'c:\\users\\uranusjr\\documents\\playground\\project\\', 'c:\\users\\uranusjr\\documents\\playground\\project')
```
I did some digging and it seems the most straightforward fix is to call an additional `os.path.normpath` in [`normalize_path`](https://github.com/pypa/setuptools/blob/e0433cf32dfdf39c0c14a3740c3920c496f09486/pkg_resources/__init__.py#L2223) to normalise the trailing slash:

```python
def normalize_path(filename):
    """Normalize a file/dir name for comparison purposes"""
    return os.path.normcase(os.path.normpath(os.path.realpath(filename)))
```

This seems reasonable based on the function’s docstring, but I am not sure how accurate it is, and whether the change would have more implications.
I think based on the file's docstring, it makes sense to add the additional `normpath` call in there, since these *should* compare equal. For example, `pathlib` has them compare equal:

```python
>>> pathlib.Path("path/to/file") == pathlib.Path("path/to/file/")
True
```

Resolving patch:
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py
--- a/pkg_resources/__init__.py
+++ b/pkg_resources/__init__.py
@@ -2228,7 +2228,7 @@ def null_ns_handler(importer, path_item, packageName, module):
 
 def normalize_path(filename):
     """Normalize a file/dir name for comparison purposes"""
-    return os.path.normcase(os.path.realpath(_cygwin_patch(filename)))
+    return os.path.normcase(os.path.realpath(os.path.normpath(_cygwin_patch(filename))))
 
 
 def _cygwin_patch(filename):  # pragma: nocover


Test patch:
diff --git a/pkg_resources/tests/test_pkg_resources.py b/pkg_resources/tests/test_pkg_resources.py
--- a/pkg_resources/tests/test_pkg_resources.py
+++ b/pkg_resources/tests/test_pkg_resources.py
@@ -236,3 +236,56 @@ def test_version_resolved_from_egg_info(self, env):
         req = pkg_resources.Requirement.parse('foo>=1.9')
         dist = pkg_resources.WorkingSet([env.paths['lib']]).find(req)
         assert dist.version == version
+
+    @pytest.mark.parametrize(
+        'unnormalized, normalized',
+        [
+            ('foo', 'foo'),
+            ('foo/', 'foo'),
+            ('foo/bar', 'foo/bar'),
+            ('foo/bar/', 'foo/bar'),
+        ],
+    )
+    def test_normalize_path_trailing_sep(self, unnormalized, normalized):
+        """Ensure the trailing slash is cleaned for path comparison.
+
+        See pypa/setuptools#1519.
+        """
+        result_from_unnormalized = pkg_resources.normalize_path(unnormalized)
+        result_from_normalized = pkg_resources.normalize_path(normalized)
+        assert result_from_unnormalized == result_from_normalized
+
+    @pytest.mark.skipif(
+        os.path.normcase('A') != os.path.normcase('a'),
+        reason='Testing case-insensitive filesystems.',
+    )
+    @pytest.mark.parametrize(
+        'unnormalized, normalized',
+        [
+            ('MiXeD/CasE', 'mixed/case'),
+        ],
+    )
+    def test_normalize_path_normcase(self, unnormalized, normalized):
+        """Ensure mixed case is normalized on case-insensitive filesystems.
+        """
+        result_from_unnormalized = pkg_resources.normalize_path(unnormalized)
+        result_from_normalized = pkg_resources.normalize_path(normalized)
+        assert result_from_unnormalized == result_from_normalized
+
+    @pytest.mark.skipif(
+        os.path.sep != '\\',
+        reason='Testing systems using backslashes as path separators.',
+    )
+    @pytest.mark.parametrize(
+        'unnormalized, expected',
+        [
+            ('forward/slash', 'forward\\slash'),
+            ('forward/slash/', 'forward\\slash'),
+            ('backward\\slash\\', 'backward\\slash'),
+        ],
+    )
+    def test_normalize_path_backslash_sep(self, unnormalized, expected):
+        """Ensure path seps are cleaned on backslash path sep systems.
+        """
+        result = pkg_resources.normalize_path(unnormalized)
+        assert result.endswith(expected)