From f2ab2bac6b26f82730d5f5d7ab1e6d5f2f1060e3 Mon Sep 17 00:00:00 2001
From: David Luevano Alvarado <david@luevano.xyz>
Date: Sun, 19 Feb 2023 23:36:04 -0600
Subject: add yaml_parser tests, small refactor

---
 src/pyssg/__init__.py      |  2 ++
 src/pyssg/yaml_parser.py   |  7 ++++---
 tests/conftest.py          |  6 ++++++
 tests/io_files/simple.yaml | 28 ++++++++++++++++++++++++++++
 tests/test_yaml_parser.py  | 19 +++++++++++++++++++
 5 files changed, 59 insertions(+), 3 deletions(-)
 create mode 100644 tests/io_files/simple.yaml
 create mode 100644 tests/test_yaml_parser.py

diff --git a/src/pyssg/__init__.py b/src/pyssg/__init__.py
index 5d112f1..46e2323 100644
--- a/src/pyssg/__init__.py
+++ b/src/pyssg/__init__.py
@@ -1,7 +1,9 @@
 from .pyssg import main
 from .custom_logger import setup_logger
+from .yaml_parser import setup_custom_yaml
 
 
 setup_logger()
+setup_custom_yaml()
 # not meant to be used as a package, so just give main
 __all__ = ['main']
diff --git a/src/pyssg/yaml_parser.py b/src/pyssg/yaml_parser.py
index fb19a23..2e1548b 100644
--- a/src/pyssg/yaml_parser.py
+++ b/src/pyssg/yaml_parser.py
@@ -13,6 +13,10 @@ def __join_constructor(loader: SafeLoader, node: SequenceNode) -> str:
     return ''.join([str(i) for i in seq])
 
 
+def setup_custom_yaml() -> None:
+    SafeLoader.add_constructor('!join', __join_constructor)
+
+
 def __read_raw_yaml(path: str) -> list[dict]:
     all_docs: list[dict] = []
     with open(path, 'r') as f:
@@ -28,6 +32,3 @@ def get_parsed_yaml(resource: str, package: str = '') -> list[dict]:
     log.debug('parsing yaml; reading "%s.%s"', package, resource)
     with rpath(package, resource) as p:
         return __read_raw_yaml(str(p))
-
-
-SafeLoader.add_constructor('!join', __join_constructor)
diff --git a/tests/conftest.py b/tests/conftest.py
index 58416ea..1beffc6 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,3 +1,4 @@
+import os
 import sys
 from typing import Callable
 import pytest
@@ -7,6 +8,11 @@ from pyssg.arg_parser import get_parser
 from pyssg.custom_logger import setup_logger
 
 
+@pytest.fixture
+def test_dir():
+    return os.path.dirname(os.path.abspath(__file__))
+
+
 @pytest.fixture(scope='session')
 def arg_parser():
     return get_parser()
diff --git a/tests/io_files/simple.yaml b/tests/io_files/simple.yaml
new file mode 100644
index 0000000..0b722a6
--- /dev/null
+++ b/tests/io_files/simple.yaml
@@ -0,0 +1,28 @@
+%YAML 1.2
+---
+define: &root "$HOME/pyssg/site_example/"
+
+title: "Example site"
+path:
+  src: !join [*root, "src"]
+  dst: !join [*root, "dst"]
+  plt: !join [*root, "plt"]
+  db: !join [*root, ".files"]
+url:
+  main: "https://example.com"
+  static: "https://static.example.com"
+  default_image: "images/default.png"
+fmt:
+  date: "%a, %b %d, %Y @ %H:%M %Z"
+  list_date: "%b %d"
+  list_sep_date: "%B %Y"
+dirs:
+  /:
+    cfg:
+      plt: "page.html"
+      tags: False
+      index: False
+      rss: False
+      sitemap: False
+      exclude_dirs: []
+...
\ No newline at end of file
diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py
new file mode 100644
index 0000000..9a1b3c3
--- /dev/null
+++ b/tests/test_yaml_parser.py
@@ -0,0 +1,19 @@
+from importlib.resources import path as rpath
+from pyssg.yaml_parser import get_parsed_yaml
+
+
+def test_yaml_resource_read() -> None:
+    yaml: list[dict] = get_parsed_yaml('simple.yaml', 'tests.io_files')
+    assert len(yaml) == 1
+
+
+def test_yaml_path_read(test_dir: str) -> None:
+    yaml: list[dict] = get_parsed_yaml(f'{test_dir}/io_files/simple.yaml')
+    assert len(yaml) == 1
+
+
+def test_yaml_join() -> None:
+    yaml: dict = get_parsed_yaml('simple.yaml', 'tests.io_files')[0]
+    define_str: str = '$HOME/pyssg/site_example/'
+    assert yaml['define'] == define_str
+    assert yaml['path']['src'] == f'{define_str}src'
-- 
cgit v1.2.3-70-g09d2