Skip to content
Snippets Groups Projects
test_build.py 2.25 KiB
Newer Older
Simon Rose's avatar
Simon Rose committed
import pytest

Simon Rose's avatar
Simon Rose committed
from .utils import TEST_MODULE_NAME, run_make
Simon Rose's avatar
Simon Rose committed

Simon Rose's avatar
Simon Rose committed
MODULE_VERSION = "0.0.0+0"
Simon Rose's avatar
Simon Rose committed
MODULE_VERSION_NO_BUILD = "0.0.0"
Simon Rose's avatar
Simon Rose committed

DB_FILE = """record(ai, "TEST") {
Simon Rose's avatar
Simon Rose committed

Simon Rose's avatar
Simon Rose committed
}
"""

PATCH_FILE = """
diff --git database.db database.db
index 1806ff6..8701832 100644
--- database.db
+++ database.db
@@ -1,3 +1,3 @@
 record(ai, "TEST") {{
Simon Rose's avatar
Simon Rose committed
-
Simon Rose's avatar
Simon Rose committed
+    field(DESC, "{desc}")
 }}
"""
Simon Rose's avatar
Simon Rose committed
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))


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

Simon Rose's avatar
Simon Rose committed
    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")
Simon Rose's avatar
Simon Rose committed
    rc, outs, _ = run_make(
Simon Rose's avatar
Simon Rose committed
        wrapper,
Simon Rose's avatar
Simon Rose committed
        "init",
Simon Rose's avatar
Simon Rose committed
        E3_MODULE_VERSION=MODULE_VERSION,
Simon Rose's avatar
Simon Rose committed
    )
Simon Rose's avatar
Simon Rose committed
    assert rc == 0
Simon Rose's avatar
Simon Rose committed
    assert "You are in the local source mode" in outs.decode("utf-8")

Simon Rose's avatar
Simon Rose committed
    rc, _, _ = run_make(wrapper, "patch", E3_MODULE_VERSION=MODULE_VERSION)
Simon Rose's avatar
Simon Rose committed
    assert rc == 0
Simon Rose's avatar
Simon Rose committed
    with open(db_path, "r") as f:
Simon Rose's avatar
Simon Rose committed
        db_contents = f.read()
    assert 'field(DESC, "OK")' in db_contents
Simon Rose's avatar
Simon Rose committed
    assert "Bad" not in db_contents
Simon Rose's avatar
Simon Rose committed
    rc, _, _ = run_make(wrapper, "build", E3_MODULE_VERSION=MODULE_VERSION)
Simon Rose's avatar
Simon Rose committed
    assert rc == 0
    assert any((wrapper / TEST_MODULE_NAME).glob("O.*"))
Simon Rose's avatar
Simon Rose committed
    rc, _, _ = run_make(wrapper, "cellinstall", E3_MODULE_VERSION=MODULE_VERSION)
    assert any((wrapper / "cellMods").glob("**/*.db"))


@pytest.mark.parametrize(
    "wrapper",
    [{"templates": "", "sources": "", "dbds": "nonexistent.dbd", "headers": ""}],
    indirect=True,
)
def test_missing_file(wrapper):
Simon Rose's avatar
Simon Rose committed
    rc, _, errs = run_make(wrapper, "build")
Simon Rose's avatar
Simon Rose committed

    # TODO: This should probably be a regex, since the quote marks seem to
    #       depend on the make version.
    assert "No rule to make target `../nonexistent.dbd'" in errs.decode("utf-8")
    assert rc == 2