Files
reverse-engineering-anything/src/domain/desktop/debianPackageFile.test.ts
T
Anurag SinghandAnurag Singh 82ce15e20a fix(desktop): install the packages that provided a package file's virtual dependencies (#15779)
A package file's Depends can name a virtual package, such as Chrome's
libasound2 on Ubuntu 24.04, which apt refuses to install by name. The
capture now records, for each dependency the host has no package of, the
installed packages that provide it, read from dpkg-query. The export
installs those providers instead and says so in its limitations. When
dpkg-query cannot say, the capture records why and the export keeps the
declared name.

Fixes #15682

Co-authored-by: Anurag Singh <211784907+Anurag-5ingh@users.noreply.github.com>
2026-12-02 13:52:55 +08:00

157 lines
5.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { DPKG_DEB_OUTPUT } from "./debianPackageFile.fixture.js";
import {
dependencyPackages,
packageFileMismatch,
parseDependencyProviders,
parseDpkgDebFields,
} from "./debianPackageFile.js";
describe("parseDpkgDebFields", () => {
it("reads the fields dpkg-deb prints and splits relationship fields into their entries", () => {
expect(parseDpkgDebFields(DPKG_DEB_OUTPUT)).toEqual({
package: "varicad2026-en",
version: "1:2.03-1",
architecture: "amd64",
depends: [
"libc6 (>= 2.34)",
"libgl1 | libgl1-mesa-glx",
"libstdc++6:any (>= 12)",
"libx11-6",
"libfontconfig1 (>= 2.12.6)",
],
pre_depends: ["dpkg (>= 1.15.6)"],
});
});
it("leaves out a field the file lacks and joins a folded one", () => {
expect(
parseDpkgDebFields(
"Package: tool\nVersion: 1.0\nArchitecture: all\nDepends: libc6,\n python3\n (>= 3.12)\n",
),
).toMatchObject({
architecture: "all",
depends: ["libc6", "python3 (>= 3.12)"],
pre_depends: [],
});
});
it("gives nothing without Package, Version and Architecture", () => {
expect(parseDpkgDebFields("Package: tool\nVersion: 1.0\n")).toBeUndefined();
expect(parseDpkgDebFields("")).toBeUndefined();
});
});
describe("packageFileMismatch", () => {
const fields = parseDpkgDebFields(DPKG_DEB_OUTPUT);
if (fields === undefined) throw new Error("fixture did not parse");
const installed = {
name: "varicad2026-en:amd64",
version: "1:2.03-1",
architecture: "amd64",
};
it("accepts the installed name, with or without its architecture qualifier, version and architecture", () => {
expect(packageFileMismatch(fields, installed)).toBeUndefined();
expect(
packageFileMismatch(fields, { ...installed, name: "varicad2026-en" }),
).toBeUndefined();
});
it("names what the file holds and what dpkg installed when they differ", () => {
expect(
packageFileMismatch(fields, { ...installed, version: "1:2.02-1" }),
).toBe(
"holds varicad2026-en 1:2.03-1 (amd64), not the installed varicad2026-en 1:2.02-1 (amd64)",
);
expect(
packageFileMismatch(fields, { ...installed, architecture: "arm64" }),
).toContain("not the installed varicad2026-en 1:2.03-1 (arm64)");
expect(
packageFileMismatch(fields, { ...installed, name: "varicad" }),
).toContain("not the installed varicad ");
});
});
describe("dependencyPackages", () => {
it("takes each entry's first alternative without its qualifier or version, once", () => {
expect(
dependencyPackages({
depends: [
"libc6 (>= 2.34)",
"libgl1 | libgl1-mesa-glx",
"libstdc++6:any (>= 12)",
"libc6",
],
pre_depends: ["dpkg (>= 1.15.6)", "python3:native"],
}),
).toEqual({
names: ["libc6", "libgl1", "libstdc++6", "dpkg", "python3"],
unread: [],
});
});
it("returns an entry whose first alternative names no package apart", () => {
expect(
dependencyPackages({
depends: ["${shlibs:Depends}", "x"],
pre_depends: [],
}),
).toEqual({ names: [], unread: ["${shlibs:Depends}", "x"] });
});
});
describe("dependencyPackages with recorded providers", () => {
it("installs the packages that provided a dependency the host had no package of, once", () => {
expect(
dependencyPackages({
depends: ["libasound2 (>= 1.0.17)", "libc6", "libasound2t64"],
pre_depends: [],
dependency_providers: { libasound2: ["libasound2t64"] },
}),
).toEqual({ names: ["libasound2t64", "libc6"], unread: [] });
});
});
describe("parseDependencyProviders", () => {
// `dpkg-query -W -f='${Package}\t${db:Status-Status}\t${Provides}\n'` on
// Ubuntu 24.04 with libasound2t64 installed in place of libasound2.
const NOBLE = [
"libasound2-data\tinstalled\t",
"libasound2t64\tinstalled\tlibasound2 (= 1.2.11-1build2)",
"libc6\tinstalled\t",
"liboss4-salsa-asound2\tconfig-files\tlibasound2 (= 4.2)",
"mawk\tinstalled\tawk",
"gawk\tinstalled\tawk:any (= 1:5.2.1-2build3)",
"",
].join("\n");
const file = {
depends: ["libasound2 (>= 1.0.17)", "libc6 (>= 2.17)", "awk | mawk"],
pre_depends: ["libgone1"],
};
it("names the installed providers of each dependency the host has no package of", () => {
expect(parseDependencyProviders(NOBLE, file)).toEqual({
libasound2: ["libasound2t64"],
awk: ["mawk", "gawk"],
});
});
it("keeps a dependency installed by its own name even when another package provides it", () => {
expect(
parseDependencyProviders(`${NOBLE}libasound2\tinstalled\t\n`, file),
).toEqual({ awk: ["mawk", "gawk"] });
});
it("gives nothing for a row the format does not print", () => {
expect(parseDependencyProviders("libc6 installed\n", file)).toBeUndefined();
expect(
parseDependencyProviders("libc6\tinstalled\t\textra\n", file),
).toBeUndefined();
expect(
parseDependencyProviders("libc6\tinstalled\t${shlibs}\n", file),
).toBeUndefined();
});
});