|
| 1 | +# Guide: Testing PATH Directory Order in Minishell |
| 2 | +This document explains how to test whether a minishell implementation correctly searches for executables in PATH directories in left-to-right order. |
| 3 | + |
| 4 | +## Test Setup (Already in Repository) |
| 5 | +The following test environment already exists in the repository: |
| 6 | +1. Test directory structure: |
| 7 | + ``` |
| 8 | + docs/dir1 |
| 9 | + docs/dir2 |
| 10 | + ``` |
| 11 | + |
| 12 | +2. Test executable in the first directory: |
| 13 | + ``` |
| 14 | + docs/dir1/test (executable, outputs "This is from dir1") |
| 15 | + ``` |
| 16 | + |
| 17 | +3. Test executable with the same name in the second directory: |
| 18 | + ``` |
| 19 | + docs/dir2/test (executable, outputs "This is from dir2") |
| 20 | + ``` |
| 21 | + |
| 22 | +4. Verify both scripts work correctly by running them directly: |
| 23 | + ```bash |
| 24 | + ./docs/dir1/test # Should print "This is from dir1" |
| 25 | + ./docs/dir2/test # Should print "This is from dir2" |
| 26 | + ``` |
| 27 | + |
| 28 | +## Testing in Minishell |
| 29 | +1. Start minishell implementation: |
| 30 | + ```bash |
| 31 | + ./minishell |
| 32 | + ``` |
| 33 | + |
| 34 | +2. Set the PATH environment variable with dir1 before dir2: |
| 35 | + ```bash |
| 36 | + export PATH=/full_path_to/docs/dir1:/full_path_to/docs/dir2:$PATH |
| 37 | + ``` |
| 38 | + (Replace `/full_path_to/` with the actual path, e.g., `$(pwd)/`) |
| 39 | + |
| 40 | +3. Verify the PATH is set correctly: |
| 41 | + ```bash |
| 42 | + echo $PATH |
| 43 | + ``` |
| 44 | + |
| 45 | +4. Run the test command: |
| 46 | + ```bash |
| 47 | + test |
| 48 | + ``` |
| 49 | + If PATH ordering is correct, this should execute dir1/test and print "This is from dir1" |
| 50 | + |
| 51 | +5. Reverse the order to verify behavior changes: |
| 52 | + ```bash |
| 53 | + export PATH=/full_path_to/docs/dir2:/full_path_to/docs/dir1:$PATH |
| 54 | + test |
| 55 | + ``` |
| 56 | + This should now print "This is from dir2" |
| 57 | + |
| 58 | +## Troubleshooting |
| 59 | +If you get "command not found": |
| 60 | +- Ensure the paths in PATH are absolute, not relative |
| 61 | +- Check that the directories exist and are accessible |
| 62 | +- Verify the command name doesn't conflict with built-ins |
| 63 | + |
| 64 | +If you get "Exec format error": |
| 65 | +- Ensure the script has the correct line endings (LF, not CRLF) |
| 66 | +- Verify the shebang interpreter exists (#!/bin/bash) |
| 67 | +- Check file permissions are correct (chmod +x) |
| 68 | + |
| 69 | +If the wrong script executes: |
| 70 | +- Use `which test` to see which path is being found |
| 71 | +- Check if a system command with the same name exists |
| 72 | +- Try with a unique command name that doesn't conflict with system commands |
| 73 | + |
| 74 | +## Implementation Notes |
| 75 | +For minishell implementation, ensure: |
| 76 | +1. PATH is split by colons into an array of directories |
| 77 | +2. Directories are searched in order from left to right |
| 78 | +3. The first executable found with the requested name is executed |
| 79 | +4. If no executable is found in any directory, "command not found" is returned |
0 commit comments