• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 31083928084

06 Aug 2026 08:12AM UTC coverage: 72.616% (+0.006%) from 72.61%
31083928084

Pull #2258

github

web-flow
Merge 276049d85 into cd9770b9c
Pull Request #2258: #2250 uninstall commands from package manager

5038 of 7697 branches covered (65.45%)

Branch coverage included in aggregate %.

13137 of 17332 relevant lines covered (75.8%)

3.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.55
cli/src/main/java/com/devonfw/tools/ide/tool/NativePackageManager.java
1
package com.devonfw.tools.ide.tool;
2

3
import java.util.ArrayList;
4
import java.util.List;
5

6
/**
7
 * Represents an OS native package manager used for managing software packages.
8
 */
9
public enum NativePackageManager {
3✔
10
  /** Advanced Package Tool (APT) is the package manager of Debian based Linux distributions. */
11
  APT("install -y", "-y autoremove --purge", "=", "*"),
10✔
12

13
  /** Zypper is the package manager of SUSE based Linux distributions. */
14
  ZYPPER("--non-interactive install", "remove", "=", ""),
10✔
15

16
  /** Yellowdog Updater Modified (YUM) is the package manager of RPM package based Linux distributions like Fedora, Red Hat, or CentOS. */
17
  YUM("install -y", "remove -y", "-", "*"),
10✔
18

19
  /** DaNdiFied yum (DNF) is the package manager of RPM package based Linux distributions like Fedora. It is the successor of {@link #YUM}. */
20
  DNF("install -y", "remove -y", "-", "*");
10✔
21

22
  private static final String DPKG_STATUS_INSTALLED = "installed";
23

24
  private final String installCommand;
25
  private final String uninstallCommand;
26
  private final String versionSeparator;
27
  private final String versionWildCard;
28

29
  NativePackageManager(String installCommand, String uninstallCommand, String versionSeparator, String versionWildCard) {
4✔
30
    this.installCommand = installCommand;
3✔
31
    this.uninstallCommand = uninstallCommand;
3✔
32
    this.versionSeparator = versionSeparator;
3✔
33
    this.versionWildCard = versionWildCard;
3✔
34
  }
1✔
35

36
  /**
37
   * Extracts the package manager from the provided command string.
38
   *
39
   * @param command The command string to extract the package manager from.
40
   * @return The corresponding {@code PackageManager} based on the provided command string.
41
   * @throws IllegalArgumentException If the command string does not contain a recognized package manager.
42
   */
43
  public static NativePackageManager extractPackageManager(String command) {
44

45
    if (command.contains("apt")) {
×
46
      return APT;
×
47
    }
48
    if (command.contains("yum")) {
×
49
      return YUM;
×
50
    }
51
    if (command.contains("zypper")) {
×
52
      return ZYPPER;
×
53
    }
54
    if (command.contains("dnf")) {
×
55
      return DNF;
×
56
    }
57

58
    throw new IllegalArgumentException("Unknown package manager in command: " + command);
×
59
  }
60

61
  public String getBinaryName() {
62

63
    return name().toLowerCase();
4✔
64
  }
65

66
  /**
67
   * Builds the package specification pinning the given package to the given version.
68
   *
69
   * @param pkg the name of the package.
70
   * @param version the version to pin the package to or {@code null} to use the latest available version.
71
   * @return the package specification for this package manager.
72
   */
73

74
  public String getPackageSpec(String pkg, String version) {
75
    if ((version == null) || version.isBlank()) {
5!
76
      return pkg;
2✔
77
    }
78
    String spec = pkg + this.versionSeparator + version + this.versionWildCard;
8✔
79
    if (this.versionWildCard.isEmpty()) {
4✔
80
      return spec;
2✔
81
    }
82
    return "'" + spec + "'";
3✔
83
  }
84

85
  /**
86
   * @param pkg the name of the package to query.
87
   * @return the command to determine the installed version of the given package as executable followed by its arguments.
88
   */
89
  public List<String> getVersionQueryCommand(String pkg) {
90
    List<String> command = new ArrayList<>(switch (this) {
7✔
91
      case APT -> List.of("dpkg-query", "-W", "-f=${db:Status-Status}|${Version}");
5✔
92
      case ZYPPER, YUM, DNF -> List.of("rpm", "-q", "--queryformat", "%{VERSION}");
5✔
93
    });
94
    command.add(pkg);
4✔
95
    return command;
2✔
96
  }
97

98
  /**
99
   * @param output the output of the {@link #getVersionQueryCommand(String) version query command}.
100
   * @return the version of the installed package or {@code null} if the package is not installed.
101
   */
102
  public String parseVersionQueryOutput(String output) {
103
    if ((output == null) || output.isBlank()) {
5!
104
      return null;
×
105
    }
106
    String version = output.trim();
3✔
107
    if (this == APT) {
3✔
108
      String[] parts = version.split("\\|", 2);
5✔
109
      if ((parts.length != 2) || !DPKG_STATUS_INSTALLED.equals(parts[0].trim())) {
11!
110
        return null;
×
111
      }
112
      version = parts[1].trim();
5✔
113
    }
114
    return version.isEmpty() ? null : version;
5!
115
  }
116

117
  /**
118
   * @param version the version of the package to install.
119
   * @param nativePackage the {@link NativePackage} to install.
120
   * @return the {@link PackageManagerCommand} to install the given {@link NativePackage} including its {@link NativePackage#getSetupCommands()} setup commands.
121
   */
122
  public PackageManagerCommand install(NativePackage nativePackage, String version) {
123
    verifyPackageManager(nativePackage);
3✔
124
    List<String> commands = new ArrayList<>(nativePackage.getSetupCommands());
6✔
125
    StringBuilder command = new StringBuilder("sudo ").append(getBinaryName());
8✔
126
    for (String option : nativePackage.getExtraInstallOptions()) {
11✔
127
      command.append(' ').append(option);
6✔
128
    }
1✔
129
    command.append(' ').append(this.installCommand);
7✔
130
    for (String pkg : nativePackage.getPackages()) {
11✔
131
      command.append(' ').append(getPackageSpec(pkg, version));
9✔
132
    }
1✔
133
    commands.add(command.toString());
5✔
134
    return new PackageManagerCommand(this, commands);
6✔
135
  }
136

137
  /**
138
   * @param nativePackage the {@link NativePackage} to uninstall
139
   * @return the {@link PackageManagerCommand} to uninstall the given {@link NativePackage} including its
140
   *     {@link NativePackage#getCleanupCommands() clean up commands}.
141
   */
142
  public PackageManagerCommand uninstall(NativePackage nativePackage) {
143
    verifyPackageManager(nativePackage);
3✔
144
    StringBuilder command = new StringBuilder("sudo ").append(getBinaryName()).append(' ').append(this.uninstallCommand);
13✔
145
    for (String pkg : nativePackage.getPackages()) {
11✔
146
      command.append(' ').append(pkg);
6✔
147
    }
1✔
148
    List<String> commands = new ArrayList<>();
4✔
149
    commands.add(command.toString());
5✔
150
    commands.addAll(nativePackage.getCleanupCommands());
5✔
151
    return new PackageManagerCommand(this, commands);
6✔
152
  }
153

154
  private void verifyPackageManager(NativePackage nativePackage) {
155
    if (nativePackage.getPackageManager() != this) {
4!
156
      throw new IllegalArgumentException(
×
157
          "Package" + nativePackage.getPackages() + " is declared for package manager " + nativePackage.getPackageManager() + " and cannot be handled by "
×
158
              + this);
159
    }
160
  }
1✔
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc