Forráskód Böngészése

Custom configuration support (#792)

Kasun Herath 2 éve
szülő
commit
fc9b03ebd7
3 módosított fájl, 32 hozzáadás és 2 törlés
  1. 4 1
      pynecone/compiler/templates.py
  2. 3 1
      pynecone/utils/prerequisites.py
  3. 25 0
      tests/test_utils.py

+ 4 - 1
pynecone/compiler/templates.py

@@ -8,7 +8,10 @@ from pynecone.utils import path_ops
 # Template for the Pynecone config file.
 PCCONFIG = f"""import pynecone as pc
 
-config = pc.Config(
+class {{config_name}}(pc.Config):
+    pass
+
+config = {{config_name}}(
     app_name="{{app_name}}",
     db_url="{constants.DB_URL}",
     env=pc.Env.DEV,

+ 3 - 1
pynecone/utils/prerequisites.py

@@ -5,6 +5,7 @@ from __future__ import annotations
 import json
 import os
 import platform
+import re
 import subprocess
 import sys
 from pathlib import Path
@@ -147,8 +148,9 @@ def create_config(app_name: str):
     # Import here to avoid circular imports.
     from pynecone.compiler import templates
 
+    config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
     with open(constants.CONFIG_FILE, "w") as f:
-        f.write(templates.PCCONFIG.format(app_name=app_name))
+        f.write(templates.PCCONFIG.format(app_name=app_name, config_name=config_name))
 
 
 def create_web_directory(root: Path) -> str:

+ 25 - 0
tests/test_utils.py

@@ -312,3 +312,28 @@ def test_format_upload_event(upload_event_spec):
         'upload_state.files, "upload_state.handle_upload1",'
         "UPLOAD)"
     )
+
+
+@pytest.mark.parametrize(
+    "app_name,expected_config_name",
+    [
+        ("appname", "AppnameConfig"),
+        ("app_name", "AppnameConfig"),
+        ("app-name", "AppnameConfig"),
+        ("appname2.io", "AppnameioConfig"),
+    ],
+)
+def test_create_config(app_name, expected_config_name, mocker):
+    """Test templates.PCCONFIG is formatted with correct app name and config class name.
+
+    Args:
+        app_name: App name.
+        expected_config_name: Expected config name.
+        mocker: Mocker object.
+    """
+    mocker.patch("builtins.open")
+    tmpl_mock = mocker.patch("pynecone.compiler.templates.PCCONFIG")
+    prerequisites.create_config(app_name)
+    tmpl_mock.format.assert_called_with(
+        app_name=app_name, config_name=expected_config_name
+    )