Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test_build.py 3.56 KiB
import re
from pathlib import Path

import pytest

from .utils import TEST_MODULE_NAME, get_env_var, run_make

MODULE_VERSION = "0.0.0+0"
MODULE_VERSION_NO_BUILD = "0.0.0"

RE_MISSING_FILE = "No rule to make target [`']{filename}'"

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"}],
    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",
        E3_MODULE_VERSION=MODULE_VERSION,
    )
    assert rc == 0
    assert "You are in the local source mode" in outs

    rc, _, _ = run_make(wrapper, "patch", E3_MODULE_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 "Bad" not in db_contents

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

    rc, _, _ = run_make(wrapper, "cellinstall", E3_MODULE_VERSION=MODULE_VERSION)
    assert rc == 0
    assert any((wrapper / "cellMods").glob("**/*.db"))


@pytest.mark.parametrize(
    "wrapper",
    [{"dbds": "nonexistent.dbd"}],
    indirect=True,
)
def test_missing_dbd_file(wrapper):
    rc, _, errs = run_make(wrapper, "build")

    assert rc == 2
    assert re.search(
        RE_MISSING_FILE.format(filename=re.escape("../nonexistent.dbd")),
        errs,
    )


@pytest.mark.parametrize(
    "wrapper",
    [{"sources": "nonexistent.c"}],
    indirect=True,
)
def test_missing_source_file(wrapper):
    rc, _, errs = run_make(wrapper, "build")

    assert rc == 2
    assert re.search(
        RE_MISSING_FILE.format(filename=re.escape("nonexistent.o")),
        errs,
    )


def test_missing_requirement(wrapper):
    with open(wrapper / f"{TEST_MODULE_NAME}.Makefile", "a") as f:
        f.write("REQUIRED += foo")

    rc, _, errs = run_make(wrapper, "build")
    assert rc == 2
    assert "REQUIRED module 'foo' version '' does not exist" in errs


def test_header_install_location(wrapper):
    subdir = wrapper / TEST_MODULE_NAME / "db" / "subdir"
    subdir.mkdir(parents=True)

    extensions = ["h", "hpp", "hxx", "hh"]
    with open(wrapper / f"{TEST_MODULE_NAME}.Makefile", "a") as f:
        for ext in extensions:
            f.write(f"HEADERS += db/subdir/header.{ext}\n")
        f.write("KEEP_HEADER_SUBDIRS += db\n")

    for extension in extensions:
        with open(subdir / f"header.{extension}", "w") as f:
            pass

    rc, _, _ = run_make(wrapper, "cellinstall")
    assert rc == 0

    cell_path = get_env_var(wrapper, "E3_MODULES_INSTALL_LOCATION")

    for ext in extensions:
        assert (Path(cell_path) / "include" / "subdir" / f"header.{ext}").is_file()
        assert not (Path(cell_path) / "include" / f"header.{ext}").is_file()