Skip to content

Commit

Permalink
Add integration test that builds a project
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkwah committed Feb 26, 2025
1 parent df9e984 commit c105748
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/deploy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def __init__(
depends: list[Package],
) -> None:
self.configpath = configpath
self.extra_scripts = extra_scripts
self.storepath = storepath
self.cachepath = cachepath
self.config = config
self.depends = depends
self.extra_scripts = extra_scripts

@property
def fullname(self) -> str:
Expand Down Expand Up @@ -189,12 +189,12 @@ def __init__(
system: bool = False,
force: bool = False,
) -> None:
self.force = force
self.base = Path(
self.force: bool = force
self.base: Path = Path(
config.paths.system_base if system else config.paths.local_base
)
self.storepath = self.base / config.paths.store
self.cachepath = Path("tmp").resolve()
self.storepath: Path = self.base / config.paths.store
self.cachepath: Path = Path("tmp").resolve()
buildmap = {x.name: x for x in config.builds}

graph: nx.DiGraph[str] = nx.DiGraph()
Expand Down
95 changes: 95 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from __future__ import annotations
import os
from pathlib import Path
from deploy.build import Build
from deploy.config import Config
import pytest
import subprocess
import shutil


DIR = Path(os.path.dirname(__file__))


class Git:
def __init__(self, path: Path, initial_commit: Path | None = None) -> None:
self.path: Path = path

if initial_commit:
self.initial_commit(initial_commit)

def initial_commit(self, file: Path) -> None:
shutil.copy(file, self.path / file.name)

self("init", "-b", "main")
self("config", "user.email", "user@example.com")
self("config", "user.name", "Ola Nordmann")
self("add", file.name)
self("commit", "-m", "Initial commit")

def __call__(self, *args: str | Path) -> str:
return subprocess.check_output(["git", *args], cwd=self.path).decode("utf-8")

@property
def ref(self) -> str:
return self("rev-parse", "HEAD").strip()


@pytest.fixture(scope="session")
def git_foo(tmp_path_factory: pytest.TempPathFactory) -> Git:
tmp_path = tmp_path_factory.mktemp("foo.git")
return Git(tmp_path, DIR / "data/foo.c")


@pytest.fixture(scope="session")
def git_bar(tmp_path_factory: pytest.TempPathFactory) -> Git:
tmp_path = tmp_path_factory.mktemp("bar.git")
return Git(tmp_path, DIR / "data/bar.c")


@pytest.fixture()
def config(tmp_path: Path, git_foo: Git, git_bar: Git) -> Config:
foo_config = {
"name": "foo",
"version": "1.2.3",
"src": {
"type": "git",
"url": f"file://{git_foo.path}",
"ref": git_foo.ref,
},
}

bar_config = {
"name": "bar",
"version": "3.2.1",
"src": {
"type": "git",
"url": f"file://{git_bar.path}",
"ref": git_bar.ref,
},
"depends": ["foo"],
}

base_config = {
"paths": {
"local_base": tmp_path / "output",
"system_base": "/dev/null",
"store": "versions/.store",
},
"builds": [foo_config, bar_config],
"envs": [],
"links": {},
}

return Config.model_validate(base_config)


@pytest.fixture
def build(config: Config) -> Build:
return Build(
Path("/dev/null"),
config,
system=False,
force=False,
extra_scripts=DIR / "data/scripts",
)
7 changes: 7 additions & 0 deletions tests/integration/data/bar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>

const char *foo_get_text();

int main() {
printf("%s\n", foo_get_text());
}
3 changes: 3 additions & 0 deletions tests/integration/data/foo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const char *foo_get_text() {
return "I am a test";
}
4 changes: 4 additions & 0 deletions tests/integration/data/scripts/build_bar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

mkdir $out/bin
cc -o $out/bin/bar $src/bar.c -L$foo/lib -lfoo -Wl,-rpath,$foo/lib
4 changes: 4 additions & 0 deletions tests/integration/data/scripts/build_foo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

mkdir $out/lib
cc -shared -fPIC -o $out/lib/libfoo.so $src/foo.c
18 changes: 18 additions & 0 deletions tests/integration/test_build_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import subprocess
from deploy.build import Build


def test_build(build: Build) -> None:
assert set(build.packages) == {"foo", "bar"}


def test_checkout(build: Build) -> None:
build.build()

out = build.packages["foo"].out
assert (out / "lib/libfoo.so").is_file()

out = build.packages["bar"].out
assert (out / "bin/bar").is_file()

assert subprocess.check_output([out / "bin/bar"]) == b"I am a test\n"

0 comments on commit c105748

Please sign in to comment.