@@ -17,6 +17,7 @@ import java.nio.file.Paths
1717
1818internal class SourceFileHasherTest : KoinTest {
1919 private val repoAbsolutePath = Paths .get(" " ).toAbsolutePath()
20+ private val outputBasePath = Files .createTempDirectory(" SourceFileHasherTest" )
2021 private val fixtureFileTarget = " //cli/src/test/kotlin/com/bazel_diff/hash/fixture:foo.ts"
2122 private val fixtureFileContent: ByteArray
2223 private val seed = " seed" .toByteArray()
@@ -34,7 +35,7 @@ internal class SourceFileHasherTest: KoinTest {
3435
3536 @Test
3637 fun testHashConcreteFile () = runBlocking {
37- val hasher = SourceFileHasher (repoAbsolutePath, null )
38+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, null )
3839 val bazelSourceFileTarget = BazelSourceFileTarget (fixtureFileTarget, seed)
3940 val actual = hasher.digest(bazelSourceFileTarget).toHexString()
4041 val expected = sha256 {
@@ -45,9 +46,27 @@ internal class SourceFileHasherTest: KoinTest {
4546 assertThat(actual).isEqualTo(expected)
4647 }
4748
49+ @Test
50+ fun testHashConcreteFileInExternalRepo () = runBlocking {
51+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, null , setOf (" external_repo" ))
52+ val externalRepoFilePath = outputBasePath.resolve(" external/external_repo/path/to/my_file.txt" )
53+ Files .createDirectories(externalRepoFilePath.parent)
54+ val externalRepoFileTarget = " @external_repo//path/to:my_file.txt"
55+ val externalRepoFileContent = " hello world"
56+ externalRepoFilePath.toFile().writeText(externalRepoFileContent)
57+ val bazelSourceFileTarget = BazelSourceFileTarget (externalRepoFileTarget, seed)
58+ val actual = hasher.digest(bazelSourceFileTarget).toHexString()
59+ val expected = sha256 {
60+ safePutBytes(externalRepoFileContent.toByteArray())
61+ safePutBytes(seed)
62+ safePutBytes(externalRepoFileTarget.toByteArray())
63+ }.toHexString()
64+ assertThat(actual).isEqualTo(expected)
65+ }
66+
4867 @Test
4968 fun testSoftHashConcreteFile () = runBlocking {
50- val hasher = SourceFileHasher (repoAbsolutePath, null )
69+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, null )
5170 val bazelSourceFileTarget = BazelSourceFileTarget (fixtureFileTarget, seed)
5271 val actual = hasher.softDigest(bazelSourceFileTarget)?.toHexString()
5372 val expected = sha256 {
@@ -60,7 +79,7 @@ internal class SourceFileHasherTest: KoinTest {
6079
6180 @Test
6281 fun testSoftHashNonExistedFile () = runBlocking {
63- val hasher = SourceFileHasher (repoAbsolutePath, null )
82+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, null )
6483 val bazelSourceFileTarget = BazelSourceFileTarget (" //i/do/not/exist" , seed)
6584 val actual = hasher.softDigest(bazelSourceFileTarget)
6685 assertThat(actual).isNull()
@@ -69,7 +88,7 @@ internal class SourceFileHasherTest: KoinTest {
6988 @Test
7089 fun testSoftHashExternalTarget () = runBlocking {
7190 val target = " @bazel-diff//some:file"
72- val hasher = SourceFileHasher (repoAbsolutePath, null )
91+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, null )
7392 val bazelSourceFileTarget = BazelSourceFileTarget (target, seed)
7493 val actual = hasher.softDigest(bazelSourceFileTarget)
7594 assertThat(actual).isNull()
@@ -78,7 +97,7 @@ internal class SourceFileHasherTest: KoinTest {
7897 @Test
7998 fun testHashNonExistedFile () = runBlocking {
8099 val target = " //i/do/not/exist"
81- val hasher = SourceFileHasher (repoAbsolutePath, null )
100+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, null )
82101 val bazelSourceFileTarget = BazelSourceFileTarget (target, seed)
83102 val actual = hasher.digest(bazelSourceFileTarget).toHexString()
84103 val expected = sha256 {
@@ -91,7 +110,7 @@ internal class SourceFileHasherTest: KoinTest {
91110 @Test
92111 fun testHashExternalTarget () = runBlocking {
93112 val target = " @bazel-diff//some:file"
94- val hasher = SourceFileHasher (repoAbsolutePath, null )
113+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, null )
95114 val bazelSourceFileTarget = BazelSourceFileTarget (target, seed)
96115 val actual = hasher.digest(bazelSourceFileTarget).toHexString()
97116 val expected = sha256 {}.toHexString()
@@ -101,7 +120,7 @@ internal class SourceFileHasherTest: KoinTest {
101120 @Test
102121 fun testHashWithProvidedContentHash () = runBlocking {
103122 val filenameToContentHash = hashMapOf(" cli/src/test/kotlin/com/bazel_diff/hash/fixture/foo.ts" to " foo-content-hash" )
104- val hasher = SourceFileHasher (repoAbsolutePath, filenameToContentHash)
123+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, filenameToContentHash)
105124 val bazelSourceFileTarget = BazelSourceFileTarget (fixtureFileTarget, seed)
106125 val actual = hasher.digest(bazelSourceFileTarget).toHexString()
107126 val expected = sha256 {
@@ -115,7 +134,7 @@ internal class SourceFileHasherTest: KoinTest {
115134 @Test
116135 fun testHashWithProvidedContentHashButNotInKey () = runBlocking {
117136 val filenameToContentHash = hashMapOf(" cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" to " foo-content-hash" )
118- val hasher = SourceFileHasher (repoAbsolutePath, filenameToContentHash)
137+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, filenameToContentHash)
119138 val bazelSourceFileTarget = BazelSourceFileTarget (fixtureFileTarget, seed)
120139 val actual = hasher.digest(bazelSourceFileTarget).toHexString()
121140 val expected = sha256 {
@@ -130,7 +149,7 @@ internal class SourceFileHasherTest: KoinTest {
130149 fun testHashWithProvidedContentHashWithLeadingColon () = runBlocking {
131150 val targetName = " //:cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts"
132151 val filenameToContentHash = hashMapOf(" cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" to " foo-content-hash" )
133- val hasher = SourceFileHasher (repoAbsolutePath, filenameToContentHash)
152+ val hasher = SourceFileHasher (repoAbsolutePath, outputBasePath, filenameToContentHash)
134153 val bazelSourceFileTarget = BazelSourceFileTarget (targetName, seed)
135154 val actual = hasher.digest(bazelSourceFileTarget).toHexString()
136155 val expected = sha256 {
0 commit comments