-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: Allow user to provide their own content hash to minimise IO operation #129
Merged
tinder-maxwellelliott
merged 7 commits into
Tinder:master
from
fa93hws:provide-content-hash
May 19, 2022
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1bac893
content hash class to parse json file, di and args
fa93hws 5125715
use provided content hash whenever possible
fa93hws 15c919e
update docs
fa93hws 8546a1c
use existing deserialiser
fa93hws b48ded2
Merge branch 'master' into provide-content-hash
fa93hws f0246f4
Merge branch 'master' into provide-content-hash
tinder-maxwellelliott 4262c07
Merge branch 'master' into provide-content-hash
tinder-maxwellelliott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
18 changes: 18 additions & 0 deletions
18
cli/src/main/kotlin/com/bazel_diff/io/ContentHashProvider.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.bazel_diff.io | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
|
||
class ContentHashProvider(path: Path?) { | ||
// filename relative to workspace -> content hash of the file | ||
val filenameToHash: Map<String, String>? = if (path == null) null else readJson(path) | ||
|
||
private fun readJson(file: Path): Map<String, String> { | ||
val gson = Gson() | ||
val reader = Files.newBufferedReader(file) | ||
val shape = object : TypeToken<Map<String, String>>() {}.type | ||
return gson.fromJson(reader, shape) | ||
} | ||
} |
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
143 changes: 143 additions & 0 deletions
143
cli/src/test/kotlin/com/bazel_diff/hash/SourceFileHasherTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.bazel_diff.hash | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isNull | ||
import com.bazel_diff.bazel.BazelSourceFileTarget | ||
import com.bazel_diff.extensions.toHexString | ||
import com.bazel_diff.testModule | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.koin.test.KoinTest | ||
import org.koin.test.KoinTestRule | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
|
||
internal class SourceFileHasherTest: KoinTest { | ||
private val repoAbsolutePath = Paths.get("").toAbsolutePath() | ||
private val fixtureFileTarget = "//cli/src/test/kotlin/com/bazel_diff/hash/fixture:foo.ts" | ||
private val fixtureFileContent: ByteArray | ||
private val seed = "seed".toByteArray() | ||
|
||
init { | ||
val path = Paths.get("cli/src/test/kotlin/com/bazel_diff/hash/fixture/foo.ts") | ||
fixtureFileContent = Files.readAllBytes(path) | ||
} | ||
|
||
|
||
@get:Rule | ||
val koinTestRule = KoinTestRule.create { | ||
modules(testModule()) | ||
} | ||
|
||
@Test | ||
fun testHashConcreteFile() = runBlocking { | ||
val hasher = SourceFileHasher(repoAbsolutePath, null) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) | ||
val actual = hasher.digest(bazelSourceFileTarget).toHexString() | ||
val expected = sha256 { | ||
safePutBytes(fixtureFileContent) | ||
safePutBytes(seed) | ||
safePutBytes(fixtureFileTarget.toByteArray()) | ||
}.toHexString() | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun testSoftHashConcreteFile() = runBlocking { | ||
val hasher = SourceFileHasher(repoAbsolutePath, null) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) | ||
val actual = hasher.softDigest(bazelSourceFileTarget)?.toHexString() | ||
val expected = sha256 { | ||
safePutBytes(fixtureFileContent) | ||
safePutBytes(seed) | ||
safePutBytes(fixtureFileTarget.toByteArray()) | ||
}.toHexString() | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun testSoftHashNonExistedFile() = runBlocking { | ||
val hasher = SourceFileHasher(repoAbsolutePath, null) | ||
val bazelSourceFileTarget = BazelSourceFileTarget("//i/do/not/exist", seed) | ||
val actual = hasher.softDigest(bazelSourceFileTarget) | ||
assertThat(actual).isNull() | ||
} | ||
|
||
@Test | ||
fun testSoftHashExternalTarget() = runBlocking { | ||
val target = "@bazel-diff//some:file" | ||
val hasher = SourceFileHasher(repoAbsolutePath, null) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(target, seed) | ||
val actual = hasher.softDigest(bazelSourceFileTarget) | ||
assertThat(actual).isNull() | ||
} | ||
|
||
@Test | ||
fun testHashNonExistedFile() = runBlocking { | ||
val target = "//i/do/not/exist" | ||
val hasher = SourceFileHasher(repoAbsolutePath, null) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(target, seed) | ||
val actual = hasher.digest(bazelSourceFileTarget).toHexString() | ||
val expected = sha256 { | ||
safePutBytes(seed) | ||
safePutBytes(target.toByteArray()) | ||
}.toHexString() | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun testHashExternalTarget() = runBlocking { | ||
val target = "@bazel-diff//some:file" | ||
val hasher = SourceFileHasher(repoAbsolutePath, null) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(target, seed) | ||
val actual = hasher.digest(bazelSourceFileTarget).toHexString() | ||
val expected = sha256 {}.toHexString() | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun testHashWithProvidedContentHash() = runBlocking { | ||
val filenameToContentHash = hashMapOf("cli/src/test/kotlin/com/bazel_diff/hash/fixture/foo.ts" to "foo-content-hash") | ||
val hasher = SourceFileHasher(repoAbsolutePath, filenameToContentHash) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) | ||
val actual = hasher.digest(bazelSourceFileTarget).toHexString() | ||
val expected = sha256 { | ||
safePutBytes("foo-content-hash".toByteArray()) | ||
safePutBytes(seed) | ||
safePutBytes(fixtureFileTarget.toByteArray()) | ||
}.toHexString() | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun testHashWithProvidedContentHashButNotInKey() = runBlocking { | ||
val filenameToContentHash = hashMapOf("cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" to "foo-content-hash") | ||
val hasher = SourceFileHasher(repoAbsolutePath, filenameToContentHash) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(fixtureFileTarget, seed) | ||
val actual = hasher.digest(bazelSourceFileTarget).toHexString() | ||
val expected = sha256 { | ||
safePutBytes(fixtureFileContent) | ||
safePutBytes(seed) | ||
safePutBytes(fixtureFileTarget.toByteArray()) | ||
}.toHexString() | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun testHashWithProvidedContentHashWithLeadingColon() = runBlocking { | ||
val targetName = "//:cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" | ||
val filenameToContentHash = hashMapOf("cli/src/test/kotlin/com/bazel_diff/hash/fixture/bar.ts" to "foo-content-hash") | ||
val hasher = SourceFileHasher(repoAbsolutePath, filenameToContentHash) | ||
val bazelSourceFileTarget = BazelSourceFileTarget(targetName, seed) | ||
val actual = hasher.digest(bazelSourceFileTarget).toHexString() | ||
val expected = sha256 { | ||
safePutBytes("foo-content-hash".toByteArray()) | ||
safePutBytes(seed) | ||
safePutBytes(targetName.toByteArray()) | ||
}.toHexString() | ||
assertThat(actual).isEqualTo(expected) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('123') |
50 changes: 50 additions & 0 deletions
50
cli/src/test/kotlin/com/bazel_diff/io/ContentHashProviderTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bazel_diff.io | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.* | ||
import com.bazel_diff.testModule | ||
import com.google.gson.JsonSyntaxException | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.koin.test.KoinTest | ||
import org.koin.test.KoinTestRule | ||
import kotlin.io.path.Path | ||
|
||
internal class ContentHashProviderTest: KoinTest { | ||
@get:Rule | ||
val koinTestRule = KoinTestRule.create { | ||
modules(testModule()) | ||
} | ||
|
||
@Test | ||
fun testNullPath() = runBlocking { | ||
val contentHashProvider = ContentHashProvider(null) | ||
assertThat(contentHashProvider.filenameToHash).isNull() | ||
} | ||
|
||
@Test | ||
fun testNonExistingPath() = runBlocking { | ||
assertThat { | ||
ContentHashProvider(Path("/not/exists")) | ||
}.isFailure().hasClass(java.nio.file.NoSuchFileException::class) | ||
} | ||
|
||
@Test | ||
fun testParseJsonFileWithWrongShape() = runBlocking { | ||
val path = Path("cli/src/test/kotlin/com/bazel_diff/io/fixture/wrong.json") | ||
assertThat { | ||
ContentHashProvider(path) | ||
}.isFailure().hasClass(JsonSyntaxException::class) | ||
} | ||
|
||
@Test | ||
fun testParseJsonFileWithCorrectShape() = runBlocking { | ||
val path = Path("cli/src/test/kotlin/com/bazel_diff/io/fixture/correct.json") | ||
val map = ContentHashProvider(path).filenameToHash | ||
assertThat(map).isNotNull().containsOnly( | ||
"foo" to "content-hash-for-foo", | ||
"bar" to "content-hash-for-bar" | ||
) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"foo": "content-hash-for-foo", | ||
"bar": "content-hash-for-bar" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want
//:foo/bar
to befoo/bar
instead of/foo/bar
.