Skip to content

fix: properly expand tilde (~) in path configuration options#81

Open
yuyuan12138 wants to merge 1 commit intoxeluxee:masterfrom
yuyuan12138:master
Open

fix: properly expand tilde (~) in path configuration options#81
yuyuan12138 wants to merge 1 commit intoxeluxee:masterfrom
yuyuan12138:master

Conversation

@yuyuan12138
Copy link


Title

fix: properly expand tilde (~) in path configuration options

Description

This PR fixes the path resolution bug reported in #78, where configuration options that accept file paths would not properly expand the tilde (~) to the user's home directory.

Problem

Previously, when users configured paths like:
testcases_directory = "~/cp/testcases"

The plugin would incorrectly treat this as a relative path, creating a directory literally named ~/cp/testcases in the current working directory, instead of resolving it to
/home/user/cp/testcases.

Solution

This fix introduces proper path expansion and normalization:

  1. New utility functions in utils.lua:
    - normalize_path() - Expands ~ to home directory and converts to absolute path
    - resolve_config_path() - Smart resolution that distinguishes between absolute paths (starting with ~ or /) and relative paths
  2. Path normalization during config loading:
    - Added normalize_path_config() in config.lua to normalize path options when configuration is loaded
    - Applied to both global setup and local .competitest.lua configurations
  3. Updated all path-related configuration usage:
    - testcases_directory
    - compile_directory
    - running_directory
    - received_problems_path
    - received_contests_directory
    - received_contests_problems_path
    - template_file

Backward Compatibility

✅ Fully backward compatible - all existing configurations continue to work:

  • Relative paths (., ./path, ../path) work as before
  • $(HOME) modifier in file format strings continues to work
  • Absolute paths work correctly

Example Usage

After this fix, all of these configurations work correctly:

require('competitest').setup({
-- Tilde expansion (newly fixed)
testcases_directory = "~/cp/testcases",

-- Absolute paths (already worked)
testcases_directory = "/home/user/cp/testcases",

-- Relative paths (still work)
testcases_directory = "./testcases",
testcases_directory = ".",

-- HOME modifier (still works)
testcases_directory = "$(HOME)/cp/testcases",

})

Changes

  • lua/competitest/utils.lua - Add path normalization functions
  • lua/competitest/config.lua - Normalize paths during config loading
  • lua/competitest/testcases.lua - Use smart path resolution
  • lua/competitest/runner.lua - Use smart path resolution
  • lua/competitest/receive.lua - Use smart path resolution

Total: 5 files changed, 85 insertions(+), 4 deletions(-)

Testing

Thoroughly tested with:

  • ✅ Tilde expansion (~/path)
  • ✅ Absolute paths (/absolute/path)
  • ✅ Relative paths (., ./path, ../path)
  • ✅ $(HOME) modifier compatibility
  • ✅ Configuration hierarchy (global/local configs)
  • ✅ All path-related configuration options

Fixes #78


Fix path resolution for configuration options that accept file paths.
Previously, paths starting with ~ were treated as relative paths and
literal directory names (e.g., ~/cp/testcases created a directory
named "~" in the current working directory).

Changes:
- Add normalize_path() to expand ~ to home directory and normalize paths
- Add resolve_config_path() for smart absolute/relative path resolution
- Normalize path options during configuration loading
- Update all path-related configuration usage:
  - testcases_directory
  - compile_directory
  - running_directory
  - received_problems_path
  - received_contests_directory
  - received_contests_problems_path
  - template_file

Backward compatibility maintained:
- Relative paths (., ./path, ../path) still work as before
- $(HOME) modifier in file format strings continues to work
- Absolute paths work correctly

Fixes xeluxee#78

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
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.

testcases_directory should be an absolute path rather than relative to current file.

1 participant