black/tests
Shantanu 23dfc5b2c3
Fix ignoring input files for symlink reasons (#4222)
This relates to #4015, #4161 and the behaviour of os.getcwd()

Black is a big user of pathlib and as such loves doing `.resolve()`,
since for a long time it was the only good way of getting an absolute
path in pathlib. However, this has two problems:

The first minor problem is performance, e.g. in #3751 I (safely) got rid
of a bunch of `.resolve()` which made Black 40% faster on cached runs.

The second more important problem is that always resolving symlinks
results in unintuitive exclusion behaviour. For instance, a gitignored
symlink should never alter formatting of your actual code. This kind of
thing was reported by users a few times.

In #3846, I improved the exclusion rule logic for symlinks in
`gen_python_files` and everything was good.

But `gen_python_files` isn't enough, there's also `get_sources`, which
handles user specified paths directly (instead of files Black
discovers). So in #4015, I made a very similar change to #3846 for
`get_sources`, and this is where some problems began.

The core issue was the line:
```
root_relative_path = path.absolute().relative_to(root).as_posix()
```
The first issue is that despite root being computed from user inputs, we
call `.resolve()` while computing it (likely unecessarily). Which means
that `path` may not actually be relative to `root`. So I started off
this PR trying to fix that, when I ran into the second issue. Which is
that `os.getcwd()` (as called by `os.path.abspath` or `Path.absolute` or
`Path.cwd`) also often resolves symlinks!
```
>>> import os
>>> os.environ.get("PWD")
'/Users/shantanu/dev/black/symlink/bug'
>>> os.getcwd()
'/Users/shantanu/dev/black/actual/bug'
```
This also meant that the breakage often would not show up when input
relative paths.

This doesn't affect `gen_python_files` / #3846 because things are always
absolute and known to be relative to `root`.

Anyway, it looks like #4161 fixed the crash by just swallowing the error
and ignoring the file. Instead, we should just try to compute the actual
relative path. I think this PR should be quite safe, but we could also
consider reverting some of the previous changes; the associated issues
weren't too popular.

At the same time, I think there's still behaviour that can be improved
and I kind of want to make larger changes, but maybe I'll save that for
if we do something like #3952

Hopefully fixes #4205, fixes #4209, actual fix for #4077
2024-02-12 00:04:09 -08:00
..
data Remove redundant parentheses in case statement if guards (#4214) 2024-02-07 06:55:02 -08:00
__init__.py Remove deprecated use of 'setup.py test' (#1275) 2020-05-08 06:23:50 -07:00
conftest.py add support for printing the diff of AST trees when running tests (#3902) 2023-09-28 07:03:24 -07:00
empty.toml Add --target-version option to allow users to choose targeted Python versions (#618) 2019-02-06 18:43:50 -08:00
optional.py Upgrade mypy to 1.6.1 (#4049) 2023-11-18 11:42:36 -08:00
test_black.py Fix ignoring input files for symlink reasons (#4222) 2024-02-12 00:04:09 -08:00
test_blackd.py Migrate mypy config to pyproject.toml (#3936) 2023-10-16 05:24:16 -07:00
test_docs.py Test that preview/unstable features are documented (#4187) 2024-01-30 22:22:38 -08:00
test_format.py test preview cases with line-length 1 unless explicitly skipped (#4087) 2023-12-06 07:17:33 -08:00
test_ipynb.py Avoid importing IPython if notebook cells do not contain magics (#3782) 2023-07-16 21:09:26 -04:00
test_no_ipynb.py Enable PYTHONWARNDEFAULTENCODING = 1 in CI (#3763) 2023-07-04 16:38:39 -07:00
test_ranges.py Support formatting specified lines (#4020) 2023-11-06 18:05:25 -08:00
test_schema.py feat: add schema and validate-pyproject support (#4181) 2024-01-29 07:56:48 -08:00
test_trans.py Rewrite mostly useless assert in test_trans.py (#3810) 2023-07-27 12:51:28 -04:00
test.toml Add support for custom python cell magics (#2744) 2022-01-20 16:45:28 -08:00
util.py Add --unstable flag (#4096) 2024-01-25 17:00:47 -08:00