How to use File.shouldHaveSameStructureAndContentAs method of io.kotest.matchers.file.matchers class

Best Kotest code snippet using io.kotest.matchers.file.matchers.File.shouldHaveSameStructureAndContentAs

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

matchers.kt

Source:matchers.kt Github

copy

Full Screen

1package io.kotest.matchers.file2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.collections.shouldBeSameSizeAs5import io.kotest.matchers.paths.beSymbolicLink6import io.kotest.matchers.should7import io.kotest.matchers.shouldBe8import io.kotest.matchers.shouldNot9import io.kotest.matchers.shouldNotBe10import java.io.File11import java.io.FileFilter12import java.nio.file.Path13private fun File.safeList(): List<String> = this.list()?.toList() ?: emptyList()14private fun File.safeListFiles(): List<File> = this.listFiles()?.toList() ?: emptyList()15private fun File.safeListFiles(filter: FileFilter): List<File> = this.listFiles(filter)?.toList() ?: emptyList()16fun File.shouldBeEmptyDirectory() = this should beEmptyDirectory()17fun File.shouldNotBeEmptyDirectory() = this shouldNot beEmptyDirectory()18fun beEmptyDirectory(): Matcher<File> = object : Matcher<File> {19 override fun test(value: File): MatcherResult {20 val contents = if (value.isDirectory) value.safeList() else emptyList()21 return MatcherResult(22 contents.isEmpty(),23 { "$value should be an empty directory but contained ${contents.size} file(s) [${contents.joinToString(", ")}]" },24 { "$value should not be a non empty directory" }25 )26 }27}28infix fun File.shouldContainNFiles(n: Int) = this shouldBe containNFiles(n)29infix fun File.shouldNotContainNFiles(n: Int) = this shouldNotBe containNFiles(n)30fun containNFiles(n: Int): Matcher<File> = object : Matcher<File> {31 override fun test(value: File): MatcherResult = MatcherResult(32 value.isDirectory && value.safeList().size == n,33 { "$value should be a directory and contain $n files" },34 { "$value should not be a directory containing $n files" }35 )36}37fun File.shouldBeEmpty() = this shouldBe emptyFile()38fun File.shouldNotBeEmpty() = this shouldNotBe emptyFile()39fun emptyFile(): Matcher<File> = object : Matcher<File> {40 override fun test(value: File): MatcherResult =41 MatcherResult(42 value.length() == 0L,43 { "File $value should be empty" },44 { "File $value should not be empty" }45 )46}47fun File.shouldExist() = this should exist()48fun File.shouldNotExist() = this shouldNot exist()49fun exist() = object : Matcher<File> {50 override fun test(value: File) =51 MatcherResult(52 value.exists(),53 { "File $value should exist" },54 { "File $value should not exist" }55 )56}57infix fun File.shouldContainFile(name: String) = this should containFile(name)58infix fun File.shouldNotContainFile(name: String) = this shouldNot containFile(name)59fun containFile(name: String) = object : Matcher<File> {60 override fun test(value: File): MatcherResult {61 val contents = value.safeList()62 val passed = value.isDirectory && contents.contains(name)63 return MatcherResult(64 passed,65 { "Directory $value should contain a file with filename $name (detected ${contents.size} other files)" },66 { "Directory $value should not contain a file with filename $name" }67 )68 }69}70fun File.shouldBeSymbolicLink() = this.toPath() should beSymbolicLink()71fun File.shouldNotBeSymbolicLink() = this.toPath() shouldNot beSymbolicLink()72infix fun File.shouldHaveParent(name: String) = this should haveParent(name)73infix fun File.shouldNotHaveParent(name: String) = this shouldNot haveParent(name)74fun haveParent(name: String) = object : Matcher<File> {75 private fun isParentEqualExpected(parent: File?): Boolean =76 parent != null && (parent.name == name || isParentEqualExpected(parent.parentFile))77 override fun test(value: File) = MatcherResult(78 isParentEqualExpected(value.parentFile),79 { "File $value should have parent $name" },80 { "File $value should not have parent $name" }81 )82}83fun File.shouldBeADirectory() = this should aDirectory()84fun File.shouldNotBeADirectory() = this shouldNot aDirectory()85fun aDirectory(): Matcher<File> = object : Matcher<File> {86 override fun test(value: File): MatcherResult = MatcherResult(87 value.isDirectory,88 { "File $value should be a directory" },89 { "File $value should not be a directory" }90 )91}92fun File.shouldBeAFile() = this should aFile()93fun File.shouldNotBeAFile() = this shouldNot aFile()94fun aFile(): Matcher<File> = object : Matcher<File> {95 override fun test(value: File): MatcherResult =96 MatcherResult(97 value.isFile,98 { "File $value should be a file" },99 { "File $value should not be a file" })100}101infix fun File.shouldBeSmaller(other: Path) = this should beSmaller(other.toFile())102infix fun File.shouldBeSmaller(other: File) = this should beSmaller(other)103infix fun File.shouldNotBeSmaller(other: Path) = this shouldNot beSmaller(other.toFile())104infix fun File.shouldNotBeSmaller(other: File) = this shouldNot beSmaller(other)105fun beSmaller(other: File): Matcher<File> = object : Matcher<File> {106 override fun test(value: File): MatcherResult {107 val sizea = value.length()108 val sizeb = other.length()109 return MatcherResult(110 value.length() < other.length(),111 { "File $value ($sizea bytes) should be smaller than $other ($sizeb bytes)" },112 { "File $value ($sizea bytes) should not be smaller than $other ($sizeb bytes)" }113 )114 }115}116infix fun File.shouldBeLarger(other: Path) = this should beLarger(other.toFile())117infix fun File.shouldBeLarger(other: File) = this should beLarger(other)118infix fun File.shouldNotBeLarger(other: Path) = this shouldNot beLarger(other.toFile())119infix fun File.shouldNotBeLarger(other: File) = this shouldNot beLarger(other)120fun beLarger(other: File): Matcher<File> = object : Matcher<File> {121 override fun test(value: File): MatcherResult {122 val sizea = value.length()123 val sizeb = other.length()124 return MatcherResult(125 value.length() > other.length(),126 { "File $value ($sizea bytes) should be larger than $other ($sizeb bytes)" },127 { "File $value ($sizea bytes) should not be larger than $other ($sizeb bytes)" }128 )129 }130}131fun File.shouldBeCanonical() = this should beCanonicalPath()132fun File.shouldNotBeCanonical() = this shouldNot beCanonicalPath()133fun beCanonicalPath(): Matcher<File> = object : Matcher<File> {134 override fun test(value: File): MatcherResult = MatcherResult(135 value.canonicalPath == value.path,136 { "File $value should be canonical" },137 { "File $value should not be canonical" }138 )139}140fun File.shouldBeAbsolute() = this should beAbsolute()141fun File.shouldNotBeAbsolute() = this shouldNot beAbsolute()142fun beAbsolute(): Matcher<File> = object : Matcher<File> {143 override fun test(value: File): MatcherResult =144 MatcherResult(145 value.isAbsolute,146 { "File $value should be absolute" },147 { "File $value should not be absolute" })148}149fun File.shouldBeRelative() = this should beRelative()150fun File.shouldNotBeRelative() = this shouldNot beRelative()151fun beRelative(): Matcher<File> = object : Matcher<File> {152 override fun test(value: File): MatcherResult =153 MatcherResult(154 !value.isAbsolute,155 { "File $value should be relative" },156 { "File $value should not be relative" })157}158infix fun File.shouldHaveFileSize(size: Long) = this should haveFileSize(size)159infix fun File.shouldNotHaveFileSize(size: Long) = this shouldNot haveFileSize(size)160fun haveFileSize(size: Long): Matcher<File> = object : Matcher<File> {161 override fun test(value: File): MatcherResult = MatcherResult(162 value.length() == size,163 { "File $value should have size $size" },164 { "File $value should not have size $size" }165 )166}167fun File.shouldBeWriteable() = this should beWriteable()168fun File.shouldNotBeWriteable() = this shouldNot beWriteable()169fun beWriteable(): Matcher<File> = object : Matcher<File> {170 override fun test(value: File): MatcherResult =171 MatcherResult(172 value.canWrite(),173 { "File $value should be writeable" },174 { "File $value should not be writeable" })175}176fun File.shouldBeExecutable() = this should beExecutable()177fun File.shouldNotBeExecutable() = this shouldNot beExecutable()178fun beExecutable(): Matcher<File> = object : Matcher<File> {179 override fun test(value: File): MatcherResult = MatcherResult(180 value.canExecute(),181 { "File $value should be executable" },182 { "File $value should not be executable" }183 )184}185fun File.shouldBeHidden() = this should beHidden()186fun File.shouldNotBeHidden() = this shouldNot beHidden()187fun beHidden(): Matcher<File> = object : Matcher<File> {188 override fun test(value: File): MatcherResult =189 MatcherResult(190 value.isHidden,191 { "File $value should be hidden" },192 { "File $value should not be hidden" })193}194fun File.shouldBeReadable() = this should beReadable()195fun File.shouldNotBeReadable() = this shouldNot beReadable()196fun beReadable(): Matcher<File> = object : Matcher<File> {197 override fun test(value: File): MatcherResult =198 MatcherResult(199 value.canRead(),200 { "File $value should be readable" },201 { "File $value should not be readable" })202}203infix fun File.shouldStartWithPath(path: Path) = this should startWithPath(path)204infix fun File.shouldNotStartWithPath(path: Path) = this shouldNot startWithPath(path)205infix fun File.shouldStartWithPath(prefix: String) = this should startWithPath(prefix)206infix fun File.shouldNotStartWithPath(prefix: String) = this shouldNot startWithPath(prefix)207infix fun File.shouldStartWithPath(file: File) = this should startWithPath(file)208infix fun File.shouldNotStartWithPath(file: File) = this shouldNot startWithPath(file)209infix fun Path.shouldStartWithPath(path: Path) = this.toFile() should startWithPath(path)210infix fun Path.shouldNotStartWithPath(path: Path) = this.toFile() shouldNot startWithPath(path)211fun startWithPath(path: Path) = startWithPath(path.toFile())212fun startWithPath(file: File) = startWithPath(file.toString())213fun startWithPath(prefix: String) = object : Matcher<File> {214 override fun test(value: File): MatcherResult = MatcherResult(215 value.toString().startsWith(prefix),216 { "File $value should start with $prefix" },217 { "File $value should not start with $prefix" }218 )219}220infix fun File.shouldHaveSameStructureAs(file: File) {221 this.shouldHaveSameStructureAs(file) { _, _ -> false }222}223fun File.shouldHaveSameStructureAs(224 file: File,225 compare: (expect: File, actual: File) -> Boolean,226) {227 val expectFiles = this.walkTopDown().toList()228 val actualFiles = file.walkTopDown().toList()229 val expectParentPath = this.path230 val actualParentPath = file.path231 expectFiles shouldBeSameSizeAs actualFiles232 expectFiles.zip(actualFiles) { expect, actual ->233 when {234 compare(expect, actual) -> {}235 expect.isDirectory -> actual.shouldBeADirectory()236 expect.isFile -> {237 expect.path.removePrefix(expectParentPath)238 .shouldBe(actual.path.removePrefix(actualParentPath))239 }240 else -> error("There is an unexpected error analyzing file trees")241 }242 }243}244fun File.shouldHaveSameStructureAs(245 file: File,246 filterLhs: (File) -> Boolean = { false },247 filterRhs: (File) -> Boolean = { false },248) {249 this.shouldHaveSameStructureAs(file) { expect, actual ->250 filterLhs(expect) || filterRhs(actual)251 }252}253infix fun File.shouldHaveSameStructureAndContentAs(file: File) {254 this.shouldHaveSameStructureAndContentAs(file) { _, _ -> false }255}256fun File.shouldHaveSameStructureAndContentAs(257 file: File,258 compare: (expect: File, actual: File) -> Boolean,259) {260 val expectFiles = this.walkTopDown().toList()261 val actualFiles = file.walkTopDown().toList()262 val expectParentPath = this.path263 val actualParentPath = file.path264 expectFiles shouldBeSameSizeAs actualFiles265 expectFiles.zip(actualFiles) { expect, actual ->266 when {267 compare(expect, actual) -> {}268 expect.isDirectory -> actual.shouldBeADirectory()269 expect.isFile -> {270 expect.path.removePrefix(expectParentPath)271 .shouldBe(actual.path.removePrefix(actualParentPath))272 expect.shouldHaveSameContentAs(actual)273 }274 else -> error("There is an unexpected error analyzing file trees")275 }276 }277}278fun File.shouldHaveSameStructureAndContentAs(279 file: File,280 filterLhs: (File) -> Boolean = { false },281 filterRhs: (File) -> Boolean = { false },282) {283 this.shouldHaveSameStructureAndContentAs(file) { expect, actual ->284 filterLhs(expect) || filterRhs(actual)285 }286}...

Full Screen

Full Screen

BuildDocsTest.kt

Source:BuildDocsTest.kt Github

copy

Full Screen

1package com.javiersc.gradle.plugins.docs2import com.javiersc.gradle.plugins.core.test.arguments3import com.javiersc.gradle.plugins.core.test.getResource4import com.javiersc.gradle.plugins.core.test.taskFromArguments5import com.javiersc.gradle.plugins.core.test.testSandbox6import io.kotest.matchers.file.shouldBeADirectory7import io.kotest.matchers.file.shouldBeAFile8import io.kotest.matchers.file.shouldHaveSameStructureAndContentAs9import io.kotest.matchers.nulls.shouldNotBeNull10import io.kotest.matchers.shouldBe11import io.kotest.matchers.string.shouldContain12import java.io.File13import kotlin.test.Ignore14import kotlin.test.Test15import org.gradle.testkit.runner.BuildResult16import org.gradle.testkit.runner.TaskOutcome17class BuildDocsTest {18 @Test19 fun `build docs`() {20 val docsDir: File = getResource("docs")21 val sandboxDirs: List<File> =22 checkNotNull(docsDir.listFiles()).toList().filterNot {23 it.isFile || (it.isDirectory && it.name.endsWith("-actual"))24 }25 for (dir in sandboxDirs) {26 val sandboxPath = dir.toRelativeString(docsDir.parentFile).replace("\\", "/")27 val actualPath = "$sandboxPath-actual/.docs"28 testSandbox(29 sandboxPath = sandboxPath,30 test = { _: BuildResult, testProjectDir: File ->31 val expect = File("$testProjectDir/build/.docs/")32 val actual: File = getResource(actualPath)33 expect shouldHaveSameStructureAndContentAs actual34 File("$testProjectDir/build/.docs/").shouldBeADirectory()35 File("$testProjectDir/build/docs/").shouldBeADirectory()36 val siteDir = File("$testProjectDir/build/docs/_site/")37 File("$siteDir/index.html").shouldBeAFile()38 File("$siteDir/api/").shouldBeADirectory()39 if (sandboxPath.contains("snapshot")) {40 File("$siteDir/api/snapshot/").shouldBeADirectory()41 } else {42 File("$siteDir/api/versions/").shouldBeADirectory()43 }44 }45 )46 }47 }48 @Test49 fun `build cache docs`() {50 // to simulate IDEA sync the task is run three times51 val (runner, testProjectDir) =52 testSandbox(53 sandboxPath = "docs-gradle-features/build-cache-1",54 isBuildCacheTest = true,55 test = { result: BuildResult, testProjectDir: File ->56 result57 .task(":${testProjectDir.taskFromArguments}")58 .shouldNotBeNull()59 .outcome.shouldBe(TaskOutcome.SUCCESS)60 }61 )62 File("$testProjectDir/build").deleteRecursively()63 runner.withArguments(testProjectDir.arguments).build()64 File("$testProjectDir/build").deleteRecursively()65 val result = runner.withArguments(testProjectDir.arguments).build()66 result67 .task(":${testProjectDir.taskFromArguments}")68 .shouldNotBeNull()69 .outcome.shouldBe(TaskOutcome.FROM_CACHE)70 }71 @Ignore("MkDocs Gradle plugin needs to be compatible with Configuration cache (grgit issue)")72 @Test73 fun `configuration cache docs`() {74 val (runner, testProjectDir) =75 testSandbox(76 sandboxPath = "docs-gradle-features/configuration-cache-1",77 test = { result: BuildResult, testProjectDir: File ->78 result79 .task(":${testProjectDir.taskFromArguments}")80 .shouldNotBeNull()81 .outcome.shouldBe(TaskOutcome.SUCCESS)82 }83 )84 val result = runner.withArguments(testProjectDir.arguments + "--info").build()85 println("----------------")86 println(result.output)87 println("----------------")88 result.output.shouldContain("Reusing configuration cache")89 result90 .task(":${testProjectDir.taskFromArguments}")91 .shouldNotBeNull()92 .outcome.shouldBe(TaskOutcome.UP_TO_DATE)93 }94}...

Full Screen

Full Screen

ReadmeBadgesGeneratorTest.kt

Source:ReadmeBadgesGeneratorTest.kt Github

copy

Full Screen

1package com.javiersc.gradle.plugins.readme.badges.generator2import com.javiersc.gradle.plugins.core.test.arguments3import com.javiersc.gradle.plugins.core.test.getResource4import com.javiersc.gradle.plugins.core.test.taskFromArguments5import com.javiersc.gradle.plugins.core.test.testSandbox6import io.kotest.matchers.file.shouldHaveSameStructureAndContentAs7import io.kotest.matchers.nulls.shouldNotBeNull8import io.kotest.matchers.shouldBe9import io.kotest.matchers.string.shouldContain10import java.io.File11import kotlin.test.Test12import org.gradle.testkit.runner.BuildResult13import org.gradle.testkit.runner.TaskOutcome14class ReadmeBadgesGeneratorTest {15 @Test16 fun `build readme badges`() {17 val readmeDir: File = getResource("readme")18 val sandboxDirs: List<File> =19 checkNotNull(readmeDir.listFiles()).toList().filterNot {20 it.isFile || (it.isDirectory && it.name.endsWith("-actual"))21 }22 for (dir in sandboxDirs) {23 val sandboxPath = dir.toRelativeString(readmeDir.parentFile).replace("\\", "/")24 val actualPath = "$sandboxPath-actual/README.md"25 testSandbox(26 sandboxPath = sandboxPath,27 test = { _: BuildResult, testProjectDir: File ->28 val expect = File("$testProjectDir/README.md")29 val actual: File = getResource(actualPath)30 expect shouldHaveSameStructureAndContentAs actual31 }32 )33 }34 }35 @Test36 fun `build readme badges build cache snapshot 1`() {37 val (runner, testProjectDir) =38 testSandbox(39 sandboxPath = "gradle-features/build-cache-snapshot-1",40 isBuildCacheTest = true,41 test = { result: BuildResult, testProjectDir: File ->42 result43 .task(":${testProjectDir.taskFromArguments}")44 .shouldNotBeNull()45 .outcome.shouldBe(TaskOutcome.SUCCESS)46 }47 )48 runner.withArguments(testProjectDir.arguments).build()49 val result = runner.withArguments(testProjectDir.arguments).build()50 result51 .task(":${testProjectDir.taskFromArguments}")52 .shouldNotBeNull()53 .outcome.shouldBe(TaskOutcome.SUCCESS)54 }55 @Test56 fun `build readme badges configuration cache snapshot 1`() {57 val (runner, testProjectDir) =58 testSandbox(59 sandboxPath = "gradle-features/configuration-cache-snapshot-1",60 test = { result: BuildResult, testProjectDir: File ->61 result62 .task(":${testProjectDir.taskFromArguments}")63 .shouldNotBeNull()64 .outcome.shouldBe(TaskOutcome.SUCCESS)65 }66 )67 val result = runner.withArguments(testProjectDir.arguments + "--stacktrace").build()68 println(result.output)69 result.output.shouldContain("Reusing configuration cache")70 result.task(":${testProjectDir.taskFromArguments}")?.outcome.shouldBe(TaskOutcome.SUCCESS)71 }72}...

Full Screen

Full Screen

File.shouldHaveSameStructureAndContentAs

Using AI Code Generation

copy

Full Screen

1file1.shouldHaveSameStructureAndContentAs(file2)2file1.shouldHaveSameStructureAs(file2)3file1.shouldHaveSameTextAs(file2)4file1.shouldHaveSameTextAs(file2, charset)5file1.shouldHaveSameTextAs(file2, charset, ignoreLineEndingDifferences)6file1.shouldHaveSameTextAs(file2, charset, ignoreLineEndingDifferences, ignoreCase)7file1.shouldHaveSameTextAs(file2, charset, ignoreLineEndingDifferences, ignoreCase, ignoreExtraEmptyLines)8file1.shouldHaveSameTextAs(file2, charset, ignoreLineEndingDifferences, ignoreCase, ignoreExtraEmptyLines, normalizeLineEndings)9file1.shouldHaveSameTextAs(file2, charset, ignoreLineEndingDifferences, ignoreCase, ignoreExtraEmptyLines, normalizeLineEndings, ignoreWhitespace)10file1.shouldHaveSameTextAs(file2, charset, ignoreLineEndingDifferences, ignoreCase, ignoreExtraEmptyLines, normalizeLineEndings, ignoreWhitespace, ignoreBlankLines)11file1.shouldHaveSameTextAs(file2, charset, ignoreLineEndingDifferences, ignoreCase, ignoreExtraEmptyLines, normalizeLineEndings, ignoreWhitespace, ignoreBlankLines, ignoreComments)

Full Screen

Full Screen

File.shouldHaveSameStructureAndContentAs

Using AI Code Generation

copy

Full Screen

1val file1 = File("src/test/resources/file1.txt")2val file2 = File("src/test/resources/file2.txt")3file1.shouldHaveSameStructureAndContentAs(file2)4file1.shouldNotHaveSameStructureAndContentAs(file2)5file1.shouldHaveSameStructureAndContentAs(file2)6file1.shouldNotHaveSameStructureAndContentAs(file2)7file1.shouldHaveSameStructureAndContentAs(file2)8file1.shouldNotHaveSameStructureAndContentAs(file2)9file1.shouldHaveSameStructureAndContentAs(file2)10file1.shouldNotHaveSameStructureAndContentAs(file2)11file1.shouldHaveSameStructureAndContentAs(file2)12file1.shouldNotHaveSameStructureAndContentAs(file2)13file1.shouldHaveSameStructureAndContentAs(file2)14file1.shouldNotHaveSameStructureAndContentAs(file2)15file1.shouldHaveSameStructureAndContentAs(file2)16file1.shouldNotHaveSameStructureAndContentAs(file2)17file1.shouldHaveSameStructureAndContentAs(file2)18file1.shouldNotHaveSameStructureAndContentAs(file2)19file1.shouldHaveSameStructureAndContentAs(file2)20file1.shouldNotHaveSameStructureAndContentAs(file2)21file1.shouldHaveSameStructureAndContentAs(file2)

Full Screen

Full Screen

File.shouldHaveSameStructureAndContentAs

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.file.matchers.*2val file1 = File("file1.txt")3val file2 = File("file2.txt")4file1.shouldHaveSameStructureAndContentAs(file2)5import io.kotest.matchers.file.matchers.*6val file1 = File("file1.txt")7val file2 = File("file2.txt")8file1.shouldHaveSameStructureAs(file2)9import io.kotest.matchers.file.matchers.*10val file1 = File("file1.txt")11val file2 = File("file2.txt")12file1.shouldHaveSameContentAs(file2)13import io.kotest.matchers.file.matchers.*14val file1 = File("file1.txt")15val file2 = File("file2.txt")16file1.shouldHaveSameTextualContentAs(file2)17import io.kotest.matchers.file.matchers.*18val file1 = File("file1.txt")19val file2 = File("file2.txt")20file1.shouldHaveSameNameAs(file2)21import io.kotest.matchers.file.matchers.*22val file1 = File("file1.txt")23val file2 = File("file2.txt")24file1.shouldHaveSameNameIgnoringCaseAs(file2)25import io.kotest.matchers.file.matchers.*26val file1 = File("file1.txt")27val file2 = File("file2.txt")28file1.shouldHaveSameNameIgnoringCaseAs(file2)29import io.kotest.matchers.file.matchers.*30val file = File("file.txt")31file.shouldBeADirectory()

Full Screen

Full Screen

File.shouldHaveSameStructureAndContentAs

Using AI Code Generation

copy

Full Screen

1File("src/test/resources/expected.txt").shouldHaveSameStructureAndContentAs(File("src/test/resources/actual.txt"))2File("src/test/resources/expected.txt").shouldHaveSameStructureAndContentAs(File("src/test/resources/actual.txt"), "UTF-8")3File("src/test/resources/expected.txt").shouldHaveSameStructureAndContentAs(File("src/test/resources/actual.txt"), "UTF-8", 1000)4File("src/test/resources/expected.txt").shouldHaveSameStructureAndContentAs(File("src/test/resources/actual.txt"), 1000)5File("src/test/resources/expected.txt").shouldHaveSameStructureAs(File("src/test/resources/actual.txt"))6File("src/test/resources/expected.txt").shouldHaveSameStructureAs(File("src/test/resources/actual.txt"), 1000)7File("src/test/resources/expected.txt").shouldHaveSameStructureAndContentAs(File("src/test/resources/actual.txt"))8File("src/test/resources/expected.txt").shouldHaveSameStructureAndContentAs(File("src/test/resources/actual.txt"), 1000)9File("src/test/resources/expected.txt").shouldHaveSameStructureAndContentAs(File("src/test/resources/actual.txt"), "UTF-8")

Full Screen

Full Screen

File.shouldHaveSameStructureAndContentAs

Using AI Code Generation

copy

Full Screen

1 File("/path/to/file").shouldHaveSameStructureAndContentAs(File("/path/to/another/file"))2 File("/path/to/file").shouldHaveSameStructureAs(File("/path/to/another/file"))3 File("/path/to/file").shouldHaveSameTextAs(File("/path/to/another/file"))4 File("/path/to/file").shouldHaveSameTextAs("content")5 File("/path/to/file").shouldHaveText("content")6 File("/path/to/file").shouldHaveTextContaining("content")7 File("/path/to/file").shouldHaveTextMatching(Regex("content"))8 File("/path/to/file").shouldHaveTextStartingWith("content")9 File("/path/to/file").shouldNotBeADirectory()10 File("/path/to/file").shouldNotBeAFile()11 File("/path/to/file").shouldNotBeAbsolute()12 File("/path/to/file").shouldNotBeHidden()13 File("/path/to/file").shouldNotBeRelative()

Full Screen

Full Screen

File.shouldHaveSameStructureAndContentAs

Using AI Code Generation

copy

Full Screen

1val file1 = File("test.txt")2file1.writeText("Hello")3val file2 = File("test.txt")4file1.shouldHaveSameStructureAndContentAs(file2)5val file1 = File("test.txt")6file1.writeText("Hello")7val file2 = File("test.txt")8file1.shouldHaveSameStructureAndContentAs(file2)9val file1 = File("test.txt")10file1.writeText("Hello")11val file2 = File("test.txt")12file1.shouldHaveSameStructureAndContentAs(file2)13val file1 = File("test.txt")14file1.writeText("Hello")15val file2 = File("test.txt")16file1.shouldHaveSameStructureAndContentAs(file2)17val file1 = File("test.txt")18file1.writeText("Hello")19val file2 = File("test.txt")20file1.shouldHaveSameStructureAndContentAs(file2)21val file1 = File("test.txt")22file1.writeText("Hello")23val file2 = File("test.txt")24file1.shouldHaveSameStructureAndContentAs(file2)25val file1 = File("test.txt")26file1.writeText("Hello")27val file2 = File("test.txt")28file1.shouldHaveSameStructureAndContentAs(file2)29val file1 = File("test.txt")30file1.writeText("Hello")31val file2 = File("test.txt")32file1.shouldHaveSameStructureAndContentAs(file2)

Full Screen

Full Screen

File.shouldHaveSameStructureAndContentAs

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.file.shouldHaveSameStructureAndContentAs2import io.kotest.core.spec.style.AnnotationSpec3import java.io.File4class FileTest : AnnotationSpec() {5fun testFile() {6File("src/test/resources/file1.txt").shouldHaveSameStructureAndContentAs(7File("src/test/resources/file2.txt")8}9}10import io.kotest.assertions.file.shouldHaveSameTextAs11import io.kotest.core.spec.style.AnnotationSpec12import java.io.File13class FileTest : AnnotationSpec() {14fun testFile() {15File("src/test/resources/file1.txt").shouldHaveSameTextAs(16File("src/test/resources/file2.txt")17}18}19import io.kotest.assertions.file.shouldHaveSameTextAsIgnoringLineEndings20import io.kotest.core.spec.style.AnnotationSpec21import java.io.File22class FileTest : AnnotationSpec() {23fun testFile() {24File("src/test/resources/file1.txt").shouldHaveSameTextAsIgnoringLineEndings(25File("src/test/resources/file2.txt")26}27}28import io.kotest.assertions.file.shouldHaveSameTextAsIgnoringLineEndings29import io.kotest.core.spec.style.AnnotationSpec30import java.io.File31class FileTest : AnnotationSpec() {32fun testFile() {33File("src/test/resources/file1.txt").shouldHaveSameTextAsIgnoringLineEndings(34File("src/test/resources/file2.txt")35}36}37import io.kotest.assertions.file.shouldHaveSameTextAsIgnoringLineEndings38import io.kotest.core.spec.style.AnnotationSpec39import java.io.File

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful