You are not logged in.

#1 2020-12-03 08:03:44

l0b0
Member
Registered: 2014-10-21
Posts: 13
Website

How to mock Pacman package?

I've just finished a script to print out the contents of a file in a Pacman package (based on pkg-extract_original), but I'm having trouble testing the final functionality. Basically I have a Python test:

def should_print_file_contents_from_package():
    # Given a dummy package which claims to own a filesystem package file
    package_name = "filesystem"
    file_path = "/etc/hosts"
    file_contents = "Hello World!"
    current_directory = dirname(__file__)
    package_directory = f"{current_directory}/package"
    run(
        ["makepkg", "--force"],
        check=True,
        cwd=package_directory,
        env={
            "PACKAGE_NAME": package_name,
            "FILE_PATH": file_path,
            "FILE_CONTENTS": file_contents,
        },
    )

    # Given the dummy package directory as cache
    environment = {"PACMAN_CONFIG": f"{DIRECTORY}/pacman.conf"}

    # When requesting a file from an installed package
    result = run(
        ["./pacman-package-file.bash", file_path],
        stdout=PIPE,
        check=True,
        env=environment,
    )

    # Then it should print the contents of the build file
    assert result.stdout == file_contents


 a dummy

, a PKGBUILD file:

#!/usr/bin/env bash

# shellcheck disable=SC2034,SC2154
pkgname="$PACKAGE_NAME"
pkgbase="$PACKAGE_NAME"
version_and_release="$(pacman --query "$PACKAGE_NAME" | cut --delimiter=' ' --fields=2)"
pkgver="$(cut --delimiter='-' --fields=1 <<< "$version_and_release")"
pkgrel="$(cut --delimiter='-' --fields=2 <<< "$version_and_release")"
arch=(any)
package() {
    mkdir --parents "${pkgdir}$(dirname "$FILE_PATH")"
    printf '%s' "$FILE_CONTENTS" > "${pkgdir}${FILE_PATH}"
}

, a dummy pacman.conf:

[options]
CacheDir    = tests/package
…

, and a shell script:

#!/usr/bin/env bash
…
script_name="$(basename "$0")"

# Add extra pacman parameters if specified
pacman_command=('pacman')
if [[ -n "${PACMAN_CONFIG-}" ]]
then
    pacman_command+=("--config=${PACMAN_CONFIG}")
fi

owners_stderr="${working_directory}/owners_stderr"
mapfile -t owners < <("${pacman_command[@]}" --query --owns --quiet "$1" 2> "$owners_stderr")


owner="${owners[0]}"
package_url="$("${pacman_command[@]}" --sync --print "$owner")"
if [[ "${package_url::7}" != 'file://' ]]
then
    printf "%s: Package missing - download using \`pacman --sync --downloadonly %s\` and retry\n" "$script_name" "$owner" >&2
    exit 9
fi
…

The problem is, even with the configuration pointing to the dummy package directory,

pacman --cachedir=tests/package --sync --print filesystem

prints a HTTPS URL rather than the path to the file in tests/package.

What else is needed to get Pacman to print the path to the dummy package file?

Offline

#2 2020-12-03 18:28:25

mpan
Member
Registered: 2012-08-01
Posts: 1,641
Website

Re: How to mock Pacman package?

Put it into a local repository and use a pacman.conf that has that repository configured.

Offline

Board footer

Powered by FluxBB