Repository: yuvipanda/pre-commit-hook-ensure-sops (language: python)

Issue:
Add unit tests for the package
# Problem

There is no way to verify that the package works without some kind of test framework that can be run after installation.

# Solution

I suggest starting with a few simple tests for the `validate_enc` and `check_file` functions. `pytest`seems reasonable as the test runner.

Let me know if you want me to make a pull request for this! I would add `pytest` to `setup.py` `test_requires` dependencies and draft a few simple tests that check a valid and invalid file at the very least.
Add unit tests for the package
# Problem

There is no way to verify that the package works without some kind of test framework that can be run after installation.

# Solution

I suggest starting with a few simple tests for the `validate_enc` and `check_file` functions. `pytest`seems reasonable as the test runner.

Let me know if you want me to make a pull request for this! I would add `pytest` to `setup.py` `test_requires` dependencies and draft a few simple tests that check a valid and invalid file at the very least.


Discussion / hints:
Yes, would *love* to have a PR with some tests! Thank you so much!
Yes, would *love* to have a PR with some tests! Thank you so much!

Fix patch:
diff --git a/setup.py b/setup.py
index ce171fe..9a687ea 100644
--- a/setup.py
+++ b/setup.py
@@ -16,4 +16,7 @@
     install_requires=[
         "ruamel.yaml",
     ],
+    test_requires=[
+        "pytest",
+    ],
 )


Test patch:
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_main.py b/tests/test_main.py
new file mode 100644
index 0000000..208a122
--- /dev/null
+++ b/tests/test_main.py
@@ -0,0 +1,51 @@
+"""
+Tests for main functions.
+"""
+from contextlib import nullcontext
+from pathlib import Path
+
+import pytest
+from ruamel.yaml.parser import ParserError
+
+from pre_commit_hook_ensure_sops.__main__ import check_file
+
+VALID_SECRET = """
+foo: ENC[AES256_GCM,data:9LiS,iv:B/Add+R3lTSx66Qrq8/+jFD2mok8GdD7R32uAf04+Ho=,tag:iyCZ1thFop63/2L+skFcdg==,type:str]
+sops:
+"""
+
+INVALID_SECRET = """
+foo: this_is_not_encrypted_text
+sops:
+"""
+
+NO_SOPS_METADATA = """
+foo: ENC[AES256_GCM,data:9LiS,iv:B/Add+R3lTSx66Qrq8/+jFD2mok8GdD7R32uAf04+Ho=,tag:iyCZ1thFop63/2L+skFcdg==,type:str]
+"""
+
+INVALID_YAML = """
+this_is_not_valid_yaml
+"""
+
+
+@pytest.mark.parametrize(
+    "text,is_valid",
+    [
+        pytest.param(INVALID_SECRET, False, id="invalid_secret"),
+        pytest.param(VALID_SECRET, True, id="valid_secret"),
+        pytest.param(NO_SOPS_METADATA, False, id="no_sops_metadata"),
+        pytest.param(INVALID_YAML, False, id="invalid_yaml"),
+    ],
+)
+def test_check_file_validity(text, is_valid, tmp_path: Path):
+    """
+    Test check_file.
+
+    Checks with known valid and invalid yaml texts and checks
+    if an error is raised with invalid one.
+    """
+    filepath = tmp_path / "test.yaml"
+    filepath.write_text(text)
+    filename = filepath.as_posix()
+    result = check_file(filename=filename)
+    assert result[0] == is_valid