Repository: ryanhiebert/django-cmd

Issue:
Automatically adjust the PYTHONPATH
https://discord.com/channels/856567261900832808/1317919343098724442

Someone on discord had some issues with their settings module not being found. I need to confirm what the issue might be, but I'm thinking that it's likely that the PYTHONPATH isn't including the current directory because the command isn't running from a direct `python` invocation anymore.

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/ryanhiebert/django-cmd/pull/8?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ryan+Hiebert) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 100.00%. Comparing base [(`0663c12`)](https://app.codecov.io/gh/ryanhiebert/django-cmd/commit/0663c12ede2899ad5cc137283701c9aeb7342bda?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ryan+Hiebert) to head [(`48039e8`)](https://app.codecov.io/gh/ryanhiebert/django-cmd/commit/48039e81d570c21fd3c9f0dae50d5f224c4ec0c7?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ryan+Hiebert).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##              main        #8   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           24        26    +2     
  Branches         3         3           
=========================================
+ Hits            24        26    +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/ryanhiebert/django-cmd/pull/8?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ryan+Hiebert).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Ryan+Hiebert).


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 389e4dc..cf2e197 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -1,3 +1,9 @@
+2.1 (2024-12-16)
+++++++++++++++++
+
+* Automatically add the directory to the Python path
+  when a configuration file is found.
+
 2.0 (2024-12-15)
 ++++++++++++++++
 
diff --git a/django_cmd.py b/django_cmd.py
index 4d78fe7..ea4a29a 100644
--- a/django_cmd.py
+++ b/django_cmd.py
@@ -20,6 +20,7 @@ def main():
                 config = tomllib.load(f)
             settings_module = config["tool"]["django"]["settings_module"]
             os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
+            sys.path.insert(0, "")
         except (FileNotFoundError, KeyError):
             pass  # No pyproject.toml or no settings_module configured
 
@@ -30,5 +31,6 @@ def main():
         if parser.has_option("django", "settings_module"):
             settings_module = parser.get("django", "settings_module")
             os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
+            sys.path.insert(0, "")
 
     execute_from_command_line(sys.argv)
diff --git a/pyproject.toml b/pyproject.toml
index 6ac39d7..00767f7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [project]
 name = "django-cmd"
-version = "2.0"
+version = "2.1"
 description = "Have a django command"
 authors = [{ name = "Ryan Hiebert", email = "ryan@ryanhiebert.com" }]
 license = "MIT"


Test patch:
diff --git a/django_cmd_test.py b/django_cmd_test.py
index 51f436a..69a6b22 100644
--- a/django_cmd_test.py
+++ b/django_cmd_test.py
@@ -71,6 +71,10 @@ def test_main_no_configfile(mocker, tmpdir):
     assert cmd.called
 
 
-def test_call_command():
-    """Should have installed a "django" command."""
-    subprocess.call(["django"])
+def test_new_project(tmpdir):
+    """Should be able to use with a new project."""
+    tmpdir.chdir()
+    subprocess.run(["django", "startproject", "myproject", "."], check=True)
+    config = '[tool.django]\nsettings_module = "myproject.settings"\n'
+    tmpdir.join("pyproject.toml").write(config.encode("utf-8"))
+    subprocess.run(["django", "check"], check=True)