Skip to content

Commit 6ad256e

Browse files
committed
add: test scripts and documentation for PATH directory order testing in minishell
1 parent 68a4d36 commit 6ad256e

File tree

5 files changed

+167
-9
lines changed

5 files changed

+167
-9
lines changed

docs/dir1/test

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo "This is from dir1"

docs/dir2/test

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo "This is from dir2"

docs/path-testing-en.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

docs/path-testing-ja.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
こちらのガイドは、Minishellでの PATH ディレクトリの順序テストに関する説明ですね。すでにリポジトリにテスト環境があることを明記したいとのことですので、以下のように修正しました:
2+
3+
# Minishellにおける PATH ディレクトリ順序テストガイド
4+
このドキュメントでは、Minishell実装が PATH 環境変数内のディレクトリを左から右への順序で正しく検索するかをテストする方法を説明します。
5+
6+
## テスト環境(すでにリポジトリに存在)
7+
以下のテスト環境はすでにリポジトリに存在しています:
8+
1. テストディレクトリ構造:
9+
```
10+
docs/dir1
11+
docs/dir2
12+
```
13+
14+
2. 最初のディレクトリ内のテスト実行ファイル:
15+
```
16+
docs/dir1/test (実行可能、"This is from dir1"を出力)
17+
```
18+
19+
3. 2番目のディレクトリ内の同名テスト実行ファイル:
20+
```
21+
docs/dir2/test (実行可能、"This is from dir2"を出力)
22+
```
23+
24+
4. 各スクリプトが正常に動作することを確認:
25+
```bash
26+
./docs/dir1/test # "This is from dir1"と表示されるはず
27+
./docs/dir2/test # "This is from dir2"と表示されるはず
28+
```
29+
30+
## Minishellでのテスト手順
31+
1. Minishell実装を起動:
32+
```bash
33+
./minishell
34+
```
35+
36+
2. PATH環境変数をdir1がdir2よりも前になるように設定:
37+
```bash
38+
export PATH=/full_path_to/docs/dir1:/full_path_to/docs/dir2:$PATH
39+
```
40+
(`/full_path_to/`は実際のパスに置き換え、例えば`$(pwd)/`など)
41+
42+
3. PATHが正しく設定されていることを確認:
43+
```bash
44+
echo $PATH
45+
```
46+
47+
4. テストコマンドを実行:
48+
```bash
49+
test
50+
```
51+
PATH順序が正しければ、dir1/testが実行され「This is from dir1」と表示されるはず
52+
53+
5. 順序を逆にして動作の変化を確認:
54+
```bash
55+
export PATH=/full_path_to/docs/dir2:/full_path_to/docs/dir1:$PATH
56+
test
57+
```
58+
これで「This is from dir2」と表示されるはず
59+
60+
## トラブルシューティング
61+
「command not found」が表示される場合:
62+
- PATHのパスが相対パスではなく絶対パスであることを確認
63+
- ディレクトリが存在しアクセス可能であることを確認
64+
- コマンド名がビルトインと競合していないか確認
65+
66+
「Exec format error」が表示される場合:
67+
- スクリプトの改行コードが正しいこと(CRLF ではなく LF)を確認
68+
- シェバンインタプリタ(#!/bin/bash)が存在することを確認
69+
- ファイルのパーミッションが正しいこと(chmod +x)を確認
70+
71+
間違ったスクリプトが実行される場合:
72+
- `which test` を使用してどのパスが見つかっているかを確認
73+
- 同じ名前のシステムコマンドが存在するか確認
74+
- システムコマンドと競合しない一意のコマンド名を試す
75+
76+
## 実装に関する注意点
77+
Minishell実装では、以下を確認してください:
78+
1. PATHはコロンで区切られてディレクトリの配列に分割される
79+
2. ディレクトリは左から右の順序で検索される
80+
3. 要求された名前の最初に見つかった実行可能ファイルが実行される
81+
4. どのディレクトリにも実行可能ファイルが見つからない場合、「command not found」が返される

src/executor/executor_path.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: pchung <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/03/08 23:24:32 by pchung #+# #+# */
9-
/* Updated: 2025/03/08 23:32:07 by pchung ### ########.fr */
9+
/* Updated: 2025/03/11 12:14:16 by pchung ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,21 +16,15 @@ static char *search_executable_in_paths(const char *cmd, char *paths)
1616
{
1717
char *dir;
1818
char full_path[1024];
19-
size_t dir_len;
20-
size_t cmd_len;
2119

2220
dir = ft_strtok(paths, ":");
2321
while (dir)
2422
{
2523
ft_snprintf(full_path, sizeof(full_path), "%s/%s", dir, cmd);
26-
dir_len = ft_strlen(dir);
27-
cmd_len = ft_strlen(cmd);
28-
memcpy(full_path, dir, dir_len);
29-
full_path[dir_len] = '/';
30-
memcpy(full_path + dir_len + 1, cmd, cmd_len);
31-
full_path[dir_len + cmd_len + 1] = '\0';
3224
if (access(full_path, X_OK) == 0)
25+
{
3326
return (ft_strdup(full_path));
27+
}
3428
dir = ft_strtok(NULL, ":");
3529
}
3630
return (NULL);

0 commit comments

Comments
 (0)