Repository: dynaconf/dynaconf (python)

Issue:
[bug] list command must error for not found key
```bash
# /venv/bin/dynaconf list -k "D" --json
Key not found
# echo $?
0
```

Expected: `1` retcode

---

Open for discussion:

- Should retcode be `1`
- Should return empty json `{}`

?

Pull request:
fix: `-k` must exit code 1 when key do not exist
fix #1235

Patch:
diff --git a/dynaconf/cli.py b/dynaconf/cli.py
index bca73f1..d096bd9 100644
--- a/dynaconf/cli.py
+++ b/dynaconf/cli.py
@@ -320,7 +320,7 @@ def init(ctx, fileformat, path, env, _vars, _secrets, wg, y, django):
             "app = Flask(__name__)\n"
             "FlaskDynaconf(app)\n"
         )
-        exit(1)
+        sys.exit(1)
 
     path = Path(path)
 
@@ -650,7 +650,7 @@ def _list(
 
         if value is empty:
             click.secho("Key not found", bg="red", fg="white", err=True)
-            return
+            sys.exit(1)
 
         if not _json:
             click.echo(format_setting(key, value))


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 92d0bb3..d607119 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -115,6 +115,25 @@ def test_list(testdir):
     assert "TEST_KEY<str> 'test_value'" in result
 
 
+@pytest.mark.parametrize("cmd", ["list", "inspect", "get"])
+def test_not_found_key_exit_error(cmd, testdir):
+    """Test error code when key not found"""
+    if cmd == "get":
+        command = [cmd, "DONTEXIST"]
+    else:
+        command = [cmd, "-k", "DONTEXIST"]
+
+    result = run(
+        command,
+        env={
+            "ROOT_PATH_FOR_DYNACONF": testdir,
+            "INSTANCE_FOR_DYNACONF": "tests.config.settings",
+        },
+        attr="exit_code",
+    )
+    assert result == 1
+
+
 def test_get(testdir):
     """Tests get command"""
     result = run(