pacman: add root, cachedir, and config options (#11681)

* pacman: add root, cachedir, and config options

Add three dedicated options -- O(root), O(cachedir), and O(config) --
so that all pacman commands get the corresponding global flags
(--root, --cachedir, --config) prepended, enabling use cases such as
installing packages into a chroot or alternative root directory
(similar to pacstrap).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* add changelog frag

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Alexei Znamensky
2026-03-26 11:53:11 +13:00
committed by GitHub
parent d06c83eb68
commit e2c06f2d12
3 changed files with 192 additions and 17 deletions

View File

@@ -404,6 +404,92 @@ class TestPacman:
],
True,
),
(
# root + cachedir + config: all commands get prefixed with the global options
{"root": "/mnt", "cachedir": "/mnt/var/cache/pacman/pkg", "config": "/alt/pacman.conf"},
[
(
[
"pacman",
"--config",
"/alt/pacman.conf",
"--root",
"/mnt",
"--cachedir",
"/mnt/var/cache/pacman/pkg",
"--sync",
"--list",
],
{"check_rc": True},
0,
"a\nb\nc",
"",
),
(
[
"pacman",
"--config",
"/alt/pacman.conf",
"--root",
"/mnt",
"--cachedir",
"/mnt/var/cache/pacman/pkg",
"--sync",
"--refresh",
],
{"check_rc": False},
0,
"stdout",
"stderr",
),
(
[
"pacman",
"--config",
"/alt/pacman.conf",
"--root",
"/mnt",
"--cachedir",
"/mnt/var/cache/pacman/pkg",
"--sync",
"--list",
],
{"check_rc": True},
0,
"a\nb\nc",
"",
),
],
False,
),
(
# config only (no root/cachedir)
{"config": "/alt/pacman.conf"},
[
(
["pacman", "--config", "/alt/pacman.conf", "--sync", "--list"],
{"check_rc": True},
0,
"a\nb\nc",
"",
),
(
["pacman", "--config", "/alt/pacman.conf", "--sync", "--refresh"],
{"check_rc": False},
0,
"stdout",
"stderr",
),
(
["pacman", "--config", "/alt/pacman.conf", "--sync", "--list"],
{"check_rc": True},
0,
"a\nb\nc",
"",
),
],
False,
),
(
# Test whether pacman --sync --list is not called more than twice
{"upgrade": True},
@@ -1006,6 +1092,52 @@ class TestPacman:
},
AnsibleExitJson,
),
(
# install pkg with root: global options come before the operation flags
{"name": ["sudo"], "state": "present", "root": "/mnt", "cachedir": "/mnt/var/cache/pacman/pkg"},
["sudo"],
[Package("sudo", "sudo")],
{
"calls": [
mock.call(
mock.ANY,
[
"pacman",
"--root",
"/mnt",
"--cachedir",
"/mnt/var/cache/pacman/pkg",
"--noconfirm",
"--noprogressbar",
"--needed",
"--sync",
"--print-format",
"%n %v",
"sudo",
],
check_rc=False,
),
mock.call(
mock.ANY,
[
"pacman",
"--root",
"/mnt",
"--cachedir",
"/mnt/var/cache/pacman/pkg",
"--noconfirm",
"--noprogressbar",
"--needed",
"--sync",
"sudo",
],
check_rc=False,
),
],
"side_effect": [(0, "sudo version", ""), (0, "", "")],
},
AnsibleExitJson,
),
(
# latest pkg: Check mode
{"_ansible_check_mode": True, "name": ["sqlite"], "state": "latest"},