Skip to content

Extract /proc/[pid]/stat parsing and add LinuxFileSystem tests - #3205

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:3189-extract-proc-filesystem-parsing
Apr 28, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:3189-extract-proc-filesystem-parsing

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 28, 2026 •

Copy link
Copy Markdown
Member

Resolves the remaining items in #3189.

Refactored methods:

  • LinuxOperatingSystem.getParentPidFromProcFile() → new getParentPidFromStat(String) — parses /proc/[pid]/stat content
    • Also fixed to use ProcPath.PID_STAT instead of hardcoded /proc path

Tests added to LinuxOperatingSystemTest:

  • Standard stat line (PPID extraction)
  • Init process (PPID = 0)
  • Empty stat line
  • Process name with spaces/parentheses

New LinuxFileSystemTest (Linux-only):

  • Verifies mount parsing returns root filesystem with expected properties
  • Verifies local-only filtering is a subset of all mounts

No public API changes. No new dependencies.

Summary by CodeRabbit

  • Refactor

    • Improved process identification logic structure for enhanced code reusability and maintainability.
  • Tests

    • Added comprehensive test coverage for file system operations and process management, including edge cases and boundary conditions.

- LinuxOperatingSystem.getParentPidFromStat(String): extracted from
  getParentPidFromProcFile(), also fixed to use ProcPath.PID_STAT
  instead of hardcoded /proc path
- LinuxFileSystemTest: Linux-only integration test verifying mount
  parsing returns root filesystem with expected properties

Tests cover stat line parsing (standard, init, empty, comm with spaces)
and filesystem mount parsing (root present, local-only filtering).
No public API changes.
@coderabbitai

coderabbitai Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Refactors parent-PID extraction in LinuxOperatingSystem by introducing a reusable getParentPidFromStat(String stat) static helper method, then updates getParentPidFromProcFile to use this helper. Adds comprehensive test coverage for the new helper method and Linux file system functionality.

Changes

Cohort / File(s) Summary
LinuxOperatingSystem refactoring
oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOperatingSystem.java
Introduces new static helper getParentPidFromStat(String stat) to extract PPID from /proc/[pid]/stat format. Updates getParentPidFromProcFile to delegate parsing to this helper, removing inline parsing and race-condition comments while preserving fallback behavior.
LinuxOperatingSystem test suite
oshi-common/src/test/java/oshi/software/common/os/linux/LinuxOperatingSystemTest.java
Adds test methods for getParentPidFromStat covering typical processes, PID 1 edge case, empty input, and comm fields with spaces/parentheses.
LinuxFileSystem test suite
oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java
New test file introducing stubbed LinuxFileSystem implementation to verify getFileStoreMatching behavior with null mount point, empty parameters, and localOnly flag variations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

Extracting wisdom from the /proc maze,
A helper method now lights the way,
Tests confirm what parsing does convey—
Parent PIDs dance in cleaner phase,
Code refined through modern test-driven daze! 🧪✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: extracting /proc/[pid]/stat parsing logic and adding LinuxFileSystem tests, which aligns with the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.11%. Comparing base (fa85496) to head (6338e65).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3205      +/-   ##
==========================================
+ Coverage   59.02%   59.11%   +0.08%     
==========================================
  Files         421      421              
  Lines       20358    20358              
  Branches     3576     3575       -1     
==========================================
+ Hits        12017    12035      +18     
+ Misses       6583     6571      -12     
+ Partials     1758     1752       -6     
Flag Coverage Δ
linux 25.12% <100.00%> (+0.06%) ⬆️
macos 28.46% <0.00%> (ø)
windows 37.62% <0.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis dbwiddis linked an issue Apr 28, 2026 that may be closed by this pull request
23 tasks done

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java`:
- Around line 55-62: Replace the weak size-only check in
testGetFileStoresLocalOnly with assertions that verify the filter actually
selects a subset of mounts: call StubLinuxFileSystem.getFileStoreMatching(null,
Collections.emptyMap(), false) and getFileStoreMatching(..., true) as before,
then assert that every OSFileStore in local is contained in all (e.g., assert
that all containsAll local) to prove locality is a subset, and also assert that
the two lists are not equal when the StubLinuxFileSystem returns at least one
non-local mount (i.e., assertThat(all, is(not(local)))) so the test fails if
localOnly is ignored. Ensure you reference StubLinuxFileSystem and
getFileStoreMatching in the assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e5d71985-5baf-43ad-ad5c-8c4a293c0e16

📥 Commits

Reviewing files that changed from the base of the PR and between fa85496 and 6338e65.

📒 Files selected for processing (3)
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOperatingSystem.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxOperatingSystemTest.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Analyze (java)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2026-04-06T04:45:51.516Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:51.516Z
Learning: In the oshi/oshi project, the public API is strictly limited to interfaces and classes in the `oshi`, `oshi.hardware`, and `oshi.software.os` packages. All platform-specific code under subpackages like `oshi.software.os.linux`, `oshi.hardware.platform.linux`, etc., is considered implementation detail — not public API. Do not flag changes to platform-specific classes (e.g., making `LinuxOperatingSystem` abstract, narrowing constructor visibility, adding abstract methods) as public API breaks.

Applied to files:

  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java
📚 Learning: 2026-04-05T05:40:45.797Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:45.797Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.

Applied to files:

  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java
📚 Learning: 2026-03-29T20:01:06.452Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java11/src/main/java/module-info.java:10-17
Timestamp: 2026-03-29T20:01:06.452Z
Learning: In the oshi project, `oshi-core-java25/src/main/java/module-info.java` intentionally does NOT export `oshi.util.platform.unix.freebsd`, `oshi.util.platform.unix.openbsd`, or `oshi.util.platform.unix.solaris`, because the FFM (Foreign Function & Memory) implementation only supports Windows, macOS, and Linux. This differs from `oshi-core-java11/src/main/java/module-info.java` which exports all Unix platform packages. The asymmetry is by design.

Applied to files:

  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.

Applied to files:

  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOperatingSystem.java
🔇 Additional comments (3)
oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java (1)

34-53: Good coverage for the root mount path.

This test exercises the real Linux mount parsing path and verifies the root store's mount, type, and space metadata without relying on a hardcoded fixture.

oshi-common/src/test/java/oshi/software/common/os/linux/LinuxOperatingSystemTest.java (1)

90-117: Good coverage for the new stat parser.

These cases hit the important edge conditions here: a normal PPID, PID 1 returning 0, empty input, and comm values with spaces/parentheses.

oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOperatingSystem.java (1)

193-198: No null check needed here. FileUtil.getStringFromFile() returns an empty string (not null) on failure, and your empty-string check on line 194 already handles that case. The implementation is safe as-is.

			> Likely an incorrect or invalid review comment.

Comment on lines +55 to +62
@Test
void testGetFileStoresLocalOnly() {
StubLinuxFileSystem fs = new StubLinuxFileSystem();
List<OSFileStore> all = fs.getFileStoreMatching(null, Collections.emptyMap(), false);
List<OSFileStore> local = fs.getFileStoreMatching(null, Collections.emptyMap(), true);
// Local should be a subset of all
assertThat(local.size(), is(not(greaterThan(all.size()))));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Strengthen the localOnly assertion.

This only compares list sizes, so it still passes if localOnly is ignored or if the environment happens to expose only local mounts. Please assert on the actual returned mounts to prove the filter is doing work.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@oshi-common/src/test/java/oshi/software/common/os/linux/LinuxFileSystemTest.java`
around lines 55 - 62, Replace the weak size-only check in
testGetFileStoresLocalOnly with assertions that verify the filter actually
selects a subset of mounts: call StubLinuxFileSystem.getFileStoreMatching(null,
Collections.emptyMap(), false) and getFileStoreMatching(..., true) as before,
then assert that every OSFileStore in local is contained in all (e.g., assert
that all containsAll local) to prove locality is a subset, and also assert that
the two lists are not equal when the StubLinuxFileSystem returns at least one
non-local mount (i.e., assertThat(all, is(not(local)))) so the test fails if
localOnly is ignored. Ensure you reference StubLinuxFileSystem and
getFileStoreMatching in the assertions.

@dbwiddis
dbwiddis merged commit 4312c46 into oshi:master Apr 28, 2026
16 checks passed
@dbwiddis
dbwiddis deleted the 3189-extract-proc-filesystem-parsing branch April 28, 2026 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract parsing logic from command execution for testability

1 participant