From 9f18d9cb7fcb120304b7a371aed0c8dfe21f2bc5 Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Wed, 4 Jan 2023 16:53:18 -0600 Subject: [PATCH 1/8] change name of configuration files --- src/pyscript/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pyscript/__init__.py b/src/pyscript/__init__.py index 471235c..dd9dfc1 100644 --- a/src/pyscript/__init__.py +++ b/src/pyscript/__init__.py @@ -7,13 +7,13 @@ APPNAME = "pyscript" APPAUTHOR = "python" -DEFAULT_CONFIG_FILENAME = "pyscript.json" +DEFAULT_CONFIG_FILENAME = ".pyscriptconfig" # Default initial data for the command line. DEFAULT_CONFIG = { # Name of config file for PyScript projects. - "project_config_filename": "manifest.json", + "project_config_filename": "pyscript.toml", } From 8a98eb1946cf40a13ea757d741c51c9ae5cb2329 Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Wed, 4 Jan 2023 17:27:30 -0600 Subject: [PATCH 2/8] make sure generator can handle both toml and json config files --- src/pyscript/_generator.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pyscript/_generator.py b/src/pyscript/_generator.py index 1a949ba..3b39ba6 100644 --- a/src/pyscript/_generator.py +++ b/src/pyscript/_generator.py @@ -2,7 +2,7 @@ import json from pathlib import Path from typing import Optional - +import toml import jinja2 from pyscript import config @@ -50,6 +50,10 @@ def create_project( app_dir.mkdir() manifest_file = app_dir / config["project_config_filename"] with manifest_file.open("w", encoding="utf-8") as fp: - json.dump(context, fp) + if str(manifest_file).endswith(".json"): + json.dump(context, fp) + else: + toml.dump(context, fp) + index_file = app_dir / "index.html" string_to_html('print("Hello, world!")', app_name, index_file) From f045730287819e78ecc829e30bd7a73ef477dac0 Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Wed, 4 Jan 2023 17:28:35 -0600 Subject: [PATCH 3/8] change the default config test to be toml as it's the new default --- tests/test_generator.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_generator.py b/tests/test_generator.py index 1664ce7..55fd6e4 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -6,6 +6,7 @@ import json from pathlib import Path from typing import Any +import toml import pytest @@ -18,13 +19,20 @@ def test_create_project(tmp_cwd: Path, is_not_none: Any) -> None: app_description = "A longer, human friendly, app description." author_name = "A.Coder" author_email = "acoder@domain.com" + + # GIVEN a a new project gen.create_project(app_name, app_description, author_name, author_email) + # with a default config path manifest_path = tmp_cwd / app_name / config["project_config_filename"] + + # assert that the new project config file exists assert manifest_path.exists() + # assert that we can load it as a TOML file (TOML is the default config format) + # and that the contents of the config are as we expect with manifest_path.open() as fp: - contents = json.load(fp) + contents = toml.load(fp) assert contents == { "name": "app_name", From f493beef79f2deb3c9ed3f72d507df69865e120b Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Wed, 4 Jan 2023 18:45:01 -0600 Subject: [PATCH 4/8] add more tests --- tests/test_generator.py | 100 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/tests/test_generator.py b/tests/test_generator.py index 55fd6e4..6100a10 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -54,3 +54,103 @@ def test_create_project_twice_raises_error(tmp_cwd: Path) -> None: with pytest.raises(FileExistsError): gen.create_project(app_name, app_description, author_name, author_email) + + +def test_create_project_explicit_json(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: + app_name = "JSON_app_name" + app_description = "A longer, human friendly, app description." + author_name = "A.Coder" + author_email = "acoder@domain.com" + + # Let's patch the config so that the project config file is a JSON file + config_file_name = 'pyscript.json' + monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) + + # GIVEN a new project + gen.create_project(app_name, app_description, author_name, author_email) + + # get the path where the config file is being created + manifest_path = tmp_cwd / app_name / config["project_config_filename"] + + # assert that the new project config file exists + assert manifest_path.exists() + + # assert that we can load it as a TOML file (TOML is the default config format) + # and that the contents of the config are as we expect + with manifest_path.open() as fp: + contents = json.load(fp) + + assert contents == { + "name": "JSON_app_name", + "description": app_description, + "type": "app", + "author_name": author_name, + "author_email": author_email, + "version": is_not_none, + } + + +def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: + app_name = "TOML_app_name" + app_description = "A longer, human friendly, app description." + author_name = "A.Coder" + author_email = "acoder@domain.com" + + # Let's patch the config so that the project config file is a JSON file + config_file_name = 'mypyscript.toml' + monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) + + # GIVEN a new project + gen.create_project(app_name, app_description, author_name, author_email) + + # get the path where the config file is being created + manifest_path = tmp_cwd / app_name / config["project_config_filename"] + + check_project_manifest(manifest_path, toml, app_name, is_not_none) + +def check_project_manifest( + config_path: Path, serializer: Any, app_name: str, is_not_none: Any, + app_description: str = "A longer, human friendly, app description.", + author_name: str = "A.Coder", author_email: str = "acoder@domain.com", + project_type: str = "app"): + """ + Perform the following: + + * checks that `config_path` exists + * loads the contents of `config_path` using `serializer.load` + * check that the contents match with the values provided in input. Specifically: + * "name" == app_name + * "description" == app_description + * "type" == app_type + * "author_name" == author_name + * "author_email" == author_email + * "version" == is_not_none + + Params: + * config_path(Path): path to the app config file + * serializer(json|toml): serializer to be used to load contents of `config_path`. + Supported values are either modules `json` or `toml` + * app_name(str): name of application + * is_not_none(any): pytest fixture + * app_description(str): application description + * author_name(str): application author name + * author_email(str): application author email + * project_type(str): project type + + """ + # assert that the new project config file exists + assert config_path.exists() + + # assert that we can load it as a TOML file (TOML is the default config format) + # and that the contents of the config are as we expect + with config_path.open() as fp: + contents = serializer.load(fp) + + assert contents == { + "name": app_name, + "description": app_description, + "type": project_type, + "author_name": author_name, + "author_email": author_email, + "version": is_not_none, + } From 6408dfbd7e1f39913d20bc87380959fece7df9e4 Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Thu, 5 Jan 2023 08:33:37 -0600 Subject: [PATCH 5/8] remove trailing space --- tests/test_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_generator.py b/tests/test_generator.py index 6100a10..9980302 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -110,7 +110,7 @@ def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypat def check_project_manifest( config_path: Path, serializer: Any, app_name: str, is_not_none: Any, - app_description: str = "A longer, human friendly, app description.", + app_description: str = "A longer, human friendly, app description.", author_name: str = "A.Coder", author_email: str = "acoder@domain.com", project_type: str = "app"): """ From 02f86b0fc2c801ab120c437a0955069a5210e415 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Jan 2023 19:21:17 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pyscript/_generator.py | 3 ++- tests/test_generator.py | 32 +++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/pyscript/_generator.py b/src/pyscript/_generator.py index 3b39ba6..86b8e4e 100644 --- a/src/pyscript/_generator.py +++ b/src/pyscript/_generator.py @@ -2,8 +2,9 @@ import json from pathlib import Path from typing import Optional -import toml + import jinja2 +import toml from pyscript import config diff --git a/tests/test_generator.py b/tests/test_generator.py index 6100a10..7d01add 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -6,9 +6,9 @@ import json from pathlib import Path from typing import Any -import toml import pytest +import toml from pyscript import _generator as gen from pyscript import config @@ -56,15 +56,17 @@ def test_create_project_twice_raises_error(tmp_cwd: Path) -> None: gen.create_project(app_name, app_description, author_name, author_email) -def test_create_project_explicit_json(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: +def test_create_project_explicit_json( + tmp_cwd: Path, is_not_none: Any, monkeypatch +) -> None: app_name = "JSON_app_name" app_description = "A longer, human friendly, app description." author_name = "A.Coder" author_email = "acoder@domain.com" # Let's patch the config so that the project config file is a JSON file - config_file_name = 'pyscript.json' - monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) + config_file_name = "pyscript.json" + monkeypatch.setitem(gen.config, "project_config_filename", config_file_name) # GIVEN a new project gen.create_project(app_name, app_description, author_name, author_email) @@ -90,15 +92,17 @@ def test_create_project_explicit_json(tmp_cwd: Path, is_not_none: Any, monkeypat } -def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: +def test_create_project_explicit_toml( + tmp_cwd: Path, is_not_none: Any, monkeypatch +) -> None: app_name = "TOML_app_name" app_description = "A longer, human friendly, app description." author_name = "A.Coder" author_email = "acoder@domain.com" # Let's patch the config so that the project config file is a JSON file - config_file_name = 'mypyscript.toml' - monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) + config_file_name = "mypyscript.toml" + monkeypatch.setitem(gen.config, "project_config_filename", config_file_name) # GIVEN a new project gen.create_project(app_name, app_description, author_name, author_email) @@ -108,11 +112,17 @@ def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypat check_project_manifest(manifest_path, toml, app_name, is_not_none) + def check_project_manifest( - config_path: Path, serializer: Any, app_name: str, is_not_none: Any, - app_description: str = "A longer, human friendly, app description.", - author_name: str = "A.Coder", author_email: str = "acoder@domain.com", - project_type: str = "app"): + config_path: Path, + serializer: Any, + app_name: str, + is_not_none: Any, + app_description: str = "A longer, human friendly, app description.", + author_name: str = "A.Coder", + author_email: str = "acoder@domain.com", + project_type: str = "app", +): """ Perform the following: From b3fb431bd77d498c715644b294e855b0e83242ec Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Thu, 5 Jan 2023 13:28:52 -0600 Subject: [PATCH 7/8] reuse check_project_manifest on tests that make sense --- tests/test_generator.py | 56 +++++++++-------------------------------- 1 file changed, 12 insertions(+), 44 deletions(-) diff --git a/tests/test_generator.py b/tests/test_generator.py index 9980302..8721ab2 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -14,6 +14,10 @@ from pyscript import config +TESTS_AUTHOR_NAME = "A.Coder" +TESTS_AUTHOR_EMAIL = "acoder@domain.com" + + def test_create_project(tmp_cwd: Path, is_not_none: Any) -> None: app_name = "app_name" app_description = "A longer, human friendly, app description." @@ -21,87 +25,51 @@ def test_create_project(tmp_cwd: Path, is_not_none: Any) -> None: author_email = "acoder@domain.com" # GIVEN a a new project - gen.create_project(app_name, app_description, author_name, author_email) + gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) # with a default config path manifest_path = tmp_cwd / app_name / config["project_config_filename"] - # assert that the new project config file exists - assert manifest_path.exists() - - # assert that we can load it as a TOML file (TOML is the default config format) - # and that the contents of the config are as we expect - with manifest_path.open() as fp: - contents = toml.load(fp) - - assert contents == { - "name": "app_name", - "description": "A longer, human friendly, app description.", - "type": "app", - "author_name": "A.Coder", - "author_email": "acoder@domain.com", - "version": is_not_none, - } + check_project_manifest(manifest_path, toml, app_name, is_not_none) def test_create_project_twice_raises_error(tmp_cwd: Path) -> None: """We get a FileExistsError when we try to create an existing project.""" app_name = "app_name" app_description = "A longer, human friendly, app description." - author_name = "A.Coder" - author_email = "acoder@domain.com" - gen.create_project(app_name, app_description, author_name, author_email) + gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) with pytest.raises(FileExistsError): - gen.create_project(app_name, app_description, author_name, author_email) + gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) def test_create_project_explicit_json(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: app_name = "JSON_app_name" app_description = "A longer, human friendly, app description." - author_name = "A.Coder" - author_email = "acoder@domain.com" # Let's patch the config so that the project config file is a JSON file config_file_name = 'pyscript.json' monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) # GIVEN a new project - gen.create_project(app_name, app_description, author_name, author_email) + gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) # get the path where the config file is being created manifest_path = tmp_cwd / app_name / config["project_config_filename"] - # assert that the new project config file exists - assert manifest_path.exists() - - # assert that we can load it as a TOML file (TOML is the default config format) - # and that the contents of the config are as we expect - with manifest_path.open() as fp: - contents = json.load(fp) - - assert contents == { - "name": "JSON_app_name", - "description": app_description, - "type": "app", - "author_name": author_name, - "author_email": author_email, - "version": is_not_none, - } + check_project_manifest(manifest_path, json, app_name, is_not_none) def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: app_name = "TOML_app_name" app_description = "A longer, human friendly, app description." - author_name = "A.Coder" - author_email = "acoder@domain.com" # Let's patch the config so that the project config file is a JSON file config_file_name = 'mypyscript.toml' monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) # GIVEN a new project - gen.create_project(app_name, app_description, author_name, author_email) + gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) # get the path where the config file is being created manifest_path = tmp_cwd / app_name / config["project_config_filename"] @@ -111,7 +79,7 @@ def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypat def check_project_manifest( config_path: Path, serializer: Any, app_name: str, is_not_none: Any, app_description: str = "A longer, human friendly, app description.", - author_name: str = "A.Coder", author_email: str = "acoder@domain.com", + author_name: str = TESTS_AUTHOR_NAME, author_email: str = TESTS_AUTHOR_EMAIL , project_type: str = "app"): """ Perform the following: From 968d2e5a33edfb8b19dbf46413f8bea1247fb53b Mon Sep 17 00:00:00 2001 From: Fabio Pliger Date: Thu, 5 Jan 2023 13:30:06 -0600 Subject: [PATCH 8/8] reuse check_project_manifest on tests that make sense --- src/pyscript/_generator.py | 3 ++- tests/test_generator.py | 37 +++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/pyscript/_generator.py b/src/pyscript/_generator.py index 3b39ba6..86b8e4e 100644 --- a/src/pyscript/_generator.py +++ b/src/pyscript/_generator.py @@ -2,8 +2,9 @@ import json from pathlib import Path from typing import Optional -import toml + import jinja2 +import toml from pyscript import config diff --git a/tests/test_generator.py b/tests/test_generator.py index 8721ab2..da8c9ef 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -6,14 +6,13 @@ import json from pathlib import Path from typing import Any -import toml import pytest +import toml from pyscript import _generator as gen from pyscript import config - TESTS_AUTHOR_NAME = "A.Coder" TESTS_AUTHOR_EMAIL = "acoder@domain.com" @@ -21,8 +20,6 @@ def test_create_project(tmp_cwd: Path, is_not_none: Any) -> None: app_name = "app_name" app_description = "A longer, human friendly, app description." - author_name = "A.Coder" - author_email = "acoder@domain.com" # GIVEN a a new project gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) @@ -40,16 +37,20 @@ def test_create_project_twice_raises_error(tmp_cwd: Path) -> None: gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) with pytest.raises(FileExistsError): - gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) + gen.create_project( + app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL + ) -def test_create_project_explicit_json(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: +def test_create_project_explicit_json( + tmp_cwd: Path, is_not_none: Any, monkeypatch +) -> None: app_name = "JSON_app_name" app_description = "A longer, human friendly, app description." # Let's patch the config so that the project config file is a JSON file - config_file_name = 'pyscript.json' - monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) + config_file_name = "pyscript.json" + monkeypatch.setitem(gen.config, "project_config_filename", config_file_name) # GIVEN a new project gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) @@ -60,13 +61,15 @@ def test_create_project_explicit_json(tmp_cwd: Path, is_not_none: Any, monkeypat check_project_manifest(manifest_path, json, app_name, is_not_none) -def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypatch) -> None: +def test_create_project_explicit_toml( + tmp_cwd: Path, is_not_none: Any, monkeypatch +) -> None: app_name = "TOML_app_name" app_description = "A longer, human friendly, app description." # Let's patch the config so that the project config file is a JSON file - config_file_name = 'mypyscript.toml' - monkeypatch.setitem(gen.config, 'project_config_filename', config_file_name) + config_file_name = "mypyscript.toml" + monkeypatch.setitem(gen.config, "project_config_filename", config_file_name) # GIVEN a new project gen.create_project(app_name, app_description, TESTS_AUTHOR_NAME, TESTS_AUTHOR_EMAIL) @@ -76,11 +79,17 @@ def test_create_project_explicit_toml(tmp_cwd: Path, is_not_none: Any, monkeypat check_project_manifest(manifest_path, toml, app_name, is_not_none) + def check_project_manifest( - config_path: Path, serializer: Any, app_name: str, is_not_none: Any, + config_path: Path, + serializer: Any, + app_name: str, + is_not_none: Any, app_description: str = "A longer, human friendly, app description.", - author_name: str = TESTS_AUTHOR_NAME, author_email: str = TESTS_AUTHOR_EMAIL , - project_type: str = "app"): + author_name: str = TESTS_AUTHOR_NAME, + author_email: str = TESTS_AUTHOR_EMAIL, + project_type: str = "app", +): """ Perform the following: