Last active
May 27, 2023 06:29
-
-
Save dacr/c255b710ef97e25201f113618e84f99a to your computer and use it in GitHub Desktop.
Using scala betterfiles API to simplify operations with files. / published by https://github.com/dacr/code-examples-manager #df768884-8296-467e-944d-525fafc6ce96/3c3de22654bc0e0b7d04ab52dec316e414f5bba6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : Using scala betterfiles API to simplify operations with files. | |
// keywords : scala, betterfiles, file, io | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : df768884-8296-467e-944d-525fafc6ce96 | |
// created-on : 2020-05-31T19:54:52Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.3.0" | |
//> using dep "org.scalatest::scalatest:3.2.16" | |
//> using dep "com.github.pathikrit::better-files:3.9.2" | |
//> using objectWrapper | |
// --------------------- | |
import org.scalatest._, wordspec._, matchers._, OptionValues._ | |
import better.files._ | |
import better.files.Dsl._ | |
class BetterFilesRecipesTest extends AnyWordSpec with should.Matchers { | |
override def suiteName: String = "BetterFilesRecipesTest" | |
"list files" can { | |
"standard way" should { | |
"list files in a given directory" in { | |
pwd.list.map(_.name).toList should contain allOf("file-path-tools.sc", "filesio-betterfiles-basics.sc", "filesio-default-basics.sc") | |
} | |
"filter files extensions" in { | |
pwd.list.filter(_.name.endsWith(".sc")).map(_.name).toList should contain allOf("file-path-tools.sc", "filesio-betterfiles-basics.sc", "filesio-default-basics.sc") | |
} | |
} | |
"globbing" should { | |
"find and filter files in the given directory" in { | |
pwd.glob("*.sc").size should be > 0 | |
} | |
"find and filter files all files in the given hierarchy" in { | |
pwd.parent.glob("http/**/*.md").map(_.name).toList should contain allElementsOf List("index.md") | |
} | |
"should find more files in the hierarchy than in the given directory" in { | |
val curDir = pwd.glob("*.sc", includePath = false).toList // in current dir only | |
val subDir = pwd.glob("**/*.sc", includePath = false).toList // in all the hierarchy | |
val inTree = pwd.glob("*.sc", includePath = true).toList // default - in all the hierarchy | |
val inTree2 = pwd.glob("**/*.sc", includePath = true).toList // in the hierarchy but not the current dir | |
inTree.size should be > curDir.size | |
inTree.map(_.name).sorted should contain allOf("file-path-tools.sc", "filesio-betterfiles-basics.sc", "filesio-default-basics.sc") | |
inTree2.map(_.name).sorted should contain noneOf("file-path-tools.sc", "filesio-betterfiles-basics.sc", "filesio-default-basics.sc") | |
} | |
} | |
} | |
"file IO" can { | |
"read file" should { | |
"be very easy" in { | |
val content = (pwd / "filesio-betterfiles-basics.sc").contentAsString | |
content should include regex "be very easy" | |
} | |
} | |
"write file" should { | |
"be also very easy" in { | |
val newContent = | |
"""# Some new title | |
|with some content | |
|""".stripMargin | |
val tmpFile = File.newTemporaryFile() | |
tmpFile.overwrite(newContent) | |
//import better.files.Dsl.SymbolicOperations | |
//tmpFile < newContent // alternative | |
tmpFile.contentAsString shouldBe newContent | |
} | |
} | |
} | |
} | |
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[BetterFilesRecipesTest].getName)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment