Repository: akaihola/darker

Issue:
`-` as source file not allowed when using the `--stdin-filename` option (unlike in Black)
<!-- 

    NOTE:

    To ask for help using Darker, please use Discussions (see the top of this page).
    This form is only for reporting bugs.

-->

**Describe the bug**
I have the Black Formatter extension and configured darker in the black formatter path.
When formatting on save in VSCode, the output log will contain below error.

NOTE: everything works fine when not using the Black Formatter extension, but using the formatter feature of the Python extension (ms-python.python) instead.

```
  File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\xxx\envs\yyy\Scripts\darker.exe\__main__.py", line 7, in <module>
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\__main__.py", line 631, in main_with_error_handling
    return main()
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\__main__.py", line 484, in main
    args, config, config_nondefault = parse_command_line(argv)
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\command_line.py", line 150, in parse_command_line
    validate_stdin_src(args.stdin_filename, args.src)
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\config.py", line 114, in validate_stdin_src
    raise ConfigurationError(
darker.config.ConfigurationError: No Python source files are allowed when using the `stdin-filename` option
```

**To Reproduce**
Steps to reproduce the behavior:
1. pip install darker
2. In VSCode install extension Black Formatter (ms-python.black-formatter)
3. Configure Black as default formatter for python files.
4. In settings.json:
```
  "black-formatter.path": [
    "path\\to\\darker.exe"
  ],
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
    "--line-length",
    "100"
  ],
  "[python]": {
    "editor.formatOnSave": true
  }
```
5. Add whitespace after a line in a python file and save.

**Expected behavior**
Reformatted file


**Environment (please complete the following information):**
 - OS: Windows 10
 - Python 3.9.13
 - Git version 2.39.1.windows.1
 - Darker version 1.7.1
 - Black version 22.3.0

Discussion:
akaihola: @my-tien could you review this pull request? Can I add you as a collaborator on this repository and assign the review to you?
my-tien: Yes, you can assign me
akaihola: Thanks @my-tien for the review. Merging this is now blocked by #495.
akaihola: @my-tien I created fixes for #495 in
- #496 
- #497 

Is it possible for you to review those as well?

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 745bcf7..1e4f29a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -5,6 +5,8 @@ These features will be included in the next release:
 
 Added
 -----
+- Allow ``-`` as the single source filename when using the ``--stdin-filename`` option.
+  This makes the option compatible with Black.
 
 Fixed
 -----
diff --git a/src/darker/config.py b/src/darker/config.py
index 6709a2d..940a5d9 100644
--- a/src/darker/config.py
+++ b/src/darker/config.py
@@ -109,7 +109,7 @@ def validate_stdin_src(stdin_filename: Optional[str], src: List[str]) -> None:
     """Make sure both ``stdin`` mode and paths/directories are specified"""
     if stdin_filename is None:
         return
-    if len(src) == 0:
+    if len(src) == 0 or src == ["-"]:
         return
     raise ConfigurationError(
         "No Python source files are allowed when using the `stdin-filename` option"


Test patch:
diff --git a/src/darker/tests/test_main_stdin_filename.py b/src/darker/tests/test_main_stdin_filename.py
index ecb4225..d6dbd5f 100644
--- a/src/darker/tests/test_main_stdin_filename.py
+++ b/src/darker/tests/test_main_stdin_filename.py
@@ -110,6 +110,13 @@ pytestmark = pytest.mark.usefixtures("find_project_root_cache_clear")
     dict(
         stdin_filename="a.py", revision="..:STDIN:", expect_a_py='modified = "stdin"\n'
     ),
+    dict(src=["-"], stdin_filename="a.py", expect_a_py='modified = "stdin"\n'),
+    dict(
+        src=["-"],
+        stdin_filename="a.py",
+        revision="..:STDIN:",
+        expect_a_py='modified = "stdin"\n',
+    ),
     dict(
         stdin_filename="a.py",
         revision="..:WORKTREE:",