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

devonfw / IDEasy / 30885547918

04 Aug 2026 06:52AM UTC coverage: 72.715% (+0.1%) from 72.61%
30885547918

Pull #2258

github

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

5033 of 7671 branches covered (65.61%)

Branch coverage included in aggregate %.

13126 of 17302 relevant lines covered (75.86%)

3.22 hits per line

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

71.62
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", "=", "*"),
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 final String installCommand;
23
  private final String uninstallCommand;
24
  private final String versionSeparator;
25
  private final String versionWildCard;
26

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

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

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

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

59
  public String getBinaryName() {
60

61
    return name().toLowerCase();
4✔
62
  }
63

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

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

83
  /**
84
   * @param nativePackage the {@link NativePackage} to install.
85
   * @return the {@link PackageManagerCommand} to install the given {@link NativePackage} including its {@link NativePackage#getSetupCommands()} setup commands.
86
   */
87
  public PackageManagerCommand install(NativePackage nativePackage, String version) {
88
    verifyPackageManager(nativePackage);
3✔
89
    List<String> commands = new ArrayList<>(nativePackage.getSetupCommands());
6✔
90
    StringBuilder command = new StringBuilder("sudo ").append(getBinaryName());
8✔
91
    for (String option : nativePackage.getExtraInstallOptions()) {
11✔
92
      command.append(' ').append(option);
6✔
93
    }
1✔
94
    command.append(' ').append(this.installCommand);
7✔
95
    for (String pkg : nativePackage.getPackages()) {
11✔
96
      command.append(' ').append(getPackageSpec(pkg, version));
9✔
97
    }
1✔
98
    commands.add(command.toString());
5✔
99
    return new PackageManagerCommand(this, commands);
6✔
100
  }
101

102
  /**
103
   * @param nativePackage the {@link NativePackage} to uninstall
104
   * @return the {@link PackageManagerCommand} to uninstall the given {@link NativePackage} including its
105
   *     {@link NativePackage#getCleanupCommands() clean up commands}.
106
   */
107
  public PackageManagerCommand uninstall(NativePackage nativePackage) {
108
    verifyPackageManager(nativePackage);
3✔
109
    StringBuilder command = new StringBuilder("sudo ").append(getBinaryName()).append(' ').append(this.uninstallCommand);
13✔
110
    for (String pkg : nativePackage.getPackages()) {
11✔
111
      command.append(' ').append(pkg);
6✔
112
    }
1✔
113
    List<String> commands = new ArrayList<>();
4✔
114
    commands.add(command.toString());
5✔
115
    commands.addAll(nativePackage.getCleanupCommands());
5✔
116
    return new PackageManagerCommand(this, commands);
6✔
117
  }
118

119
  private void verifyPackageManager(NativePackage nativePackage) {
120
    if (nativePackage.getPackageManager() != this) {
4!
121
      throw new IllegalArgumentException(
×
122
          "Package" + nativePackage.getPackages() + " is declared for package manager " + nativePackage.getPackageManager() + " and cannot be handled by "
×
123
              + this);
124
    }
125
  }
1✔
126
}
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