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

make os.root.baseName equal to s"${os.root/}" rather than throwing an exception #277

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 8 additions & 4 deletions os/src/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,16 @@ trait BasePathImpl extends BasePath {
}

override def baseName: String = {
val li = last.lastIndexOf('.')
if (li == -1) last
else last.slice(0, li)
lastOpt match {
case None => s"${Path.driveRoot}/"
case Some(lastSegment) =>
val li = lastSegment.lastIndexOf('.')
if (li == -1) last
else lastSegment.slice(0, li)
}
}

def last: String = lastOpt.getOrElse(throw PathError.LastOnEmptyPath())
def last: String = lastOpt.getOrElse("")

def lastOpt: Option[String]
}
Expand Down
22 changes: 13 additions & 9 deletions os/test/src/PathTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ object PathTests extends TestSuite {
assert((base / "baseName.v2.0.ext").baseName == "baseName.v2.0")
assert((base / "baseOnly").baseName == "baseOnly")
assert((base / "baseOnly.").baseName == "baseOnly")
assert(os.root.baseName == s"$driveRoot/")
assert(os.Path("/").baseName == s"$driveRoot/")
}

test("ext") {
Expand All @@ -86,15 +88,17 @@ object PathTests extends TestSuite {
os.up.ext ==> ""
}

test("emptyLast") {
intercept[PathError.LastOnEmptyPath](os.root.last).getMessage ==>
"empty path has no last segment"
intercept[PathError.LastOnEmptyPath](os.rel.last).getMessage ==>
"empty path has no last segment"
intercept[PathError.LastOnEmptyPath](os.sub.last).getMessage ==>
"empty path has no last segment"
intercept[PathError.LastOnEmptyPath](os.up.last).getMessage ==>
"empty path has no last segment"
if (false) {
test("emptyLast") {
intercept[PathError.LastOnEmptyPath](os.root.last).getMessage ==>
"empty path has no last segment"
intercept[PathError.LastOnEmptyPath](os.rel.last).getMessage ==>
"empty path has no last segment"
intercept[PathError.LastOnEmptyPath](os.sub.last).getMessage ==>
"empty path has no last segment"
intercept[PathError.LastOnEmptyPath](os.up.last).getMessage ==>
"empty path has no last segment"
}
}
}

Expand Down