Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions infra/tests/test_build02_osgi_clauses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
"""build02 grades an OSGi header as clauses, the way its instruction says.

Reordering must pass; every difference the instruction calls graded must fail.
"""

from __future__ import annotations

import importlib.util
import os
import sys
from pathlib import Path

import pytest

#: The module under test declares the stage markers its own suite.toml
#: registers, and this root does not: importing it here is not the place to
#: relitigate that, so the resulting warnings are silenced rather than the
#: shared ini file widened for one test.
pytestmark = pytest.mark.filterwarnings(
"ignore::pytest.PytestUnknownMarkWarning")

REPO = Path(__file__).resolve().parents[2]
SUITE = REPO / "tasks" / "build02-gson-maven-to-gradle" / "tests" / "behavioural"

STATE_A_EXPORT = (
'com.google.gson;uses:="com.google.gson.reflect,com.google.gson.stream";'
'version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"'
)
STATE_A_IMPORT = 'sun.misc;resolution:=optional,com.google.gson.annotations'


@pytest.fixture(scope="module")
def manifest_module():
"""build02's manifest module, imported without a stage image."""
sys.path.insert(0, str(SUITE / "lib"))
os.environ["SRB_SUITE_DIR"] = str(SUITE)
try:
spec = importlib.util.spec_from_file_location(
"build02_test_manifest", SUITE / "modules" / "manifest" / "test_manifest.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
yield module
finally:
sys.path.remove(str(SUITE / "lib"))


class FakeJar:
"""Just the one thing the manifest checks read."""

def __init__(self, headers):
self.manifest = dict(headers)


def _check(module, header, produced, want):
"""Run the real check, and say whether it passed."""
jar = FakeJar({header: produced})
try:
module.test_required_header(jar, header, want)
except AssertionError:
return False
return True


# ------------------------------------------------ what must stop being graded
REORDERINGS = {
"attributes within a clause": (
'com.google.gson;version="2.10.1";'
'uses:="com.google.gson.reflect,com.google.gson.stream",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"'
),
"the uses set": (
'com.google.gson;uses:="com.google.gson.stream,com.google.gson.reflect";'
'version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"'
),
"the clauses themselves": (
'com.google.gson.stream;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson;uses:="com.google.gson.reflect,com.google.gson.stream";'
'version="2.10.1"'
),
"whitespace around the separators": (
'com.google.gson; uses:="com.google.gson.reflect, com.google.gson.stream"; '
'version="2.10.1", '
'com.google.gson.annotations; version="2.10.1", '
'com.google.gson.reflect; version="2.10.1", '
'com.google.gson.stream; version="2.10.1"'
),
}


@pytest.mark.parametrize("what", sorted(REORDERINGS), ids=sorted(REORDERINGS))
def test_reordering_passes(manifest_module, what):
"""The header means the same thing, so it is the same header."""
assert _check(manifest_module, "Export-Package",
REORDERINGS[what], STATE_A_EXPORT), what


def test_the_reorderings_really_are_reorderings():
"""Each case above differs from State A only in order."""
for what, produced in REORDERINGS.items():
assert produced != STATE_A_EXPORT, what
assert sorted(produced.replace(" ", "")) == \
sorted(STATE_A_EXPORT.replace(" ", "")), what


# ------------------------------------------------- what must still be graded
DEFECTS = {
"a dropped uses directive":
'com.google.gson;version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"',
"a uses set with a package missing":
'com.google.gson;uses:="com.google.gson.reflect";version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"',
"a uses set with a package added":
'com.google.gson;uses:="com.google.gson.reflect,com.google.gson.stream,'
'com.google.gson.internal";version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"',
"a wrong export version":
'com.google.gson;uses:="com.google.gson.reflect,com.google.gson.stream";'
'version="2.10.0",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"',
"a package that is not exported":
'com.google.gson;uses:="com.google.gson.reflect,com.google.gson.stream";'
'version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1"',
"an internal package exported as well":
'com.google.gson;uses:="com.google.gson.reflect,com.google.gson.stream";'
'version="2.10.1",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1",'
'com.google.gson.internal;version="2.10.1"',
"a version attribute with no value":
'com.google.gson;uses:="com.google.gson.reflect,com.google.gson.stream",'
'com.google.gson.annotations;version="2.10.1",'
'com.google.gson.reflect;version="2.10.1",'
'com.google.gson.stream;version="2.10.1"',
}


@pytest.mark.parametrize("what", sorted(DEFECTS), ids=sorted(DEFECTS))
def test_a_real_difference_still_fails(manifest_module, what):
assert not _check(manifest_module, "Export-Package",
DEFECTS[what], STATE_A_EXPORT), what


IMPORT_DEFECTS = {
"resolution:=optional dropped":
"sun.misc,com.google.gson.annotations",
"resolution:=mandatory instead":
"sun.misc;resolution:=mandatory,com.google.gson.annotations",
"an import that is not there":
"sun.misc;resolution:=optional",
}


@pytest.mark.parametrize("what", sorted(IMPORT_DEFECTS), ids=sorted(IMPORT_DEFECTS))
def test_a_real_import_difference_still_fails(manifest_module, what):
assert not _check(manifest_module, "Import-Package",
IMPORT_DEFECTS[what], STATE_A_IMPORT), what


def test_import_clause_order_passes(manifest_module):
assert _check(manifest_module, "Import-Package",
"com.google.gson.annotations,sun.misc;resolution:=optional",
STATE_A_IMPORT)


def test_a_directive_is_not_an_attribute(manifest_module):
"""``resolution=optional`` is a different claim from ``resolution:=optional``."""
assert not _check(manifest_module, "Import-Package",
"sun.misc;resolution=optional,com.google.gson.annotations",
STATE_A_IMPORT)


# --------------------------------------------------- the other eleven headers
def test_a_scalar_header_is_still_byte_exact(manifest_module):
"""Only the two clause headers moved; the rest are unchanged."""
header = "Bundle-RequiredExecutionEnvironment"
want = "JavaSE-1.7, JavaSE-1.8"
assert _check(manifest_module, header, want, want)
assert not _check(manifest_module, header, "JavaSE-1.8, JavaSE-1.7", want)
assert not _check(manifest_module, header, "JavaSE-1.7,JavaSE-1.8", want)


def test_an_absent_header_still_fails(manifest_module):
assert not _check(manifest_module, "Export-Package", None, STATE_A_EXPORT)


def test_state_a_passes_itself(manifest_module):
"""The ground truth in data/manifest.json agrees with the strings above."""
ground = manifest_module.MF["required"]
assert ground["Export-Package"] == STATE_A_EXPORT
assert ground["Import-Package"] == STATE_A_IMPORT
for header, want in ground.items():
assert _check(manifest_module, header, want, want), header
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ def split_osgi_clauses(value):


def parse_osgi_header(value):
"""`pkg;a=b,pkg2;c="d"` -> {pkg: {a: b}, pkg2: {c: d}}."""
"""`pkg;a=b,pkg2;c:="d"` -> {pkg: {"a": b}, pkg2: {"c:": d}}.

A directive keeps its colon: OSGi gives `a=b` and `a:=b` different meanings.
"""
out = {}
for clause in split_osgi_clauses(value):
parts = clause.split(";")
Expand All @@ -79,11 +82,27 @@ def parse_osgi_header(value):
if "=" not in p:
continue
k, v = p.split("=", 1)
attrs[k.strip().rstrip(":")] = v.strip().strip('"')
attrs[k.strip()] = v.strip().strip('"')
out[parts[0].strip()] = attrs
return out


#: Directives whose value is a set written as a comma-separated list.
OSGI_SET_VALUED = frozenset({"uses:"})


def osgi_header_meaning(value):
"""An OSGi header as what it means: clause and attribute order dropped."""
return {
pkg: {
key: (frozenset(p.strip() for p in val.split(",") if p.strip())
if key in OSGI_SET_VALUED else val)
for key, val in attrs.items()
}
for pkg, attrs in parse_osgi_header(value).items()
}


def exported_packages(manifest):
"""Package -> attributes from Export-Package, or {} when absent."""
return parse_osgi_header(manifest.get("Export-Package", ""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

Headers fall into three groups, decided by measurement, not by taste:

required 13 headers that describe the bundle itself. Byte-exact.
required 13 headers that describe the bundle itself. Byte-exact, except
``Export-Package`` and ``Import-Package``, which the instruction
compares clause by clause.
provenance 4 headers bnd-maven-plugin synthesises from the POM's
``<developers>``, ``<url>``, ``<licenses>`` and ``<scm>``. A Gradle
build has no POM to read them from, so they are graded separately.
Expand Down Expand Up @@ -57,6 +59,10 @@ def _prov_norm(value):
return out.rstrip("/")


#: The headers the instruction compares clause by clause.
CLAUSE_HEADERS = ("Export-Package", "Import-Package")


def _clauses(value):
return jarinspect.parse_osgi_header(value)

Expand Down Expand Up @@ -102,18 +108,25 @@ def test_manifest_was_generated_not_handwritten(gson_jar, data):
imp = mf.get("Import-Package")
assert imp, "no Import-Package header: bnd never analysed the bundle"
want = data["manifest"]["required"]["Import-Package"]
assert imp == want, (
"Import-Package is\n %r\nState A computes\n %r" % (imp, want))
assert jarinspect.osgi_header_meaning(imp) == \
jarinspect.osgi_header_meaning(want), (
"Import-Package is\n %r\nState A computes\n %r" % (imp, want))


# --------------------------------------------------------------- exact headers
# ------------------------------------------------------------ required headers
@pytest.mark.behavioural
@pytest.mark.parametrize("header,want", REQUIRED, ids=[h for h, _ in REQUIRED])
def test_required_header_exact(gson_jar, header, want):
"""This header matches State A byte for byte."""
def test_required_header(gson_jar, header, want):
"""This header carries State A's value, byte for byte or clause by clause."""
mf = _manifest(gson_jar)
got = mf.get(header)
assert got is not None, "manifest has no %s header" % header
if header in CLAUSE_HEADERS:
assert jarinspect.osgi_header_meaning(got) == \
jarinspect.osgi_header_meaning(want), (
"%s differs by more than clause order.\n State A : %s\n"
" produced: %s" % (header, want, got))
return
assert got == want, (
"%s differs.\n State A : %s\n produced: %s" % (header, want, got))

Expand Down Expand Up @@ -236,7 +249,7 @@ def test_sun_misc_import_is_optional(gson_jar):
mf = _manifest(gson_jar)
got = _clauses(mf.get("Import-Package", ""))
assert "sun.misc" in got, "sun.misc is not imported at all"
assert got["sun.misc"].get("resolution") == "optional", (
assert got["sun.misc"].get("resolution:") == "optional", (
"sun.misc must be imported with resolution:=optional, got %r"
% got["sun.misc"])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ def split_osgi_clauses(value):


def parse_osgi_header(value):
"""`pkg;a=b,pkg2;c="d"` -> {pkg: {a: b}, pkg2: {c: d}}."""
"""`pkg;a=b,pkg2;c:="d"` -> {pkg: {"a": b}, pkg2: {"c:": d}}.

A directive keeps its colon: OSGi gives `a=b` and `a:=b` different meanings.
"""
out = {}
for clause in split_osgi_clauses(value):
parts = clause.split(";")
Expand All @@ -79,11 +82,27 @@ def parse_osgi_header(value):
if "=" not in p:
continue
k, v = p.split("=", 1)
attrs[k.strip().rstrip(":")] = v.strip().strip('"')
attrs[k.strip()] = v.strip().strip('"')
out[parts[0].strip()] = attrs
return out


#: Directives whose value is a set written as a comma-separated list.
OSGI_SET_VALUED = frozenset({"uses:"})


def osgi_header_meaning(value):
"""An OSGi header as what it means: clause and attribute order dropped."""
return {
pkg: {
key: (frozenset(p.strip() for p in val.split(",") if p.strip())
if key in OSGI_SET_VALUED else val)
for key, val in attrs.items()
}
for pkg, attrs in parse_osgi_header(value).items()
}


def exported_packages(manifest):
"""Package -> attributes from Export-Package, or {} when absent."""
return parse_osgi_header(manifest.get("Export-Package", ""))
Expand Down
Loading