Skip to content

Commit 69cc8a3

Browse files
authored
Update error messages to allow better error messages for invalid path segments (#301)
Since #297 landed, the old error messages have become somewhat misleading. This attempts to make them a bit more accurate, and suggest to users that literal string path segments have more flexibility than dynamically computed ones
1 parent 4262d5c commit 69cc8a3

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: ci
22

33
on:
44
push:
5+
branches:
6+
- main
57
pull_request:
68
branches:
79
- main

.scalafmt.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ newlines.source = keep
1919

2020
runner.dialect = scala213
2121

22+
project.excludePaths = [
23+
"glob:**/src-3/**"
24+
]

Readme.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ string, int or set representations of the `os.PermSet` via:
22502250
[#main]
22512251
=== main
22522252

2253-
* Make `os.pwd` modifiable via the `os.dynamicPwd0`
2253+
* Allow multi-segment paths segments for literals https://github.com/com-lihaoyi/os-lib/pull/297
22542254

22552255
[#0-10-6]
22562256
=== 0.10.6

os/src/Path.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,34 +171,31 @@ object BasePath {
171171
def checkSegment(s: String) = {
172172
def fail(msg: String) = throw PathError.InvalidSegment(s, msg)
173173
def considerStr =
174-
"use the Path(...) or RelPath(...) constructor calls to convert them. "
174+
"If you are dealing with dynamic path-strings coming from external sources, " +
175+
"use the Path(...)/RelPath(...)/SubPath(...) constructor calls to convert them."
175176

176177
s.indexOf('/') match {
177178
case -1 => // do nothing
178179
case c => fail(
179-
s"[/] is not a valid character to appear in a path segment. " +
180-
"If you want to parse an absolute or relative path that may have " +
181-
"multiple segments, e.g. path-strings coming from external sources " +
180+
s"[/] is not a valid character to appear in a non-literal path segment. " +
182181
considerStr
183182
)
184183

185184
}
186-
def externalStr = "If you are dealing with path-strings coming from external sources, "
187185
s match {
188186
case "" =>
189187
fail(
190-
"OS-Lib does not allow empty path segments " +
191-
externalStr + considerStr
188+
"OS-Lib does not allow empty path segments. " +
189+
considerStr
192190
)
193191
case "." =>
194192
fail(
195-
"OS-Lib does not allow [.] as a path segment " +
196-
externalStr + considerStr
193+
"OS-Lib does not allow [.] in a non-literal path segment. " +
194+
considerStr
197195
)
198196
case ".." =>
199197
fail(
200-
"OS-Lib does not allow [..] as a path segment " +
201-
externalStr +
198+
"OS-Lib does not allow [..] in a non-literal path segment. " +
202199
considerStr +
203200
"If you want to use the `..` segment manually to represent going up " +
204201
"one level in the path, use the `up` segment from `os.up` " +

os/test/src/PathTests.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,13 @@ object PathTests extends TestSuite {
338338

339339
val PathError.InvalidSegment("Main/.scala", msg1) = ex
340340

341-
assert(msg1.contains("[/] is not a valid character to appear in a path segment"))
341+
assert(
342+
msg1.contains(
343+
"[/] is not a valid character to appear in a non-literal path segment. If you are " +
344+
"dealing with dynamic path-strings coming from external sources, use the " +
345+
"Path(...)/RelPath(...)/SubPath(...) constructor calls to convert them."
346+
)
347+
)
342348

343349
val ex2 = intercept[PathError.InvalidSegment](root / "hello" / nonLiteral("..") / "world")
344350

0 commit comments

Comments
 (0)