|
| 1 | +package com.bazel_diff.hash |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.isEqualTo |
| 5 | +import assertk.assertions.isNull |
| 6 | +import com.bazel_diff.bazel.BazelSourceFileTarget |
| 7 | +import com.bazel_diff.extensions.toHexString |
| 8 | +import com.bazel_diff.testModule |
| 9 | +import kotlinx.coroutines.runBlocking |
| 10 | +import org.junit.Rule |
| 11 | +import org.junit.Test |
| 12 | +import org.koin.test.KoinTest |
| 13 | +import org.koin.test.KoinTestRule |
| 14 | +import java.nio.file.Files |
| 15 | +import java.nio.file.Paths |
| 16 | + |
| 17 | + |
| 18 | +internal class SourceFileHasherTest: KoinTest { |
| 19 | + private val repoAbsolutePath = Paths.get("").toAbsolutePath() |
| 20 | + private val fixtureFileTarget = "//cli/src/test/kotlin/com/bazel_diff/hash/fixture:foo.ts" |
| 21 | + private val fixtureFileContent: ByteArray |
| 22 | + private val seed = "seed".toByteArray() |
| 23 | + |
| 24 | + init { |
| 25 | + val path = Paths.get("cli/src/test/kotlin/com/bazel_diff/hash/fixture/foo.ts") |
| 26 | + fixtureFileContent = Files.readAllBytes(path) |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + @get:Rule |
| 31 | + val koinTestRule = KoinTestRule.create { |
| 32 | + modules(testModule()) |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + fun testHashConcreteFile() = runBlocking { |
| 37 | + val hasher = SourceFileHasher(repoAbsolutePath, null) |
| 38 | + val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) |
| 39 | + val actual = hasher.digest(bazelSourceFileTarget).toHexString() |
| 40 | + val expected = sha256 { |
| 41 | + safePutBytes(fixtureFileContent) |
| 42 | + safePutBytes(seed) |
| 43 | + safePutBytes(fixtureFileTarget.toByteArray()) |
| 44 | + }.toHexString() |
| 45 | + assertThat(actual).isEqualTo(expected) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testSoftHashConcreteFile() = runBlocking { |
| 50 | + val hasher = SourceFileHasher(repoAbsolutePath, null) |
| 51 | + val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) |
| 52 | + val actual = hasher.softDigest(bazelSourceFileTarget)?.toHexString() |
| 53 | + val expected = sha256 { |
| 54 | + safePutBytes(fixtureFileContent) |
| 55 | + safePutBytes(seed) |
| 56 | + safePutBytes(fixtureFileTarget.toByteArray()) |
| 57 | + }.toHexString() |
| 58 | + assertThat(actual).isEqualTo(expected) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun testSoftHashNonExistedFile() = runBlocking { |
| 63 | + val hasher = SourceFileHasher(repoAbsolutePath, null) |
| 64 | + val bazelSourceFileTarget = BazelSourceFileTarget("//i/do/not/exist", seed) |
| 65 | + val actual = hasher.softDigest(bazelSourceFileTarget) |
| 66 | + assertThat(actual).isNull() |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun testSoftHashExternalTarget() = runBlocking { |
| 71 | + val target = "@bazel-diff//some:file" |
| 72 | + val hasher = SourceFileHasher(repoAbsolutePath, null) |
| 73 | + val bazelSourceFileTarget = BazelSourceFileTarget(target, seed) |
| 74 | + val actual = hasher.softDigest(bazelSourceFileTarget) |
| 75 | + assertThat(actual).isNull() |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun testHashNonExistedFile() = runBlocking { |
| 80 | + val target = "//i/do/not/exist" |
| 81 | + val hasher = SourceFileHasher(repoAbsolutePath, null) |
| 82 | + val bazelSourceFileTarget = BazelSourceFileTarget(target, seed) |
| 83 | + val actual = hasher.digest(bazelSourceFileTarget).toHexString() |
| 84 | + val expected = sha256 { |
| 85 | + safePutBytes(seed) |
| 86 | + safePutBytes(target.toByteArray()) |
| 87 | + }.toHexString() |
| 88 | + assertThat(actual).isEqualTo(expected) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun testHashExternalTarget() = runBlocking { |
| 93 | + val target = "@bazel-diff//some:file" |
| 94 | + val hasher = SourceFileHasher(repoAbsolutePath, null) |
| 95 | + val bazelSourceFileTarget = BazelSourceFileTarget(target, seed) |
| 96 | + val actual = hasher.digest(bazelSourceFileTarget).toHexString() |
| 97 | + val expected = sha256 {}.toHexString() |
| 98 | + assertThat(actual).isEqualTo(expected) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun testHashWithProvidedContentHash() = runBlocking { |
| 103 | + val filenameToContentHash = hashMapOf("cli/src/test/kotlin/com/bazel_diff/hash/fixture/foo.ts" to "foo-content-hash") |
| 104 | + val hasher = SourceFileHasher(repoAbsolutePath, filenameToContentHash) |
| 105 | + val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) |
| 106 | + val actual = hasher.digest(bazelSourceFileTarget).toHexString() |
| 107 | + val expected = sha256 { |
| 108 | + safePutBytes("foo-content-hash".toByteArray()) |
| 109 | + safePutBytes(seed) |
| 110 | + safePutBytes(fixtureFileTarget.toByteArray()) |
| 111 | + }.toHexString() |
| 112 | + assertThat(actual).isEqualTo(expected) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun testHashWithProvidedContentHashButNotInKey() = runBlocking { |
| 117 | + val filenameToContentHash = hashMapOf("cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" to "foo-content-hash") |
| 118 | + val hasher = SourceFileHasher(repoAbsolutePath, filenameToContentHash) |
| 119 | + val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) |
| 120 | + val actual = hasher.digest(bazelSourceFileTarget).toHexString() |
| 121 | + val expected = sha256 { |
| 122 | + safePutBytes(fixtureFileContent) |
| 123 | + safePutBytes(seed) |
| 124 | + safePutBytes(fixtureFileTarget.toByteArray()) |
| 125 | + }.toHexString() |
| 126 | + assertThat(actual).isEqualTo(expected) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun testHashWithProvidedContentHashWithLeadingColon() = runBlocking { |
| 131 | + val targetName = "//:cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" |
| 132 | + val filenameToContentHash = hashMapOf("cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" to "foo-content-hash") |
| 133 | + val hasher = SourceFileHasher(repoAbsolutePath, filenameToContentHash) |
| 134 | + val bazelSourceFileTarget = BazelSourceFileTarget(targetName, seed) |
| 135 | + val actual = hasher.digest(bazelSourceFileTarget).toHexString() |
| 136 | + val expected = sha256 { |
| 137 | + safePutBytes("foo-content-hash".toByteArray()) |
| 138 | + safePutBytes(seed) |
| 139 | + safePutBytes(targetName.toByteArray()) |
| 140 | + }.toHexString() |
| 141 | + assertThat(actual).isEqualTo(expected) |
| 142 | + } |
| 143 | +} |
0 commit comments