From dc4f00b3c050d813d9cf711ac87a9696d373b9c2 Mon Sep 17 00:00:00 2001 From: pawelsadlo <106806258+pawelsadlo@users.noreply.github.com> Date: Sat, 2 Nov 2024 14:06:39 +0100 Subject: [PATCH 1/7] Update Path.scala --- os/src/Path.scala | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/os/src/Path.scala b/os/src/Path.scala index 37639686..e9128ba4 100644 --- a/os/src/Path.scala +++ b/os/src/Path.scala @@ -26,10 +26,15 @@ object PathChunk extends PathChunkMacros { val splitted = strNoTrailingSeps.split('/') splitted ++ Array.fill(trailingSeparatorsCount)("") } - + private def reduceUps(in:List[String]) = in.foldLeft(List.empty[String]){case (acc,x) => + acc match{ + case h :: t if h == ".." => x :: acc + case h :: t if x == ".." => t + case _ => x :: acc} + }.reverse private[os] def segmentsFromStringLiteralValidation(literal: String): Array[String] = { val stringSegments = segmentsFromString(literal) - val validSegmnts = validLiteralSegments(stringSegments) + val validSegmnts = reduceUps(validLiteralSegments(stringSegments)) val sanitizedLiteral = validSegmnts.mkString("/") if (validSegmnts.isEmpty) throw InvalidSegment( literal, From f470844d9ff4c84159f2ab986b3978ad50116d96 Mon Sep 17 00:00:00 2001 From: pawelsadlo <106806258+pawelsadlo@users.noreply.github.com> Date: Sat, 2 Nov 2024 14:14:47 +0100 Subject: [PATCH 2/7] Update Path.scala --- os/src/Path.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/src/Path.scala b/os/src/Path.scala index e9128ba4..eb5194d2 100644 --- a/os/src/Path.scala +++ b/os/src/Path.scala @@ -26,7 +26,7 @@ object PathChunk extends PathChunkMacros { val splitted = strNoTrailingSeps.split('/') splitted ++ Array.fill(trailingSeparatorsCount)("") } - private def reduceUps(in:List[String]) = in.foldLeft(List.empty[String]){case (acc,x) => + private def reduceUps(in:Array[String]): List[String] = in.foldLeft(List.empty[String]){case (acc,x) => acc match{ case h :: t if h == ".." => x :: acc case h :: t if x == ".." => t From d34ceaf0cd95dd11db34926182ead416fb560e11 Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 6 Nov 2024 09:53:40 +0100 Subject: [PATCH 3/7] fix and add tests --- os/test/src/PathTests.scala | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/os/test/src/PathTests.scala b/os/test/src/PathTests.scala index a12519b3..95a435ca 100644 --- a/os/test/src/PathTests.scala +++ b/os/test/src/PathTests.scala @@ -23,14 +23,28 @@ object PathTests extends TestSuite { test("literals with [..]") { assert(rel / "src" / ".." == rel / "src" / os.up) - assert(root / "src/.." == root / "src" / os.up) assert(root / "src" / ".." == root / "src" / os.up) assert(root / "hello" / ".." / "world" == root / "hello" / os.up / "world") assert(root / "hello" / "../world" == root / "hello" / os.up / "world") - assert(root / "hello/../world" == root / "hello" / os.up / "world") } test("Compile errors") { + + compileError("""root / "src/../foo"""").check("", nonCanonicalLiteral("src/../foo","foo")) + compileError("""root / "hello/../world"""").check("", nonCanonicalLiteral("hello/../world","world")) + compileError("""root / "src/../foo/bar"""").check("", nonCanonicalLiteral("src/../foo/bar","foo/bar")) + compileError("""root / "src/../foo/bar/.."""").check("", nonCanonicalLiteral("src/../foo/bar/..","foo")) + compileError("""root / "src/../foo/../bar/."""").check("", nonCanonicalLiteral("src/../foo/../bar/.","bar")) + compileError("""root / "src/foo/./.."""").check("", nonCanonicalLiteral("src/foo/./..","src")) + compileError("""root / "src/foo//./.."""").check("", nonCanonicalLiteral("src/foo//./..","src")) + + compileError("""root / "src/.."""").check("", removeLiteralErr("src/..")) + compileError("""root / "src/../foo/.."""").check("", removeLiteralErr("src/../foo/..")) + compileError("""root / "src/foo/../.."""").check("", removeLiteralErr("src/foo/../..")) + compileError("""root / "src/foo/./../.."""").check("", removeLiteralErr("src/foo/./../..")) + compileError("""root / "src/./foo/./../.."""").check("", removeLiteralErr("src/./foo/./../..")) + compileError("""root / "src///foo/./../.."""").check("", removeLiteralErr("src///foo/./../..")) + compileError("""root / "/" """).check("", removeLiteralErr("/")) compileError("""root / "/ " """).check("", nonCanonicalLiteral("/ ", " ")) compileError("""root / " /" """).check("", nonCanonicalLiteral(" /", " ")) @@ -40,10 +54,7 @@ object PathTests extends TestSuite { compileError("""root / "foo//" """).check("", nonCanonicalLiteral("foo//", "foo")) compileError("""root / "foo/bar/" """).check("", nonCanonicalLiteral("foo/bar/", "foo/bar")) - compileError("""root / "foo/bar//" """).check( - "", - nonCanonicalLiteral("foo/bar//", "foo/bar") - ) + compileError("""root / "foo/bar//" """).check("", nonCanonicalLiteral("foo/bar//", "foo/bar")) compileError("""root / "/foo" """).check("", nonCanonicalLiteral("/foo", "foo")) compileError("""root / "//foo" """).check("", nonCanonicalLiteral("//foo", "foo")) From 9cfdc2a012df17990ad45cde8d8b243ade4e2438 Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 6 Nov 2024 10:03:08 +0100 Subject: [PATCH 4/7] fmt --- os/src/Path.scala | 14 ++++++----- os/test/src/PathTests.scala | 47 +++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/os/src/Path.scala b/os/src/Path.scala index eb5194d2..e135c4c5 100644 --- a/os/src/Path.scala +++ b/os/src/Path.scala @@ -26,12 +26,14 @@ object PathChunk extends PathChunkMacros { val splitted = strNoTrailingSeps.split('/') splitted ++ Array.fill(trailingSeparatorsCount)("") } - private def reduceUps(in:Array[String]): List[String] = in.foldLeft(List.empty[String]){case (acc,x) => - acc match{ - case h :: t if h == ".." => x :: acc - case h :: t if x == ".." => t - case _ => x :: acc} - }.reverse + private def reduceUps(in: Array[String]): List[String] = + in.foldLeft(List.empty[String]) { case (acc, x) => + acc match { + case h :: t if h == ".." => x :: acc + case h :: t if x == ".." => t + case _ => x :: acc + } + }.reverse private[os] def segmentsFromStringLiteralValidation(literal: String): Array[String] = { val stringSegments = segmentsFromString(literal) val validSegmnts = reduceUps(validLiteralSegments(stringSegments)) diff --git a/os/test/src/PathTests.scala b/os/test/src/PathTests.scala index 95a435ca..eaac715c 100644 --- a/os/test/src/PathTests.scala +++ b/os/test/src/PathTests.scala @@ -30,20 +30,44 @@ object PathTests extends TestSuite { test("Compile errors") { - compileError("""root / "src/../foo"""").check("", nonCanonicalLiteral("src/../foo","foo")) - compileError("""root / "hello/../world"""").check("", nonCanonicalLiteral("hello/../world","world")) - compileError("""root / "src/../foo/bar"""").check("", nonCanonicalLiteral("src/../foo/bar","foo/bar")) - compileError("""root / "src/../foo/bar/.."""").check("", nonCanonicalLiteral("src/../foo/bar/..","foo")) - compileError("""root / "src/../foo/../bar/."""").check("", nonCanonicalLiteral("src/../foo/../bar/.","bar")) - compileError("""root / "src/foo/./.."""").check("", nonCanonicalLiteral("src/foo/./..","src")) - compileError("""root / "src/foo//./.."""").check("", nonCanonicalLiteral("src/foo//./..","src")) + compileError("""root / "src/../foo"""").check("", nonCanonicalLiteral("src/../foo", "foo")) + compileError("""root / "hello/../world"""").check( + "", + nonCanonicalLiteral("hello/../world", "world") + ) + compileError("""root / "src/../foo/bar"""").check( + "", + nonCanonicalLiteral("src/../foo/bar", "foo/bar") + ) + compileError("""root / "src/../foo/bar/.."""").check( + "", + nonCanonicalLiteral("src/../foo/bar/..", "foo") + ) + compileError("""root / "src/../foo/../bar/."""").check( + "", + nonCanonicalLiteral("src/../foo/../bar/.", "bar") + ) + compileError("""root / "src/foo/./.."""").check( + "", + nonCanonicalLiteral("src/foo/./..", "src") + ) + compileError("""root / "src/foo//./.."""").check( + "", + nonCanonicalLiteral("src/foo//./..", "src") + ) compileError("""root / "src/.."""").check("", removeLiteralErr("src/..")) compileError("""root / "src/../foo/.."""").check("", removeLiteralErr("src/../foo/..")) compileError("""root / "src/foo/../.."""").check("", removeLiteralErr("src/foo/../..")) compileError("""root / "src/foo/./../.."""").check("", removeLiteralErr("src/foo/./../..")) - compileError("""root / "src/./foo/./../.."""").check("", removeLiteralErr("src/./foo/./../..")) - compileError("""root / "src///foo/./../.."""").check("", removeLiteralErr("src///foo/./../..")) + compileError("""root / "src/./foo/./../.."""").check( + "", + removeLiteralErr("src/./foo/./../..") + ) + compileError("""root / "src///foo/./../.."""").check( + "", + removeLiteralErr("src///foo/./../..") + ) compileError("""root / "/" """).check("", removeLiteralErr("/")) compileError("""root / "/ " """).check("", nonCanonicalLiteral("/ ", " ")) @@ -54,7 +78,10 @@ object PathTests extends TestSuite { compileError("""root / "foo//" """).check("", nonCanonicalLiteral("foo//", "foo")) compileError("""root / "foo/bar/" """).check("", nonCanonicalLiteral("foo/bar/", "foo/bar")) - compileError("""root / "foo/bar//" """).check("", nonCanonicalLiteral("foo/bar//", "foo/bar")) + compileError("""root / "foo/bar//" """).check( + "", + nonCanonicalLiteral("foo/bar//", "foo/bar") + ) compileError("""root / "/foo" """).check("", nonCanonicalLiteral("/foo", "foo")) compileError("""root / "//foo" """).check("", nonCanonicalLiteral("//foo", "foo")) From de5a81789b3c76ee14d887e2c8aed13b024622c1 Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 6 Nov 2024 10:22:05 +0100 Subject: [PATCH 5/7] fix --- os/test/src-jvm/ExpectyIntegration.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/os/test/src-jvm/ExpectyIntegration.scala b/os/test/src-jvm/ExpectyIntegration.scala index da6add3b..c1227dae 100644 --- a/os/test/src-jvm/ExpectyIntegration.scala +++ b/os/test/src-jvm/ExpectyIntegration.scala @@ -14,11 +14,9 @@ object ExpectyIntegration extends TestSuite { } test("literals with [..]") { expect(rel / "src" / ".." == rel / "src" / os.up) - expect(root / "src/.." == root / "src" / os.up) expect(root / "src" / ".." == root / "src" / os.up) expect(root / "hello" / ".." / "world" == root / "hello" / os.up / "world") expect(root / "hello" / "../world" == root / "hello" / os.up / "world") - expect(root / "hello/../world" == root / "hello" / os.up / "world") } test("from issue") { expect(Seq(os.pwd / "foo") == Seq(os.pwd / "foo")) From 23968ac363d89addfd063e6f0abbf05f35e470cd Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 6 Nov 2024 11:11:12 +0100 Subject: [PATCH 6/7] fix --- os/test/src-jvm/ExpectyIntegration.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/test/src-jvm/ExpectyIntegration.scala b/os/test/src-jvm/ExpectyIntegration.scala index c1227dae..feb19d83 100644 --- a/os/test/src-jvm/ExpectyIntegration.scala +++ b/os/test/src-jvm/ExpectyIntegration.scala @@ -24,7 +24,7 @@ object ExpectyIntegration extends TestSuite { expect(path.startsWith(os.Path("/") / "tmp")) } test("multiple args") { - expect(rel / "src" / ".." == rel / "src" / os.up, root / "src/.." == root / "src" / os.up) + expect(rel / "src" / ".." == rel / "src" / os.up, root / "src" / "../foo" == root / "src" / os.up / "foo") } } } From 9ae4b3bb40ac21be73904d21cc2824b6ebf82baa Mon Sep 17 00:00:00 2001 From: Pawel Sadlo Date: Wed, 6 Nov 2024 11:56:55 +0100 Subject: [PATCH 7/7] fmt --- os/test/src-jvm/ExpectyIntegration.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/os/test/src-jvm/ExpectyIntegration.scala b/os/test/src-jvm/ExpectyIntegration.scala index feb19d83..69e2205a 100644 --- a/os/test/src-jvm/ExpectyIntegration.scala +++ b/os/test/src-jvm/ExpectyIntegration.scala @@ -24,7 +24,10 @@ object ExpectyIntegration extends TestSuite { expect(path.startsWith(os.Path("/") / "tmp")) } test("multiple args") { - expect(rel / "src" / ".." == rel / "src" / os.up, root / "src" / "../foo" == root / "src" / os.up / "foo") + expect( + rel / "src" / ".." == rel / "src" / os.up, + root / "src" / "../foo" == root / "src" / os.up / "foo" + ) } } }