From be18f9ef940e65820b07e671780acfb022200999 Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Sat, 9 Sep 2023 23:16:38 +0100 Subject: [PATCH] test: add tests for auto-checksum Add a unit test to validate the auto-checksum feature, that it fixes local checksums, doesn't fix remote checksums, and that the APKBUILD can still be parsed afterwards. Signed-off-by: Caleb Connolly --- test/test_build_auto_checksum.py | 63 +++++++++++++++++++ .../apkbuild/checksum/APKBUILD.inline | 13 ++++ .../apkbuild/checksum/APKBUILD.multiline | 15 +++++ test/testdata/apkbuild/checksum/Makefile | 5 ++ test/testdata/apkbuild/checksum/main.c | 7 +++ 5 files changed, 103 insertions(+) create mode 100644 test/test_build_auto_checksum.py create mode 100644 test/testdata/apkbuild/checksum/APKBUILD.inline create mode 100644 test/testdata/apkbuild/checksum/APKBUILD.multiline create mode 100644 test/testdata/apkbuild/checksum/Makefile create mode 100644 test/testdata/apkbuild/checksum/main.c diff --git a/test/test_build_auto_checksum.py b/test/test_build_auto_checksum.py new file mode 100644 index 00000000..062db332 --- /dev/null +++ b/test/test_build_auto_checksum.py @@ -0,0 +1,63 @@ +# Copyright 2023 Caleb Connolly +# SPDX-License-Identifier: GPL-3.0-or-later +import os +import pytest +import sys + +import pmb_test +import pmb_test.const +import pmb.parse._apkbuild + + +@pytest.fixture +def args(tmpdir, request): + import pmb.parse + sys.argv = ["pmbootstrap.py", "init"] + args = pmb.parse.arguments() + args.log = args.work + "/log_testsuite.txt" + pmb.helpers.logging.init(args) + request.addfinalizer(pmb.helpers.logging.logfd.close) + return args + +testfiles = ["APKBUILD.inline", "APKBUILD.multiline"] + +@pytest.mark.parametrize('apkbuild', testfiles) +def test_auto_checksum_inline(args, tmpdir, apkbuild): + """ + Validate that the checksums are correctly calculated for inline sources + """ + testdata = pmb_test.const.testdata + pmaportdir = os.path.join(testdata, "apkbuild/checksum") + path = os.path.join(pmaportdir, apkbuild) + apkbuild = pmb.parse.apkbuild(path, check_pkgname=False) + + assert apkbuild["sha512sums"] == { + "main.c": "d06f00d", + "Makefile": "baddeed", + "invalid-url": "d00d00d", + } + + path_tmp = os.path.join(tmpdir, "APKBUILD") + + # Copy APKBUILD and the two files + pmb.helpers.run.user(args, ["cp", "-r", path, + path_tmp]) + pmb.helpers.run.user(args, ["cp", "-r", os.path.join(pmaportdir, "main.c"), + os.path.join(tmpdir, "main.c")]) + pmb.helpers.run.user(args, ["cp", "-r", os.path.join(pmaportdir, "Makefile"), + os.path.join(tmpdir, "Makefile")]) + + # make tmpdir, copy apkbuild, run checksum fix + # validate against expected checksums + pmb.build.checksum.fix_local(args, "hello-world", path_tmp) + + pmb.helpers.run.user(args, ["cat", os.path.join(tmpdir, "Makefile")]) + + apkbuild = pmb.parse.apkbuild(path_tmp, check_pkgname=False) + assert apkbuild["sha512sums"] == { + "main.c": "1644d90ce0e74edde408a92c6d1236123f66ad833af9147a3ba402e878987881" + "d34c53f856c4b2fa5fb23b5f6117789720dd1c42fdfb115c2981b74d29788867", + "Makefile": "d85ca0ac72420eadd338f14d9d13de985cd2ace27df6d49d3a3a7c5751f4ea8d" + "4425714a01db741af0cb483fc4471ab92b079563b9d5af0191b7850d35290a52", + "invalid-url": "d00d00d", + } diff --git a/test/testdata/apkbuild/checksum/APKBUILD.inline b/test/testdata/apkbuild/checksum/APKBUILD.inline new file mode 100644 index 00000000..5194f68a --- /dev/null +++ b/test/testdata/apkbuild/checksum/APKBUILD.inline @@ -0,0 +1,13 @@ +# Maintainer: Oliver Smith +pkgname=hello-world +pkgver=1 +pkgrel=6 +pkgdesc="hello world program to be built in the testsuite" +url="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" +arch="all" +license="MIT" +source="main.c Makefile https://postmarketos.org/invalid-url" + +sha512sums="d06f00d main.c +baddeed Makefile +d00d00d invalid-url" \ No newline at end of file diff --git a/test/testdata/apkbuild/checksum/APKBUILD.multiline b/test/testdata/apkbuild/checksum/APKBUILD.multiline new file mode 100644 index 00000000..d0149950 --- /dev/null +++ b/test/testdata/apkbuild/checksum/APKBUILD.multiline @@ -0,0 +1,15 @@ +# Maintainer: Oliver Smith +pkgname=hello-world +pkgver=1 +pkgrel=6 +pkgdesc="hello world program to be built in the testsuite" +url="https://en.wikipedia.org/wiki/%22Hello,_World!%22_program" +arch="all" +license="MIT" +source="main.c Makefile https://postmarketos.org/invalid-url" + +sha512sums=" +d06f00d main.c +baddeed Makefile +d00d00d invalid-url +" diff --git a/test/testdata/apkbuild/checksum/Makefile b/test/testdata/apkbuild/checksum/Makefile new file mode 100644 index 00000000..af38382e --- /dev/null +++ b/test/testdata/apkbuild/checksum/Makefile @@ -0,0 +1,5 @@ +hello-world: hello-world.o + $(CC) -o hello-world hello-world.o + +hello-world.o: main.c + $(CC) -o hello-world.o -c main.c \ No newline at end of file diff --git a/test/testdata/apkbuild/checksum/main.c b/test/testdata/apkbuild/checksum/main.c new file mode 100644 index 00000000..ac4a4dd9 --- /dev/null +++ b/test/testdata/apkbuild/checksum/main.c @@ -0,0 +1,7 @@ +#include + +int main() +{ + printf("hello, world!\n"); + return 0; +} \ No newline at end of file -- 2.45.2