Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test_build.py 1.81 KiB
from re import sub
import pytest
import subprocess
from distutils import dir_util
from git import Repo
from pathlib import Path
from .utils import *


TEST_DATA = Path(__file__).absolute().parent / "data"
MODULE_VERSION = "0.0.0+0"
MODULE_VERSION_NO_BUILD = "0.0.0"

DB_FILE = """record(ai, "TEST") {
    
}
"""

PATCH_FILE = """
diff --git database.db database.db
index 1806ff6..8701832 100644
--- database.db
+++ database.db
@@ -1,3 +1,3 @@
 record(ai, "TEST") {{
-    
+    field(DESC, "{desc}")
 }}
"""


def create_patch_file(path, desc):
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w") as f:
        f.write(PATCH_FILE.format(desc=desc))


@pytest.mark.parametrize(
    "wrapper",
    [{"templates": "database.db", "sources": "", "dbds": "", "headers": ""}],
    indirect=True,
)
def test_patch(wrapper):
    db_path = wrapper / TEST_MODULE_NAME / "database.db"
    with open(db_path, "w") as f:
        f.write(DB_FILE)

    patch_dir = wrapper / "patch" / "Site"
    create_patch_file(patch_dir / MODULE_VERSION / "apply.p0.patch", "OK")
    create_patch_file(
        patch_dir / MODULE_VERSION_NO_BUILD / "dont-apply.p0.patch", "Bad"
    )
    create_patch_file(patch_dir / (MODULE_VERSION + "-dont-apply.p0.patch"), "Bad")

    rc, outs, _ = run_make(
        wrapper,
        "init",
        __DEBUG_VERSION=MODULE_VERSION,
    )
    assert rc == 0
    assert "You are in the local source mode" in outs.decode("utf-8")

    rc, _, _ = run_make(wrapper, "patch", __DEBUG_VERSION=MODULE_VERSION)
    assert rc == 0
    with open(db_path, "r") as f:
        db_contents = f.read()
    assert 'field(DESC, "OK")' in db_contents
    assert not "Bad" in db_contents

    rc, _, _ = run_make(wrapper, "build", __DEBUG_VERSION=MODULE_VERSION)
    assert rc == 0
    assert any((wrapper / TEST_MODULE_NAME).glob("O.*"))