|
| 1 | +import { afterEach, beforeEach, describe, it } from "vitest"; |
| 2 | +import { |
| 3 | + cleanupTestDir, |
| 4 | + compareOutputs, |
| 5 | + createTestDir, |
| 6 | + setupFiles, |
| 7 | +} from "./test-helpers.js"; |
| 8 | + |
| 9 | +describe("glob expansion - Real Bash Comparison", () => { |
| 10 | + let testDir: string; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + testDir = await createTestDir(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(async () => { |
| 17 | + await cleanupTestDir(testDir); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should expand single-level glob pattern", async () => { |
| 21 | + const env = await setupFiles(testDir, { |
| 22 | + "file1.txt": "content 1", |
| 23 | + "file2.txt": "content 2", |
| 24 | + "file3.json": "{}", |
| 25 | + }); |
| 26 | + await compareOutputs(env, testDir, "echo *.txt"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should expand multi-level glob pattern with wildcard directory", async () => { |
| 30 | + const env = await setupFiles(testDir, { |
| 31 | + "folder1/data.json": '{"a":1}', |
| 32 | + "folder2/data.json": '{"b":2}', |
| 33 | + "folder3/other.txt": "text", |
| 34 | + }); |
| 35 | + await compareOutputs(env, testDir, "echo */*.json"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should expand multi-level glob pattern with absolute path", async () => { |
| 39 | + const env = await setupFiles(testDir, { |
| 40 | + "dm/folder1/data.json": '{"a":1}', |
| 41 | + "dm/folder2/data.json": '{"b":2}', |
| 42 | + "dm/folder3/other.txt": "text", |
| 43 | + }); |
| 44 | + await compareOutputs(env, testDir, "cat dm/*/*.json"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should expand triple-level glob pattern", async () => { |
| 48 | + const env = await setupFiles(testDir, { |
| 49 | + "a/b/c/file.txt": "abc", |
| 50 | + "a/d/e/file.txt": "ade", |
| 51 | + "x/y/z/file.txt": "xyz", |
| 52 | + }); |
| 53 | + await compareOutputs(env, testDir, "echo */*/*/*.txt"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should expand glob with question mark", async () => { |
| 57 | + const env = await setupFiles(testDir, { |
| 58 | + "file1.txt": "1", |
| 59 | + "file2.txt": "2", |
| 60 | + "file10.txt": "10", |
| 61 | + }); |
| 62 | + await compareOutputs(env, testDir, "echo file?.txt"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should expand glob with character class", async () => { |
| 66 | + const env = await setupFiles(testDir, { |
| 67 | + "file1.txt": "1", |
| 68 | + "file2.txt": "2", |
| 69 | + "file3.txt": "3", |
| 70 | + "filea.txt": "a", |
| 71 | + }); |
| 72 | + await compareOutputs(env, testDir, "echo file[12].txt"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should return pattern when no matches", async () => { |
| 76 | + const env = await setupFiles(testDir, { |
| 77 | + "file.txt": "content", |
| 78 | + }); |
| 79 | + await compareOutputs(env, testDir, "echo *.xyz"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should expand glob with grep command", async () => { |
| 83 | + const env = await setupFiles(testDir, { |
| 84 | + "dir1/file.txt": "hello world", |
| 85 | + "dir2/file.txt": "hello there", |
| 86 | + "dir3/other.txt": "goodbye", |
| 87 | + }); |
| 88 | + await compareOutputs(env, testDir, "grep hello */*.txt"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should expand glob at root level with subdirectory pattern", async () => { |
| 92 | + const env = await setupFiles(testDir, { |
| 93 | + "src/a/test.ts": "test a", |
| 94 | + "src/b/test.ts": "test b", |
| 95 | + "lib/c/test.ts": "test c", |
| 96 | + }); |
| 97 | + await compareOutputs(env, testDir, "echo */*/test.ts"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should handle mixed glob and literal segments", async () => { |
| 101 | + const env = await setupFiles(testDir, { |
| 102 | + "data/v1/config.json": "v1 config", |
| 103 | + "data/v2/config.json": "v2 config", |
| 104 | + "data/v1/settings.json": "v1 settings", |
| 105 | + }); |
| 106 | + await compareOutputs(env, testDir, "cat data/*/config.json"); |
| 107 | + }); |
| 108 | +}); |
0 commit comments