Skip to content

Commit

Permalink
Merge pull request #88 from Hombre-x/readlines-change
Browse files Browse the repository at this point in the history
Change `readLines` behaviour when reading an empty last newline
  • Loading branch information
TonioGela authored Aug 24, 2024
2 parents dec226e + cd4cb57 commit de57787
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
9 changes: 7 additions & 2 deletions core/src/main/scala/io/chrisdavenport/shellfish/FilesOs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,20 @@ object FilesOs {

/**
* Reads the contents of the file at the path using UTF-8 decoding and returns
* it line by line as a List of Strings.
* it line by line as a List of Strings. It will ignore any empty characters
* after the last newline (similar to `wc -l`).
*
* @param path
* The path to read from
* @return
* The file loaded in memory as a collection of lines of Strings
*/
def readLines(path: Path): IO[List[String]] =
files.readUtf8Lines(path).compile.toList
files
.readUtf8Lines(path)
.dropLastIf(_.isEmpty)
.compile
.toList

/**
* Reads the contents of the file and deserializes its contents as `A` using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ package object path {

/**
* Reads the contents of the file at the path using UTF-8 decoding and
* returns it line by line as a List of Strings.
* returns it line by line as a List of Strings. It will ignore any empty
* characters after the last newline (similar to `wc -l`).
*
* @param path
* The path to read from
Expand Down
21 changes: 19 additions & 2 deletions core/src/test/scala/io/chrisdavenport/shellfish/FileOsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ object FileOsSpec extends SimpleIOSuite with Checkers {
sizeBefore <- path.readLines.map(_.size)
_ <- path.appendLine("Im a last line!")
sizeAfter <- path.readLines.map(_.size)
} yield expect(sizeBefore + 1 == sizeAfter)
} yield expect(sizeBefore + 2 == sizeAfter)
// That is, one new line of the appendLine and, one newline character of the writeLines method
}
}
}
Expand Down Expand Up @@ -322,7 +323,7 @@ object FileOsSpec extends SimpleIOSuite with Checkers {
}
}

// Warning: Platform dependent test; this may fail on some operating systems
// Warning: Platform-dependent test; this may fail on some operating systems
test("We should be able to get the file permissions in POSIX systems") {

val permissionsGenerator: Gen[PosixPermissions] =
Expand All @@ -343,4 +344,20 @@ object FileOsSpec extends SimpleIOSuite with Checkers {
}
}

test(
"Writing lines should return a list with the same length when reading them"
) {
val contentGenerator: Gen[List[String]] =
Gen.size.flatMap(size => Gen.listOfN(size, Gen.alphaNumStr))

forall(contentGenerator) { contentsList =>
withTempFile { path =>
for {
_ <- path.writeLines(contentsList)
readlns <- path.readLines
} yield expect(contentsList.length == readlns.length)
}
}
}

}

0 comments on commit de57787

Please sign in to comment.