Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update error messages to allow better error messages for invalid path segments #301

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: ci

on:
push:
branches:
- main
pull_request:
branches:
- main
Expand Down
3 changes: 3 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ newlines.source = keep

runner.dialect = scala213

project.excludePaths = [
"glob:**/src-3/**"
]
2 changes: 1 addition & 1 deletion Readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2250,7 +2250,7 @@ string, int or set representations of the `os.PermSet` via:
[#main]
=== main

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

[#0-10-6]
=== 0.10.6
Expand Down
19 changes: 8 additions & 11 deletions os/src/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,34 +171,31 @@ object BasePath {
def checkSegment(s: String) = {
def fail(msg: String) = throw PathError.InvalidSegment(s, msg)
def considerStr =
"use the Path(...) or RelPath(...) constructor calls to convert them. "
"If you are dealing with dynamic path-strings coming from external sources, " +
"use the Path(...)/RelPath(...)/SubPath(...) constructor calls to convert them."

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

}
def externalStr = "If you are dealing with path-strings coming from external sources, "
s match {
case "" =>
fail(
"OS-Lib does not allow empty path segments " +
externalStr + considerStr
"OS-Lib does not allow empty path segments. " +
considerStr
)
case "." =>
fail(
"OS-Lib does not allow [.] as a path segment " +
externalStr + considerStr
"OS-Lib does not allow [.] in a non-literal path segment. " +
considerStr
)
case ".." =>
fail(
"OS-Lib does not allow [..] as a path segment " +
externalStr +
"OS-Lib does not allow [..] in a non-literal path segment. " +
considerStr +
"If you want to use the `..` segment manually to represent going up " +
"one level in the path, use the `up` segment from `os.up` " +
Expand Down
8 changes: 7 additions & 1 deletion os/test/src/PathTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,13 @@ object PathTests extends TestSuite {

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

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

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

Expand Down