Skip to content

Extract parsing logic from command execution for testability #3189

Closed

Description

@dbwiddis

Summary

Several classes in oshi-common combine command execution with output parsing in a single method, making the parsing logic untestable without running the actual command. This limits test coverage to ~58% — the parsing branches (error handling, edge cases, platform-specific formats) can only be exercised on real hardware with the right conditions.

The project already has an established pattern for separating these concerns. For example, LinuxCentralProcessor provides:

  • readTopologyFromCpuinfo() — runs the command
  • readTopologyFromCpuinfo(List<String> lines) — parses the output (testable with fixture data)

This pattern should be applied more broadly. The refactoring is mechanical: extract the parsing into a package-private static method that accepts List<String> (or String), and have the public method call the command then delegate.

Benefits

  • Testable with fixture data: representative command output from Linux, macOS, and Windows can be committed as test resources or inline strings, enabling deterministic cross-platform tests
  • No new dependencies: no mocking framework needed
  • No public API changes: the public methods remain unchanged
  • Incremental: each method is a small, independent PR

Fixture data strategy

Real command output can be captured from actual hardware and used as test fixtures:

  • Linux: lspci -vnnm, dmidecode --type 17, dmidecode -t system, dmidecode -t bios, who, lscpu -p=cpu,node, lscpu, lshw -C display, lshw -C system, lshw -class processor, vcgencmd version, netstat variants
  • macOS: system_profiler SPDisplaysDataType, sw_vers, who (uses different date format than Linux)
  • Windows: mostly uses WMI/COM rather than command-line parsing, so fewer candidates here

Fixtures can be stored as List.of(...) inline in tests or as resource files under src/test/resources/.

Methods to refactor

Linux — oshi-common drivers (highest priority)

  • AbstractGlobalMemory.getPhysicalMemory() — parses dmidecode --type 17 output (27 missed lines)
  • LinuxGraphicsCard.getGraphicsCardsFromLspci() — parses lspci -vnnm output (~40 missed lines)
  • LinuxGraphicsCard.queryLspciMemorySize() — parses lspci -v -s output
  • LinuxOperatingSystem.queryFamilyVersionCodenameFromReleaseFiles() — parses release files
  • LinuxOperatingSystem.getParentPidsFromProcFiles() — parses /proc/[pid]/stat
  • LinuxCentralProcessor.mapNumaNodesFromLscpu() — parses lscpu -p=cpu,node
  • LinuxCentralProcessor.mapCachesFromLscpu() — parses lscpu cache lines
  • LinuxFileSystem mount/filesystem parsing — parses /proc/self/mounts and df output
  • LinuxOSProcess — taskset -p affinity mask parsing
  • Dmidecode.querySerialNumber() — parses dmidecode -t system
  • Dmidecode.queryUUID() — parses dmidecode -t system
  • Dmidecode.queryBiosNameRev() — parses dmidecode -t bios
  • Lshw static initializer and queryCpuCapacity() — parses lshw output
  • Who.matchLinux() (linux) — already has the parsing separated but it is private; make package-private
  • LinuxFirmware.queryVcGenCmd() — parses vcgencmd version (Raspberry Pi)

macOS — oshi-common drivers

  • MacGraphicsCard.parseGraphicsCards() — parses system_profiler SPDisplaysDataType
  • MacOperatingSystem version detection — parses sw_vers
  • MacCentralProcessor — parses sysctl output for topology

Unix shared drivers

  • Who.matchUnix() — already package-private, just needs test fixtures for various who date formats
  • NetStat.queryTcpnetstat() — parses netstat -n -p tcp
  • NetStat.queryNetstat() — parses netstat -n
  • NetStat.queryTcpStats() / queryUdpStats() — parses netstat -s variants
  • Xrandr / Xwininfo — parses X11 display info

Already done (reference examples)

  • ✅ LinuxCentralProcessor.readTopologyFromCpuinfo(List<String>)
  • ✅ LinuxCentralProcessor.readTopologyFromSysfs(String path)
  • ✅ LinuxCentralProcessor.getSystemLoadAverage(int, String)
  • ✅ LinuxCentralProcessor.queryMaxFreqFromCpuFreqPath(String)
  • ✅ LinuxInstalledApps.parseLinuxAppInfo(List<String>)

Suggested approach

Each PR should:

  1. Add a package-private overload that accepts the parsed input (List<String> or String)
  2. Have the existing public method delegate to it
  3. Add tests with fixture data covering the key branches
  4. No public API changes, no new dependencies

PRs can be organized by file or by platform — whatever is easiest to review.

Activity

  1. coderabbitai commented on Apr 23, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #3188 - Improve Linux test coverage in oshi-common [merged]
    #3199 - Extract macOS parsing logic for testability [closed]
    #3201 - Extract parsing logic from dmidecode/lscpu/vcgencmd commands [closed]
    #3202 - Extract parsing logic from lspci/lshw commands [closed]
    #3203 - Extract parsing from Who, LinuxOSProcess, LinuxOperatingSystem [closed]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

    • Create Plan

    🧪 Issue enrichment is currently in open beta.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  2. added
    up for grabs
    good first issue
    on Apr 23, 2026
  3. removed
    up for grabs
    good first issue
    on Apr 28, 2026
  4. self-assigned this
    on Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions