Custom configuration support ()

This commit is contained in:
Kasun Herath 2023-04-25 10:10:07 +05:30 committed by GitHub
parent 3c4fb256a2
commit fc9b03ebd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -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,

View File

@ -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:

View File

@ -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
)