Skip to content

Commit d3693c1

Browse files
committed
fix: handle NVIM environment variable in test script
When Claude Code runs inside Neovim, the NVIM environment variable points to a socket path instead of the nvim executable. This caused the test script to fail with permission errors. The fix checks if NVIM is an executable file (not a socket) before using it, otherwise falls back to finding nvim in PATH. This allows tests to run properly both from within Claude Code/Neovim and from regular terminals. Added: - Test script to verify NVIM detection logic works correctly - Documentation in DEVELOPMENT.md explaining the behavior
1 parent ffdf35d commit d3693c1

3 files changed

Lines changed: 82 additions & 6 deletions

File tree

DEVELOPMENT.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ make test-basic
183183
make test-config
184184
```
185185

186+
### Running Tests from Within Neovim/Claude Code
187+
188+
When running tests from within a Neovim instance (such as when using Claude Code via claude-code.nvim), the test script automatically handles the `$NVIM` environment variable which normally points to a socket file instead of the nvim executable.
189+
190+
The test script will:
191+
- Use the `$NVIM` variable if it points to a valid executable file
192+
- Fall back to finding `nvim` in `$PATH` if `$NVIM` points to a socket or invalid path
193+
- Display which nvim binary is being used for transparency
194+
195+
To verify the NVIM detection logic works correctly, you can run:
196+
```bash
197+
./scripts/test_nvim_detection.sh
198+
```
199+
186200
### Writing Tests
187201

188202
Tests are written in Lua using a simple BDD-style API:

scripts/test.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ cd "$PLUGIN_DIR"
1212
# Print current directory for debugging
1313
echo "Running tests from: $(pwd)"
1414

15-
# Find nvim
16-
NVIM=${NVIM:-$(which nvim)}
17-
18-
if [ -z "$NVIM" ]; then
19-
echo "Error: nvim not found in PATH"
20-
exit 1
15+
# Find nvim - ignore NVIM env var if it points to a socket
16+
if [ -n "$NVIM" ] && [ -x "$NVIM" ] && [ ! -S "$NVIM" ]; then
17+
# NVIM is set and is an executable file (not a socket)
18+
echo "Using NVIM from environment: $NVIM"
19+
else
20+
# Find nvim in PATH
21+
NVIM=$(which nvim)
22+
if [ -z "$NVIM" ]; then
23+
echo "Error: nvim not found in PATH"
24+
exit 1
25+
fi
2126
fi
2227

2328
echo "Running tests with $NVIM"

scripts/test_nvim_detection.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Test script to verify NVIM environment variable detection logic
5+
# This script tests the fix for handling NVIM variable when running inside Neovim
6+
7+
echo "Testing NVIM environment variable detection..."
8+
9+
# Save original NVIM value
10+
ORIGINAL_NVIM="$NVIM"
11+
12+
# Test 1: NVIM points to a socket (simulating inside Neovim)
13+
echo "Test 1: NVIM points to a socket"
14+
export NVIM="/tmp/test_socket"
15+
mkfifo "$NVIM" 2>/dev/null || true # Create a named pipe (similar to socket)
16+
if timeout 5 bash -c 'cd "$(dirname "$0")" && ./scripts/test.sh' 2>&1 | head -10 | grep -q "Running tests with.*nvim"; then
17+
echo "✓ Fallback to PATH works"
18+
else
19+
echo "✗ Fallback failed"
20+
fi
21+
rm -f "$NVIM"
22+
23+
# Test 2: NVIM points to valid executable
24+
echo "Test 2: NVIM points to valid executable"
25+
export NVIM="$(which nvim)"
26+
if timeout 5 bash -c 'cd "$(dirname "$0")" && ./scripts/test.sh' 2>&1 | head -10 | grep -q "Using NVIM from environment"; then
27+
echo "✓ Using provided NVIM works"
28+
else
29+
echo "✗ Using provided NVIM failed"
30+
fi
31+
32+
# Test 3: NVIM points to non-existent path
33+
echo "Test 3: NVIM points to non-existent path"
34+
export NVIM="/nonexistent/nvim"
35+
if timeout 5 bash -c 'cd "$(dirname "$0")" && ./scripts/test.sh' 2>&1 | head -10 | grep -q "Running tests with.*nvim"; then
36+
echo "✓ Fallback from invalid path works"
37+
else
38+
echo "✗ Fallback from invalid path failed"
39+
fi
40+
41+
# Test 4: NVIM is unset
42+
echo "Test 4: NVIM is unset"
43+
unset NVIM
44+
if timeout 5 bash -c 'cd "$(dirname "$0")" && ./scripts/test.sh' 2>&1 | head -10 | grep -q "Running tests with.*nvim"; then
45+
echo "✓ Unset NVIM works"
46+
else
47+
echo "✗ Unset NVIM failed"
48+
fi
49+
50+
# Restore original NVIM value
51+
if [ -n "$ORIGINAL_NVIM" ]; then
52+
export NVIM="$ORIGINAL_NVIM"
53+
else
54+
unset NVIM
55+
fi
56+
57+
echo "All NVIM detection tests completed!"

0 commit comments

Comments
 (0)