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 .ext on empty paths return "" rather than crashing with a NPE #87

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion os/src-jvm/ResourcePath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ResourcePath private[os](val resRoot: ResourceRoot, segments0: Array[Strin
def toSource = new Source.WritableSource(getInputStream)
val segments: IndexedSeq[String] = segments0
type ThisType = ResourcePath
def last = segments0.last
def lastOpt = segments0.lastOption
override def toString = resRoot.errorName + "/" + segments0.mkString("/")
protected[this] def make(p: Seq[String], ups: Int) = {
if (ups > 0){
Expand Down
21 changes: 14 additions & 7 deletions os/src/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,14 @@ trait BasePathImpl extends BasePath{
def /(chunk: PathChunk): ThisType

def ext = {
val li = last.lastIndexOf('.')
if (li == -1) ""
else last.slice(li+1, last.length)
lastOpt match{
case None => ""
case Some(lastSegment) =>
val li = lastSegment.lastIndexOf('.')
if (li == -1) ""
else last.slice(li+1, last.length)
}

}

override def baseName: String = {
Expand All @@ -193,7 +198,9 @@ trait BasePathImpl extends BasePath{
else last.slice(0, li)
}

def last: String
def last: String = lastOpt.getOrElse("empty path has no last segment")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to throw an exception here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah good catch. I'll add a test to cover this error behavior as well


def lastOpt: Option[String]
}

object PathError{
Expand Down Expand Up @@ -239,7 +246,7 @@ object FilePath {
*/
class RelPath private[os](segments0: Array[String], val ups: Int)
extends FilePath with BasePathImpl with SegmentedPath {
def last = segments.last
def lastOpt = segments.lastOption
val segments: IndexedSeq[String] = segments0
type ThisType = RelPath
require(ups >= 0)
Expand Down Expand Up @@ -305,7 +312,7 @@ object RelPath {
*/
class SubPath private[os](val segments0: Array[String])
extends FilePath with BasePathImpl with SegmentedPath {
def last = segments.last
def lastOpt = segments.lastOption
val segments: IndexedSeq[String] = segments0
type ThisType = SubPath
protected[this] def make(p: Seq[String], ups: Int) = {
Expand Down Expand Up @@ -433,7 +440,7 @@ class Path private[os](val wrapped: java.nio.file.Path)
def segmentCount = wrapped.getNameCount
type ThisType = Path

def last = wrapped.getFileName.toString
def lastOpt = Option(wrapped.getFileName).map(_.toString)

def /(chunk: PathChunk): ThisType = {
if (chunk.ups > wrapped.getNameCount) throw PathError.AbsolutePathOutsideRoot
Expand Down
7 changes: 7 additions & 0 deletions os/test/src/PathTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ object PathTests extends TestSuite{
assert((base / "baseOnly").ext == "")
assert((base / "baseOnly.").ext == "")
}

test("emptyExt"){
os.root.ext ==> ""
os.rel.ext ==> ""
os.sub.ext ==> ""
os.up.ext ==> ""
}
}

test("RelPath"){
Expand Down