|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as os from "os"; |
| 4 | +import { globFiles } from "../src/glob"; |
| 5 | + |
| 6 | +let tmpDir: string; |
| 7 | + |
| 8 | +beforeEach(async () => { |
| 9 | + tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "glob-test-")); |
| 10 | +}); |
| 11 | + |
| 12 | +afterEach(async () => { |
| 13 | + await fs.promises.rm(tmpDir, { recursive: true, force: true }); |
| 14 | +}); |
| 15 | + |
| 16 | +/** Helper: create a file (and any parent dirs) under tmpDir. */ |
| 17 | +async function touch(...segments: string[]): Promise<string> { |
| 18 | + const filePath = path.join(tmpDir, ...segments); |
| 19 | + await fs.promises.mkdir(path.dirname(filePath), { recursive: true }); |
| 20 | + await fs.promises.writeFile(filePath, ""); |
| 21 | + return filePath; |
| 22 | +} |
| 23 | + |
| 24 | +describe("globFiles", () => { |
| 25 | + describe("core behavior", () => { |
| 26 | + it("returns absolute paths", async () => { |
| 27 | + await touch("a.js"); |
| 28 | + const result = await globFiles(path.join(tmpDir, "**/*.js")); |
| 29 | + expect(result).toHaveLength(1); |
| 30 | + expect(path.isAbsolute(result[0]!)).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it("excludes directories (nodir)", async () => { |
| 34 | + // Create a directory that matches the glob pattern |
| 35 | + await fs.promises.mkdir(path.join(tmpDir, "subdir.js"), { recursive: true }); |
| 36 | + await touch("real.js"); |
| 37 | + const result = await globFiles(path.join(tmpDir, "**/*.js")); |
| 38 | + expect(result).toEqual([path.join(tmpDir, "real.js")]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("returns [] for no matches", async () => { |
| 42 | + await touch("a.txt"); |
| 43 | + const result = await globFiles(path.join(tmpDir, "**/*.js")); |
| 44 | + expect(result).toEqual([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns [] for nonexistent pattern path", async () => { |
| 48 | + const result = await globFiles(path.join(tmpDir, "nonexistent/**/*.js")); |
| 49 | + expect(result).toEqual([]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("matches deeply nested files", async () => { |
| 53 | + const filePath = await touch("a", "b", "c", "deep.js"); |
| 54 | + const result = await globFiles(path.join(tmpDir, "**/*.js")); |
| 55 | + expect(result).toEqual([filePath]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("works with a string pattern", async () => { |
| 59 | + await touch("single.js"); |
| 60 | + const result = await globFiles(path.join(tmpDir, "*.js")); |
| 61 | + expect(result).toHaveLength(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it("works with an array of patterns", async () => { |
| 65 | + const jsFile = await touch("a.js"); |
| 66 | + const mapFile = await touch("a.js.map"); |
| 67 | + await touch("b.css"); |
| 68 | + |
| 69 | + const result = await globFiles([ |
| 70 | + path.join(tmpDir, "**/*.js"), |
| 71 | + path.join(tmpDir, "**/*.js.map"), |
| 72 | + ]); |
| 73 | + result.sort(); |
| 74 | + expect(result).toEqual([jsFile, mapFile].sort()); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("root option", () => { |
| 79 | + it("scopes results to root directory", async () => { |
| 80 | + await touch("file.js"); |
| 81 | + // Patterns starting with / are resolved relative to root |
| 82 | + const result = await globFiles("/**/*.js", { root: tmpDir }); |
| 83 | + expect(result).toEqual([path.join(tmpDir, "file.js")]); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("ignore option", () => { |
| 88 | + it("excludes files matching ignore string pattern", async () => { |
| 89 | + await touch("keep.js"); |
| 90 | + await touch("node_modules", "dep.js"); |
| 91 | + |
| 92 | + const result = await globFiles(path.join(tmpDir, "**/*.js"), { |
| 93 | + ignore: path.join(tmpDir, "node_modules/**"), |
| 94 | + }); |
| 95 | + expect(result).toEqual([path.join(tmpDir, "keep.js")]); |
| 96 | + }); |
| 97 | + |
| 98 | + it("excludes files matching ignore array patterns", async () => { |
| 99 | + await touch("keep.js"); |
| 100 | + await touch("node_modules", "dep.js"); |
| 101 | + await touch("dist", "bundle.js"); |
| 102 | + |
| 103 | + const result = await globFiles(path.join(tmpDir, "**/*.js"), { |
| 104 | + ignore: [path.join(tmpDir, "node_modules/**"), path.join(tmpDir, "dist/**")], |
| 105 | + }); |
| 106 | + expect(result).toEqual([path.join(tmpDir, "keep.js")]); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe("rollup JS/map patterns", () => { |
| 111 | + const JS_AND_MAP_PATTERNS = [ |
| 112 | + "/**/*.js", |
| 113 | + "/**/*.mjs", |
| 114 | + "/**/*.cjs", |
| 115 | + "/**/*.js.map", |
| 116 | + "/**/*.mjs.map", |
| 117 | + "/**/*.cjs.map", |
| 118 | + ].map((q) => `${q}?(\\?*)?(#*)`); |
| 119 | + |
| 120 | + it("matches .js, .mjs, .cjs and their .map variants", async () => { |
| 121 | + const files = await Promise.all([ |
| 122 | + touch("a.js"), |
| 123 | + touch("b.mjs"), |
| 124 | + touch("c.cjs"), |
| 125 | + touch("a.js.map"), |
| 126 | + touch("b.mjs.map"), |
| 127 | + touch("c.cjs.map"), |
| 128 | + ]); |
| 129 | + |
| 130 | + const result = await globFiles(JS_AND_MAP_PATTERNS, { root: tmpDir }); |
| 131 | + result.sort(); |
| 132 | + expect(result).toEqual(files.sort()); |
| 133 | + }); |
| 134 | + |
| 135 | + it("does NOT match .css, .ts, .json, etc.", async () => { |
| 136 | + await touch("style.css"); |
| 137 | + await touch("types.ts"); |
| 138 | + await touch("data.json"); |
| 139 | + await touch("readme.md"); |
| 140 | + |
| 141 | + const result = await globFiles(JS_AND_MAP_PATTERNS, { root: tmpDir }); |
| 142 | + expect(result).toEqual([]); |
| 143 | + }); |
| 144 | + |
| 145 | + it("works in nested subdirectories", async () => { |
| 146 | + const files = await Promise.all([ |
| 147 | + touch("src", "deep", "a.js"), |
| 148 | + touch("src", "deep", "a.js.map"), |
| 149 | + ]); |
| 150 | + |
| 151 | + const result = await globFiles(JS_AND_MAP_PATTERNS, { root: tmpDir }); |
| 152 | + result.sort(); |
| 153 | + expect(result).toEqual(files.sort()); |
| 154 | + }); |
| 155 | + |
| 156 | + it("returns [] for empty directory", async () => { |
| 157 | + const result = await globFiles(JS_AND_MAP_PATTERNS, { root: tmpDir }); |
| 158 | + expect(result).toEqual([]); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments