Skip to content

enhance Executable function to handle relative paths#537

Merged
creativeprojects merged 2 commits intomasterfrom
linux-executable
Jul 28, 2025
Merged

enhance Executable function to handle relative paths#537
creativeprojects merged 2 commits intomasterfrom
linux-executable

Conversation

@creativeprojects
Copy link
Copy Markdown
Owner

Enhance Executable function to handle relative paths and symlinks

This PR refactors the Executable() function in executable_linux.go to properly handle different types of executable paths:

Changes

  • Relative paths starting with .: Resolved using current working directory
  • Commands in PATH: Resolved using exec.LookPath() to find absolute path
  • Improved path handling: Better logic for determining path types and resolution

Testing

  • Added comprehensive test suite in executable_test.go covering:
    • Absolute paths
    • Relative paths (with resticprofile)
    • Symlinks (both absolute and relative)
    • Executables found via PATH environment variable
  • Created test helper binary in test_executable for testing different execution scenarios

Impact

This enhancement ensures the Executable() function returns consistent absolute paths regardless of how the binary was invoked, improving reliability when determining the current executable's location.

Fixes finding executable code added for #490

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 28, 2025

Walkthrough

The Executable() function in the Linux utility package was updated to more accurately resolve executable paths, distinguishing between relative paths and command names found in the system PATH. Comprehensive tests and a helper binary were added to validate the new behaviour across various invocation scenarios. Additionally, coverage and static analysis exclusions were updated to ignore the new test helper directory and other test-related files.

Changes

Cohort / File(s) Change Summary
Executable Path Resolution Logic
util/executable_linux.go
Updated logic in Executable() to distinguish between relative paths (starting with '.') and command names, resolving the latter using exec.LookPath and cleaning the resulting path. Added import of os/exec for PATH resolution.
Executable Path Resolution Tests
util/executable_test.go, util/executable_linux_test.go
Added TestExecutable to build and run a helper binary and its symlink in various ways to verify correct path resolution. Added Linux-specific tests for resolveExecutable covering empty, absolute, relative, PATH-resolved, and non-existent executables. Uses testify.
Test Helper Program
util/test_executable/main.go
Introduced a standalone Go program that prints the resolved executable path using the Executable() function, used by the test suite to verify correct behaviour under different invocation scenarios.
Coverage and Static Analysis Config
codecov.yml, sonar-project.properties
Updated code coverage exclusions to ignore util/test_executable/. Expanded SonarQube exclusions to include mocks, test utilities, and the new test executable directory to omit them from static analysis.

Sequence Diagram(s)

sequenceDiagram
    participant TestExecutable as TestExecutable (test)
    participant HelperBinary as executable_test_helper
    participant UtilPkg as util.Executable()

    TestExecutable->>HelperBinary: Execute via absolute/relative/PATH
    HelperBinary->>UtilPkg: Call Executable()
    UtilPkg->>UtilPkg: Resolve path (relative, PATH, etc.)
    UtilPkg-->>HelperBinary: Return resolved path
    HelperBinary-->>TestExecutable: Output resolved path
    TestExecutable->>TestExecutable: Assert correctness
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Possibly related PRs

  • linux: don't resolve symlink on executable #508: Modifies the Linux-specific Executable() function, but with different logic for resolving relative paths and explicitly avoids symlink resolution—both PRs alter the same core function, making them related.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 28846a4 and b42c6a9.

📒 Files selected for processing (4)
  • codecov.yml (1 hunks)
  • sonar-project.properties (1 hunks)
  • util/executable_linux.go (1 hunks)
  • util/executable_linux_test.go (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • sonar-project.properties
  • codecov.yml
🚧 Files skipped from review as they are similar to previous changes (1)
  • util/executable_linux.go
⏰ 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). (4)
  • GitHub Check: Build and test (1.24, macos-latest)
  • GitHub Check: Build and test (1.24, ubuntu-latest)
  • GitHub Check: Build and test (1.24, windows-latest)
  • GitHub Check: Analyze (go)
🔇 Additional comments (7)
util/executable_linux_test.go (7)

1-3: LGTM!

The build constraint and package declaration are correctly implemented for Linux-specific testing.


5-12: LGTM!

All imports are necessary and appropriate for the test functionality.


15-20: LGTM!

The empty path test case properly validates error handling with appropriate assertions for both return value and error message.


22-26: LGTM!

The absolute path test case correctly validates that absolute paths are returned unchanged. The choice of /usr/bin/ls is reasonable as it's commonly available on Linux systems.


28-36: LGTM!

The relative path test case is well-implemented with proper working directory handling and path construction using filepath.Join. The use of require.NoError for the critical dependency is appropriate.


38-46: LGTM!

The "command in PATH" test case is well-designed with appropriate validation of the absolute path result. The choice of ls is sensible, and the logging provides helpful debugging information.


48-52: LGTM!

The "command not in PATH" test case properly validates error handling for non-existent commands with appropriate assertions.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch linux-executable

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 28, 2025

Codecov Report

❌ Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.33%. Comparing base (859da21) to head (b42c6a9).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
util/executable_linux.go 81.25% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #537      +/-   ##
==========================================
+ Coverage   79.27%   79.33%   +0.06%     
==========================================
  Files         136      136              
  Lines       13295    13305      +10     
==========================================
+ Hits        10539    10555      +16     
+ Misses       2337     2332       -5     
+ Partials      419      418       -1     
Flag Coverage Δ
unittests 79.33% <81.25%> (+0.06%) ⬆️

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.

@sonarqubecloud
Copy link
Copy Markdown

@creativeprojects creativeprojects merged commit adf90cb into master Jul 28, 2025
11 checks passed
@creativeprojects creativeprojects deleted the linux-executable branch July 28, 2025 17:29
@creativeprojects creativeprojects added this to the v0.32.0 milestone Jul 28, 2025
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.

1 participant