How to use File.createWithContent method of com.sksamuel.kotest.matchers.file.FileMatchersTest class

Best Kotest code snippet using com.sksamuel.kotest.matchers.file.FileMatchersTest.File.createWithContent

FileMatchersTest.kt

Source:FileMatchersTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.file2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.file.aDirectory5import io.kotest.matchers.file.aFile6import io.kotest.matchers.file.beAbsolute7import io.kotest.matchers.file.beRelative8import io.kotest.matchers.file.exist9import io.kotest.matchers.file.haveExtension10import io.kotest.matchers.file.shouldBeADirectory11import io.kotest.matchers.file.shouldBeAFile12import io.kotest.matchers.file.shouldBeAbsolute13import io.kotest.matchers.file.shouldBeEmptyDirectory14import io.kotest.matchers.file.shouldBeRelative15import io.kotest.matchers.file.shouldBeSymbolicLink16import io.kotest.matchers.file.shouldExist17import io.kotest.matchers.file.shouldHaveExtension18import io.kotest.matchers.file.shouldHaveParent19import io.kotest.matchers.file.shouldHaveSameStructureAs20import io.kotest.matchers.file.shouldHaveSameStructureAndContentAs21import io.kotest.matchers.file.shouldNotBeADirectory22import io.kotest.matchers.file.shouldNotBeAFile23import io.kotest.matchers.file.shouldNotBeEmptyDirectory24import io.kotest.matchers.file.shouldNotBeSymbolicLink25import io.kotest.matchers.file.shouldNotExist26import io.kotest.matchers.file.shouldNotHaveExtension27import io.kotest.matchers.file.shouldNotHaveParent28import io.kotest.matchers.file.shouldStartWithPath29import io.kotest.matchers.file.startWithPath30import io.kotest.matchers.paths.shouldBeLarger31import io.kotest.matchers.paths.shouldBeSmaller32import io.kotest.matchers.paths.shouldBeSymbolicLink33import io.kotest.matchers.paths.shouldContainFile34import io.kotest.matchers.paths.shouldContainFileDeep35import io.kotest.matchers.paths.shouldContainFiles36import io.kotest.matchers.paths.shouldHaveParent37import io.kotest.matchers.paths.shouldNotBeSymbolicLink38import io.kotest.matchers.paths.shouldNotContainFile39import io.kotest.matchers.paths.shouldNotContainFileDeep40import io.kotest.matchers.paths.shouldNotContainFiles41import io.kotest.matchers.paths.shouldNotHaveParent42import io.kotest.matchers.should43import io.kotest.matchers.shouldBe44import io.kotest.matchers.shouldNot45import io.kotest.matchers.string.shouldEndWith46import io.kotest.matchers.string.shouldMatch47import java.io.File48import java.nio.file.Files49import java.nio.file.Paths50import org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS51@Suppress("BlockingMethodInNonBlockingContext")52class FileMatchersTest : FunSpec() {53 init {54 test("relative() should match only relative files") {55 File("sammy/boy") shouldBe beRelative()56 File("sammy/boy").shouldBeRelative()57 }58 test("absolute() should match only absolute files") {59 val root = if (IS_OS_WINDOWS) "C:/" else "/"60 File("${root}sammy/boy") shouldBe beAbsolute()61 File("${root}sammy/boy").shouldBeAbsolute()62 }63 test("startWithPath() should only match files that start with the given path") {64 File("sammy/boy") should startWithPath("sammy")65 File("sammy/boy") should startWithPath(Paths.get("sammy"))66 File("/sammy/boy") should startWithPath("${File.separator}sammy")67 File("/sammy/boy") should startWithPath(Paths.get("/sammy"))68 File("/sammy/boy").shouldStartWithPath("${File.separator}sammy")69 File("/sammy/boy").shouldStartWithPath(Paths.get("/sammy"))70 }71 test("exist() file matcher") {72 val file = Files.createTempFile("test", "test").toFile()73 file should exist()74 shouldThrow<AssertionError> {75 File("qweqwewqewqewee") should exist()76 }.message shouldBe "File qweqwewqewqewee should exist"77 file.shouldExist()78 shouldThrow<AssertionError> {79 file.shouldNotExist()80 }81 file.delete()82 }83 test("haveExtension") {84 val file = Files.createTempFile("test", ".test").toFile()85 file should haveExtension(".test")86 file shouldNot haveExtension(".wibble")87 shouldThrow<AssertionError> {88 file should haveExtension(".jpeg")89 }.message.shouldEndWith("with one of .jpeg")90 shouldThrow<AssertionError> {91 file.shouldHaveExtension(".jpeg")92 }.message.shouldEndWith("with one of .jpeg")93 file.shouldHaveExtension(".test")94 file.shouldNotHaveExtension(".wibble")95 file.delete()96 }97 test("aFile() file matcher") {98 val file = Files.createTempFile("test", "test").toFile()99 file shouldBe aFile()100 file.shouldBeAFile()101 shouldThrow<AssertionError> {102 file shouldBe aDirectory()103 }104 shouldThrow<AssertionError> {105 file.shouldNotBeAFile()106 }107 shouldThrow<AssertionError> {108 file.shouldBeADirectory()109 }110 file.delete()111 }112 test("aDirectory() file matcher") {113 val dir = Files.createTempDirectory("testdir").toFile()114 dir shouldBe aDirectory()115 dir.shouldBeADirectory()116 shouldThrow<AssertionError> {117 dir shouldBe aFile()118 }119 shouldThrow<AssertionError> {120 dir.shouldNotBeADirectory()121 }122 shouldThrow<AssertionError> {123 dir shouldBe aFile()124 }125 }126 test("directory should be empty (deprecated)") {127 val dir = Files.createTempDirectory("testdir").toFile()128 dir.shouldBeEmptyDirectory()129 dir.resolve("testfile.txt").writeBytes(byteArrayOf(1, 2, 3))130 dir.shouldNotBeEmptyDirectory()131 }132 test("directory should be empty") {133 val dir = Files.createTempDirectory("testdir").toFile()134 dir.shouldBeEmptyDirectory()135 dir.resolve("testfile.txt").writeBytes(byteArrayOf(1, 2, 3))136 dir.shouldNotBeEmptyDirectory()137 }138 test("directory contains file matching predicate") {139 val dir = Files.createTempDirectory("testdir")140 dir.resolve("a").toFile().createNewFile()141 dir.resolve("b").toFile().createNewFile()142 dir.shouldContainFile("a")143 dir.shouldNotContainFile("c")144 shouldThrow<AssertionError> {145 dir.shouldContainFile("c")146 }.message?.shouldMatch("^Directory .+ should contain a file with filename c \\(detected 2 other files\\)$".toRegex())147 }148 test("beSmaller should compare file sizes") {149 val dir = Files.createTempDirectory("testdir")150 Files.write(dir.resolve("a"), byteArrayOf(1, 2))151 Files.write(dir.resolve("b"), byteArrayOf(1, 2, 3))152 dir.resolve("a").shouldBeSmaller(dir.resolve("b"))153 shouldThrow<AssertionError> {154 dir.resolve("b").shouldBeSmaller(dir.resolve("a"))155 }.message shouldBe "Path ${dir.resolve("b")} (3 bytes) should be smaller than ${dir.resolve("a")} (2 bytes)"156 }157 test("beLarger should compare file sizes") {158 val dir = Files.createTempDirectory("testdir")159 Files.write(dir.resolve("a"), byteArrayOf(1, 2, 3))160 Files.write(dir.resolve("b"), byteArrayOf(1, 2))161 dir.resolve("a").shouldBeLarger(dir.resolve("b"))162 shouldThrow<AssertionError> {163 dir.resolve("b").shouldBeLarger(dir.resolve("a"))164 }.message shouldBe "File ${dir.resolve("b")} (2 bytes) should be larger than ${dir.resolve("a")} (3 bytes)"165 }166 test("containsFileDeep should find file deep") {167 val rootFileName = "super_dooper_hyper_file_root"168 val innerFileName = "super_dooper_hyper_file_inner"169 val nonExistentFileName = "super_dooper_hyper_non_existent_file"170 val rootDir = Files.createTempDirectory("testdir")171 val innerDir = Files.createDirectories(rootDir.resolve("innerfolder"))172 Files.write(rootDir.resolve(rootFileName), byteArrayOf(1, 2, 3))173 Files.write(innerDir.resolve(innerFileName), byteArrayOf(1, 2, 3))174 rootDir.shouldContainFileDeep(rootFileName)175 rootDir.shouldContainFileDeep(innerFileName)176 shouldThrow<AssertionError> {177 rootDir.shouldContainFileDeep(nonExistentFileName)178 }.message shouldBe "Path $nonExistentFileName should exist in $rootDir"179 shouldThrow<AssertionError> {180 rootDir.shouldNotContainFileDeep(rootFileName)181 }.message shouldBe "Path $rootFileName should not exist in $rootDir"182 }183 test("shouldContainFiles should check if files exists") {184 val testDir = Files.createTempDirectory("testdir")185 Files.write(testDir.resolve("a.txt"), byteArrayOf(1, 2, 3))186 Files.write(testDir.resolve("b.gif"), byteArrayOf(1, 2, 3))187 Files.write(testDir.resolve("c.doc"), byteArrayOf(1, 2, 3))188 testDir.shouldContainFiles("a.txt", "b.gif", "c.doc")189 testDir.shouldNotContainFiles("d.txt", "e.gif", "f.doc")190 shouldThrow<AssertionError> {191 testDir.shouldContainFiles("d.txt")192 }.message shouldBe "File d.txt should exist in $testDir"193 shouldThrow<AssertionError> {194 testDir.shouldContainFiles("d.txt", "e.gif")195 }.message shouldBe "Files d.txt, e.gif should exist in $testDir"196 shouldThrow<AssertionError> {197 testDir.shouldNotContainFiles("a.txt")198 }.message shouldBe "File a.txt should not exist in $testDir"199 shouldThrow<AssertionError> {200 testDir.shouldNotContainFiles("a.txt", "b.gif")201 }.message shouldBe "Files a.txt, b.gif should not exist in $testDir"202 }203 test("shouldBeSymbolicLink should check if file is symbolic link").config(enabled = isNotWindowsOrIsWindowsElevated()) {204 val testDir = Files.createTempDirectory("testdir")205 val existingFile = Files.write(testDir.resolve("original.txt"), byteArrayOf(1, 2, 3, 4))206 val existingFileAsFile = existingFile.toFile()207 val link = Files.createSymbolicLink(testDir.resolve("a.txt"), existingFile)208 val linkAsFile = link.toFile()209 link.shouldBeSymbolicLink()210 linkAsFile.shouldBeSymbolicLink()211 existingFile.shouldNotBeSymbolicLink()212 existingFileAsFile.shouldNotBeSymbolicLink()213 }214 test("shouldHaveParent should check if file has any parent with given name") {215 val testDir = Files.createTempDirectory("testdir")216 val subdir = Files.createDirectory(testDir.resolve("sub_testdir"))217 val file = Files.write(subdir.resolve("a.txt"), byteArrayOf(1, 2, 3, 4))218 val fileAsFile = file.toFile()219 file.shouldHaveParent(testDir.toFile().name)220 file.shouldHaveParent(subdir.toFile().name)221 file.shouldNotHaveParent("super_hyper_long_random_file_name")222 fileAsFile.shouldHaveParent(testDir.toFile().name)223 fileAsFile.shouldHaveParent(subdir.toFile().name)224 fileAsFile.shouldNotHaveParent("super_hyper_long_random_file_name")225 }226 test("shouldHaveSameStructureAs and shouldHaveSameStructureAndContentAs two file trees") {227 val testDir = Files.createTempDirectory("testdir")228 val expectDir = File("$testDir/expect").apply {229 File("$this/a.txt").createWithContent(byteArrayOf(1, 2, 3))230 File("$this/b.txt").createWithContent(byteArrayOf(1, 2, 3, 4))231 File("$this/subfolder/b.txt").createWithContent(byteArrayOf(1, 2, 3, 4))232 File("$this/subfolder/subfolder-two/c.txt").createWithContent(byteArrayOf(1, 2))233 }234 val actualDir = File("$testDir/actual").apply {235 File("$this/a.txt").createWithContent(byteArrayOf(1, 2, 3))236 File("$this/b.txt").createWithContent(byteArrayOf(1, 2, 3, 4))237 File("$this/subfolder/b.txt").createWithContent(byteArrayOf(1, 2, 3, 4))238 File("$this/subfolder/subfolder-two/c.txt").createWithContent(byteArrayOf(1, 2))239 }240 expectDir shouldHaveSameStructureAs actualDir241 expectDir shouldHaveSameStructureAndContentAs actualDir242 File("$expectDir/z.txt").createWithContent(byteArrayOf(1, 2, 3))243 shouldThrow<AssertionError> { expectDir shouldHaveSameStructureAs actualDir }244 shouldThrow<AssertionError> { expectDir shouldHaveSameStructureAndContentAs actualDir }245 File("$actualDir/z.txt").createWithContent(byteArrayOf(1, 2, 3, 4))246 expectDir shouldHaveSameStructureAs actualDir247 shouldThrow<AssertionError> { expectDir shouldHaveSameStructureAndContentAs actualDir }248 }249 test("shouldHaveSameStructureAs with filter should check if two file trees are the same and files have the same content") {250 val testDir = Files.createTempDirectory("testdir")251 val expectDir = File("$testDir/expect").apply {252 File("$this/a.txt").createWithContent("a/b")253 File("$this/b.txt").createWithContent("b/c")254 File("$this/subfolder/b.txt").createWithContent("b/c")255 File("$this/subfolder/subfolder-two/c.txt").createWithContent("c/d")256 File("$this/z.txt").createWithContent("z")257 }258 val actualDir = File("$testDir/actual").apply {259 File("$this/a.txt").createWithContent("a/b")260 File("$this/b.txt").createWithContent("b/c")261 File("$this/subfolder/b.txt").createWithContent("b/c")262 File("$this/subfolder/subfolder-two/c.txt").createWithContent("c/d")263 File("$this/z.txt").createWithContent("zz")264 }265 expectDir.shouldHaveSameStructureAs(actualDir, filterLhs = { it.name == "z.txt" })266 expectDir.shouldHaveSameStructureAs(actualDir, filterRhs = { it.name == "z.txt" })267 }268 test("shouldHaveSameStructureAndContentAs with compare and filter should check if two file trees are the same and files have the same content") {269 val testDir = Files.createTempDirectory("testdir")270 val expectDir = File("$testDir/expect").apply {271 File("$this/a.txt").createWithContent("a/b")272 File("$this/b.txt").createWithContent("b/c")273 File("$this/subfolder/b.txt").createWithContent("b/c")274 File("$this/subfolder/subfolder-two/c.txt").createWithContent("c/d")275 }276 val actualDir = File("$testDir/actual").apply {277 File("$this/a.txt").createWithContent("a/b")278 File("$this/b.txt").createWithContent("b\\c")279 File("$this/subfolder/b.txt").createWithContent("b\\c")280 File("$this/subfolder/subfolder-two/c.txt").createWithContent("c\\d")281 }282 expectDir.shouldHaveSameStructureAs(actualDir) { a, b ->283 a.isFile && b.isFile && a.readText() == b.readText().replace("\\", "/")284 }285 expectDir.shouldHaveSameStructureAndContentAs(actualDir, filterLhs = { it.name != "a.txt" })286 expectDir.shouldHaveSameStructureAndContentAs(actualDir, filterRhs = { it.name != "a.txt" })287 }288 }289}290private fun File.createWithContent(content: String) {291 this.parentFile.mkdirs()292 createNewFile()293 writeText(content)294}295private fun File.createWithContent(content: ByteArray) {296 this.parentFile.mkdirs()297 createNewFile()298 writeBytes(content)299}300private fun isNotWindowsOrIsWindowsElevated(): Boolean {301 return if (!IS_OS_WINDOWS) {302 true303 } else {304 try {305 val p = Runtime.getRuntime().exec("""reg query "HKU\S-1-5-19"""")306 p.waitFor()307 0 == p.exitValue()308 } catch (ex: Exception) {309 println("Failed to determine if process had elevated permissions, assuming it does not.")310 false311 }312 }313}...

Full Screen

Full Screen

File.createWithContent

Using AI Code Generation

copy

Full Screen

1File.createWithContent("test.txt", "Hello World")2File("test.txt").shouldBeDirectory()3File("test.txt").shouldBeEmpty()4File("test.txt").shouldBeFile()5File("test.txt").shouldBeReadable()6File("test.txt").shouldBeRelative()7File("test.txt").shouldBeSameFileAs(File("test.txt"))8File("test.txt").shouldBeSymbolicLink()9File("test.txt").shouldBeWritable()10File("test.txt").shouldBeZero()11File("test.txt").shouldContain("Hello World")12File("test.txt").shouldContainBytes(byteArrayOf(1, 2, 3))13File("test.txt").shouldContainOnlyDigits()14File("test.txt").shouldContainOnlyWhitespaces()

Full Screen

Full Screen

File.createWithContent

Using AI Code Generation

copy

Full Screen

1File.createWithContent( "myFile.txt" , "content" )2File( "myFile.txt" ).shouldBeEmpty()3File( "myFile.txt" ).shouldBeFile()4File( "myFile.txt" ).shouldBeRelative()5File( "myFile.txt" ).shouldBeRelative()6File( "myFile.txt" ).shouldBeSymbolicLink()7File( "myFile.txt" ).shouldBeSymlink()8File( "myFile.txt" ).shouldBeZero()9File( "myFile.txt" ).shouldContain( "content" )10File( "myFile.txt" ).shouldContain( "content" )11File( "myFile.txt" ).shouldContain( "content" )12File( "myFile.txt" ).shouldContain( "content" )13File( "myFile.txt" ).shouldContain( "content" )14File( "myFile.txt

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in FileMatchersTest

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful