Skip to content

Commit 9cd6448

Browse files
committed
fix: regex
1 parent 33377c4 commit 9cd6448

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lib/extractor/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,9 @@ export function isWhitedOutFile(filename: string) {
271271
* Remove the .wh. prefix from a whiteout file to get the original filename
272272
*/
273273
export function removeWhiteoutPrefix(filename: string): string {
274-
// Replace .wh. that appears at the start or after the last slash
275-
return filename.replace(/^(.*\/)?\.wh\./, "$1");
274+
// Replace .wh. that appears at the start or after the last slash,
275+
// and ensure no slashes come after .wh.
276+
return filename.replace(/^(.*\/)?\.wh\.([^\/]*)$/, "$1$2");
276277
}
277278

278279
function isBufferType(type: FileContent): type is Buffer {

test/lib/extractor/index.spec.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getContentAsString, isWhitedOutFile } from "../../../lib/extractor";
1+
import { getContentAsString, isWhitedOutFile, removeWhiteoutPrefix } from "../../../lib/extractor";
22
import { ExtractAction, ExtractedLayers } from "../../../lib/extractor/types";
33

44
describe("index", () => {
@@ -55,3 +55,44 @@ describe("isWhitedOutFile", () => {
5555
expect(isWhitedOutFile("/the/.wh./in/path/present")).toBe(false);
5656
});
5757
});
58+
59+
describe("removeWhiteoutPrefix", () => {
60+
test("should remove .wh. prefix from filenames without slashes", () => {
61+
expect(removeWhiteoutPrefix(".wh.hosts")).toBe("hosts");
62+
expect(removeWhiteoutPrefix(".wh.data")).toBe("data");
63+
expect(removeWhiteoutPrefix(".wh.config")).toBe("config");
64+
expect(removeWhiteoutPrefix(".wh.")).toBe("");
65+
expect(removeWhiteoutPrefix(".wh.file.txt")).toBe("file.txt");
66+
});
67+
68+
test("should remove .wh. prefix after the last slash in paths", () => {
69+
expect(removeWhiteoutPrefix("/etc/.wh.hosts")).toBe("/etc/hosts");
70+
expect(removeWhiteoutPrefix("/var/lib/.wh.data")).toBe("/var/lib/data");
71+
expect(removeWhiteoutPrefix("/.wh.config")).toBe("/config");
72+
expect(removeWhiteoutPrefix("/deeply/nested/path/.wh.present")).toBe("/deeply/nested/path/present");
73+
expect(removeWhiteoutPrefix("/path/to/.wh.")).toBe("/path/to/");
74+
});
75+
76+
test("should not modify files that don't have .wh. prefix in the correct position", () => {
77+
expect(removeWhiteoutPrefix("normal.file")).toBe("normal.file");
78+
expect(removeWhiteoutPrefix("/etc/hosts")).toBe("/etc/hosts");
79+
expect(removeWhiteoutPrefix("middle.wh.file")).toBe("middle.wh.file");
80+
expect(removeWhiteoutPrefix("/path/middle.wh.file")).toBe("/path/middle.wh.file");
81+
expect(removeWhiteoutPrefix(".whfile")).toBe(".whfile");
82+
expect(removeWhiteoutPrefix("/path/.whfile")).toBe("/path/.whfile");
83+
expect(removeWhiteoutPrefix("/path/has/.wh./in/middle")).toBe("/path/has/.wh./in/middle");
84+
});
85+
86+
test("should handle edge cases", () => {
87+
expect(removeWhiteoutPrefix("")).toBe("");
88+
expect(removeWhiteoutPrefix("/")).toBe("/");
89+
expect(removeWhiteoutPrefix("//")).toBe("//");
90+
expect(removeWhiteoutPrefix("/.wh.")).toBe("/");
91+
expect(removeWhiteoutPrefix("//.wh.test")).toBe("//test");
92+
});
93+
94+
test("should not remove .wh. that appears in the middle of paths", () => {
95+
expect(removeWhiteoutPrefix("/the/.wh./in/path/file")).toBe("/the/.wh./in/path/file");
96+
expect(removeWhiteoutPrefix("/path/.wh.dir/.wh.file")).toBe("/path/.wh.dir/file");
97+
});
98+
});

0 commit comments

Comments
 (0)