From 0988ec82266c79444cf764a4a473679e76c46cc0 Mon Sep 17 00:00:00 2001 From: Dane Horn Date: Sat, 14 May 2022 10:57:40 +0200 Subject: [PATCH 1/3] adjust regex for directory name new regex allows for more flexible folder names, while still disallowing '.' and '..' --- src/Terminal/DirectoryHelpers.ts | 2 +- test/jest/Terminal/Directory.test.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Terminal/DirectoryHelpers.ts b/src/Terminal/DirectoryHelpers.ts index 56574afd4..3b9eb0b45 100644 --- a/src/Terminal/DirectoryHelpers.ts +++ b/src/Terminal/DirectoryHelpers.ts @@ -51,7 +51,7 @@ export function isValidFilename(filename: string): boolean { export function isValidDirectoryName(name: string): boolean { // Allows alphanumerics, hyphens, underscores, and percentage signs. // Name can begin with a single period, but otherwise cannot have any - const regex = /^.?[a-zA-Z0-9_-]+$/; + const regex = /^\.?(?:\w[_\-.%]*)+$/; // match() returns null if no match is found return name.match(regex) != null; diff --git a/test/jest/Terminal/Directory.test.js b/test/jest/Terminal/Directory.test.js index 337a96ca4..94c3a4d2e 100644 --- a/test/jest/Terminal/Directory.test.js +++ b/test/jest/Terminal/Directory.test.js @@ -102,17 +102,18 @@ describe("Terminal Directory Tests", function () { expect(isValidDirectoryName(".a1")).toEqual(true); expect(isValidDirectoryName("._foo")).toEqual(true); expect(isValidDirectoryName("_foo")).toEqual(true); + expect(isValidDirectoryName("foo.dir")).toEqual(true); + expect(isValidDirectoryName("1.")).toEqual(true); + expect(isValidDirectoryName("foo.")).toEqual(true); }); it("should return false for invalid directory names", function () { expect(isValidDirectoryName("")).toEqual(false); - expect(isValidDirectoryName("foo.dir")).toEqual(false); - expect(isValidDirectoryName("1.")).toEqual(false); - expect(isValidDirectoryName("foo.")).toEqual(false); expect(isValidDirectoryName("dir#")).toEqual(false); expect(isValidDirectoryName("dir!")).toEqual(false); expect(isValidDirectoryName("dir*")).toEqual(false); expect(isValidDirectoryName(".")).toEqual(false); + expect(isValidDirectoryName("..")).toEqual(false); }); }); From 1a9d3ee9982a0444799a0a91b8388cc8e4a370e9 Mon Sep 17 00:00:00 2001 From: Dane Horn Date: Sat, 14 May 2022 15:57:25 +0200 Subject: [PATCH 2/3] clean up regex and adjust tests to new criteria --- src/Terminal/DirectoryHelpers.ts | 8 +++++--- test/jest/Terminal/Directory.test.js | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Terminal/DirectoryHelpers.ts b/src/Terminal/DirectoryHelpers.ts index 3b9eb0b45..fb15187b9 100644 --- a/src/Terminal/DirectoryHelpers.ts +++ b/src/Terminal/DirectoryHelpers.ts @@ -49,9 +49,11 @@ export function isValidFilename(filename: string): boolean { * not an entire path */ export function isValidDirectoryName(name: string): boolean { - // Allows alphanumerics, hyphens, underscores, and percentage signs. - // Name can begin with a single period, but otherwise cannot have any - const regex = /^\.?(?:\w[_\-.%]*)+$/; + // A valid directory name: + // Must be at least 1 character long + // Can only include characters in the character set [-.%a-zA-Z0-9_] + // Cannot end with a '.' + const regex = /^(?:[-.%]|\w)*[-%a-zA-Z0-9_]$/; // match() returns null if no match is found return name.match(regex) != null; diff --git a/test/jest/Terminal/Directory.test.js b/test/jest/Terminal/Directory.test.js index 94c3a4d2e..79db3a0a1 100644 --- a/test/jest/Terminal/Directory.test.js +++ b/test/jest/Terminal/Directory.test.js @@ -103,17 +103,18 @@ describe("Terminal Directory Tests", function () { expect(isValidDirectoryName("._foo")).toEqual(true); expect(isValidDirectoryName("_foo")).toEqual(true); expect(isValidDirectoryName("foo.dir")).toEqual(true); - expect(isValidDirectoryName("1.")).toEqual(true); - expect(isValidDirectoryName("foo.")).toEqual(true); }); it("should return false for invalid directory names", function () { expect(isValidDirectoryName("")).toEqual(false); + expect(isValidDirectoryName("👨‍💻")).toEqual(false); expect(isValidDirectoryName("dir#")).toEqual(false); expect(isValidDirectoryName("dir!")).toEqual(false); expect(isValidDirectoryName("dir*")).toEqual(false); expect(isValidDirectoryName(".")).toEqual(false); expect(isValidDirectoryName("..")).toEqual(false); + expect(isValidDirectoryName("1.")).toEqual(false); + expect(isValidDirectoryName("foo.")).toEqual(false); }); }); From 28dad4dd30ba994d6c6185006c613e18271c7466 Mon Sep 17 00:00:00 2001 From: Dane Horn Date: Sun, 15 May 2022 09:43:23 +0200 Subject: [PATCH 3/3] expand Directory tests --- test/jest/Terminal/Directory.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/jest/Terminal/Directory.test.js b/test/jest/Terminal/Directory.test.js index 79db3a0a1..ee8b53d41 100644 --- a/test/jest/Terminal/Directory.test.js +++ b/test/jest/Terminal/Directory.test.js @@ -103,6 +103,14 @@ describe("Terminal Directory Tests", function () { expect(isValidDirectoryName("._foo")).toEqual(true); expect(isValidDirectoryName("_foo")).toEqual(true); expect(isValidDirectoryName("foo.dir")).toEqual(true); + expect(isValidDirectoryName("foov1.0.0.1")).toEqual(true); + expect(isValidDirectoryName("foov1..0..0..1")).toEqual(true); + expect(isValidDirectoryName("foov1-0-0-1")).toEqual(true); + expect(isValidDirectoryName("foov1-0-0-1-")).toEqual(true); + expect(isValidDirectoryName("foov1--0--0--1--")).toEqual(true); + expect(isValidDirectoryName("foov1_0_0_1")).toEqual(true); + expect(isValidDirectoryName("foov1_0_0_1_")).toEqual(true); + expect(isValidDirectoryName("foov1__0__0__1")).toEqual(true); }); it("should return false for invalid directory names", function () { @@ -115,6 +123,7 @@ describe("Terminal Directory Tests", function () { expect(isValidDirectoryName("..")).toEqual(false); expect(isValidDirectoryName("1.")).toEqual(false); expect(isValidDirectoryName("foo.")).toEqual(false); + expect(isValidDirectoryName("foov1.0.0.1.")).toEqual(false); }); });