Commit 99bab6e5 authored by Tomas Krizek's avatar Tomas Krizek
Browse files

Merge branch 'self-tests' into 'master'

tests: implement self tests for archive and srcpkg

See merge request !7
parents 44adf8f9 a37f6e24
Pipeline #73272 passed with stages
in 1 minute and 32 seconds
......@@ -35,6 +35,7 @@ unit-py35:
stage: test-py35
script:
# use py.test directly to test system packages
- python3 setup.py develop --user
- py.test-3 tests/unit
self-py35:
......@@ -42,6 +43,7 @@ self-py35:
stage: test-py35
script:
# use py.test directly to test system packages
- python3 setup.py develop --user
- py.test-3 tests/self
......
"""
python version compatibility helpers
"""
import sys
# python 3.5 doesn't have pathlib fully integrated so str() conversion is
# needed in some places - use py35path() for easy removal later
if sys.version_info.minor <= 5:
def py35path(s):
return str(s)
else:
def py35path(s):
return s
......@@ -7,6 +7,7 @@ import shutil
from apkg import exception
from apkg import log
from apkg.compat import py35path
from apkg.parse import split_archive_fn
from apkg.project import Project
from apkg.util.run import run
......@@ -37,8 +38,7 @@ def make_archive(version=None, project=None):
"(indicated by last script stdout line) doesn't exist:\n\n"
"%s" % archive_path)
raise exception.UnexpectedCommandOutput(msg=msg)
else:
log.info("archive created: %s" % archive_path)
log.info("archive created: %s" % archive_path)
archive_fn = archive_path.name
if version:
......@@ -50,14 +50,14 @@ def make_archive(version=None, project=None):
log.info(msg, archive_fn)
out_path = proj.dev_archive_path / archive_fn
log.info("copying archive to: %s" % out_path)
os.makedirs(proj.dev_archive_path, exist_ok=True)
shutil.copy(archive_path, out_path)
os.makedirs(py35path(proj.dev_archive_path), exist_ok=True)
shutil.copy(py35path(archive_path), py35path(out_path))
log.success("made archive: %s", out_path)
return out_path
def get_archive(version=None, project=None):
raise exception.NotImplemented(
raise exception.ApkgException(
msg="TODO: get_archive: download upstream archive")
......
......@@ -5,6 +5,7 @@ import os
import shutil
from apkg import adistro
from apkg.compat import py35path
from apkg import exception
from apkg import log
from apkg.project import Project
......@@ -65,14 +66,14 @@ def make_source_package(
if build_path.exists():
log.info("removing existing build dir: %s" % build_path)
shutil.rmtree(build_path)
os.makedirs(build_path, exist_ok=True)
os.makedirs(py35path(build_path), exist_ok=True)
# ensure output dir doesn't exist
if out_path.exists():
log.info("removing existing result dir: %s" % out_path)
shutil.rmtree(out_path)
# prepare vars accessible from templates
vars = {
env = {
'name': pkg_name,
'version': version,
'release': release,
......@@ -85,7 +86,7 @@ def make_source_package(
out_path,
archive_path=ar_path,
package_template=template,
vars=vars)
env=env)
if out_path.exists():
log.success("made source package: %s", out_path)
......
......@@ -39,10 +39,10 @@ def build_source_package(
out_path,
archive_path,
package_template,
vars):
env):
in_pkgbuild = build_path / 'PKGBUILD'
log.info("building arch source package: %s" % in_pkgbuild)
package_template.render(build_path, vars or {})
package_template.render(build_path, env or {})
os.makedirs(out_path)
log.info("copying PKGBUILD and archive to: %s" % out_path)
shutil.copyfile(in_pkgbuild, out_path / 'PKGBUILD')
......
......@@ -8,6 +8,7 @@ from pathlib import Path
import re
import shutil
from apkg.compat import py35path
from apkg import exception
from apkg import log
from apkg import parse
......@@ -42,17 +43,18 @@ def get_package_name(path):
msg="unable to determine Source from: %s" % control)
# pylint: disable=too-many-locals
def build_source_package(
build_path,
out_path,
archive_path,
package_template,
vars):
nv = "%s-%s" % (vars['name'], vars['version'])
env):
nv = "%s-%s" % (env['name'], env['version'])
source_path = build_path / nv
log.info("building deb source package: %s" % nv)
log.info("unpacking archive: %s" % archive_path)
os.makedirs(source_path)
os.makedirs(py35path(source_path))
run('aunpack', '-X', build_path, archive_path)
if not source_path.exists():
# NOTE: if this happens oftern (it shouldn't), consider using
......@@ -61,13 +63,13 @@ def build_source_package(
raise exception.UnexpectedCommandOutput(msg=msg)
# render template
debian_path = source_path / 'debian'
package_template.render(debian_path, vars)
package_template.render(debian_path, env)
# copy archive with debian .orig name
_, _, _, ext = parse.split_archive_fn(archive_path.name)
debian_ar = "%s_%s.orig%s" % (vars['name'], vars['version'], ext)
debian_ar = "%s_%s.orig%s" % (env['name'], env['version'], ext)
debian_ar_path = build_path / debian_ar
log.info("copying archive into source package: %s", debian_ar_path)
shutil.copyfile(archive_path, debian_ar_path)
shutil.copyfile(py35path(archive_path), py35path(debian_ar_path))
log.info("building deb source-only package...")
direct = bool(log.log.level <= log.INFO)
......@@ -75,11 +77,11 @@ def build_source_package(
run('dpkg-buildpackage', '-S', '-d', '-nc', '-sa', direct=direct)
log.info("copying source package to result dir: %s", out_path)
os.makedirs(out_path)
os.makedirs(py35path(out_path))
for f in glob.iglob("%s/*" % build_path):
src = Path(f)
if not src.is_file():
continue
dst = out_path / src.relative_to(build_path)
log.verbose("copying file to result dir: %s", dst)
shutil.copyfile(src, dst)
shutil.copyfile(py35path(src), py35path(dst))
......@@ -18,10 +18,10 @@ SUPPORTED_DISTROS = [
def is_valid_package_template(path):
for spec in glob.iglob("%s/*.spec" % path):
for _ in glob.iglob("%s/*.spec" % path):
return True
return False
def get_package_name(path):
return "TODO-rpm"
return "TODO: rpm @ %s" % path
......@@ -5,6 +5,7 @@ import os
from pathlib import Path
import jinja2
from apkg.compat import py35path
from apkg import log
from apkg import pkgstyle
......@@ -28,17 +29,17 @@ class PackageTemplate:
if out_path.exists():
log.verbose("template render dir exists: %s" % out_path)
else:
os.makedirs(out_path, exist_ok=True)
os.makedirs(py35path(out_path), exist_ok=True)
# recursively render all files
for d, _, files in os.walk(self.path):
for d, _, files in os.walk(py35path(self.path)):
rel_dir = Path(d).relative_to(self.path)
dst_dir = out_path / rel_dir
os.makedirs(dst_dir, exist_ok=True)
os.makedirs(py35path(dst_dir), exist_ok=True)
for fn in files:
dst = out_path / rel_dir / fn
src = Path(dir) / fn
src = Path(d) / fn
log.verbose("rendering file: %s -> %s", src, dst)
t = None
with src.open('r') as srcf:
......
......@@ -76,7 +76,7 @@ class Project:
def load_config(self):
if self.config_path.exists():
log.verbose("loading project config: %s" % self.config_path)
self.config = toml.load(self.config_path)
self.config = toml.load(self.config_path.open())
return True
else:
log.verbose("project config not found: %s" % self.config_path)
......@@ -112,8 +112,8 @@ class Project:
def load_package_templates(path):
package_templates = []
for entry_path in os.scandir(path):
if entry_path.is_dir():
for entry_path in glob.glob('%s/*' % path):
if os.path.isdir(entry_path):
template = pkgtemplate.PackageTemplate(entry_path)
if template.package_style:
package_templates.append(template)
......
......@@ -3,6 +3,7 @@ from contextlib import contextmanager
import os
import subprocess
from apkg.compat import py35path
from apkg import exception
from apkg import log
......@@ -102,7 +103,7 @@ def cd(newdir):
Temporarily change current directory.
"""
olddir = os.getcwd()
os.chdir(os.path.expanduser(newdir))
os.chdir(os.path.expanduser(py35path(newdir)))
try:
yield
finally:
......
"""
shared apkg testing functions
"""
from pathlib import Path
import shutil
from apkg.compat import py35path
def init_testing_repo(repo_path, test_path):
dst = Path(test_path) / Path(repo_path).name
shutil.copytree(py35path(repo_path), py35path(dst))
return dst
......@@ -13,5 +13,9 @@ RUN apt-get upgrade -y -qqq
# basic deps
RUN apt-get install -y -qqq python3-pbr python3-jinja2 python3-docopt python3-blessings python3-toml
# non-python deps
RUN apt-get install -y -qqq git atool
# packaging deps
RUN apt-get install -y -qqq dpkg-dev dh-python
# testing deps
RUN apt-get install -y -qqq tox python3-pytest python3-pytest-cov
......@@ -13,6 +13,10 @@ RUN apt-get upgrade -y -qqq
# basic deps
RUN apt-get install -y -qqq python3-pbr python3-jinja2 python3-distro python3-docopt python3-blessings python3-toml
# non-python deps
RUN apt-get install -y -qqq git atool
# packaging deps
RUN apt-get install -y -qqq dpkg-dev dh-python
# testing deps
RUN apt-get install -y -qqq tox python3-pytest python3-pytest-sugar python3-pytest-cov
# linting deps
......
def common_self_fun():
print("self tests common fun")
import self_common
from pathlib import Path
import re
from apkg.util import test
from apkg.util.run import cd
from apkg.cli import apkg
def test_unit_dummy():
print("dummy unit test")
APKG_BASE_DIR = Path(__file__).parents[2]
def test_unit_common():
self_common.common_self_fun()
# NOTE(py35): use tmp_path instead of tmpdir
# when py3.5 support is dropped
def test_apkg_archive(tmpdir, capsys):
repo_path = test.init_testing_repo(APKG_BASE_DIR, str(tmpdir))
repo_dir = str(repo_path)
assert repo_dir.endswith('apkg')
with cd(repo_dir):
apkg('make-archive')
out, err = capsys.readouterr()
# last stdout line should be resulting archive
assert re.search(r"pkg/archives/dev/apkg-.*\.tar\.gz$", out)
def test_apkg_srcpkg(tmpdir, capsys):
repo_path = test.init_testing_repo(APKG_BASE_DIR, str(tmpdir))
repo_dir = str(repo_path)
assert repo_dir.endswith('apkg')
with cd(repo_dir):
apkg('srcpkg')
out, err = capsys.readouterr()
# last stdout line should be resulting source package
assert re.search(r"pkg/srcpkgs/\S+/apkg-\S+$", out)
import unit_common
def test_unit_dummy():
print("dummy unit test")
def test_unit_common():
unit_common.common_unit_fun()
def common_unit_fun():
print("common unit fun")
......@@ -3,6 +3,10 @@ envlist = unit,self,flake8,pylint
skipsdist = True
[testenv]
usedevelop = True
[testenv:unit]
deps =
-rrequirements.txt
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment