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

Change readLines behaviour when reading an empty last newline #88

Merged
merged 4 commits into from
Aug 24, 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
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)
}
}
}

}