Skip to content
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
merged 7 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ kt_jvm_test(
runtime_deps = [":cli-test-lib"],
)

kt_jvm_test(
name = "SourceFileHasherTest",
data = [
":src/test/kotlin/com/bazel_diff/hash/fixture/foo.ts",
],
test_class = "com.bazel_diff.hash.SourceFileHasherTest",
runtime_deps = [":cli-test-lib"],
)

kt_jvm_test(
name = "CalculateImpactedTargetsInteractorTest",
test_class = "com.bazel_diff.interactor.CalculateImpactedTargetsInteractorTest",
Expand Down Expand Up @@ -89,6 +98,18 @@ kt_jvm_test(
runtime_deps = [":cli-test-lib"],
)

kt_jvm_test(
name = "ContentHashProviderTest",
data = [
":src/test/kotlin/com/bazel_diff/io/fixture/correct.json",
":src/test/kotlin/com/bazel_diff/io/fixture/wrong.json",
],
test_class = "com.bazel_diff.io.ContentHashProviderTest",
runtime_deps = [
":cli-test-lib",
],
)

kt_jvm_library(
name = "cli-test-lib",
testonly = True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class GenerateHashesCommand : Callable<Int> {
)
lateinit var bazelPath: Path

@CommandLine.Option(
names = ["--contentHashPath"],
description = ["Path to content hash json file. It's a map which maps relative file path from workspace path to its content hash. Files in this map will skip content hashing"],
scope = CommandLine.ScopeType.INHERIT,
required = false
)
var contentHashPath: Path? = null

@CommandLine.Option(
names = ["-so", "--bazelStartupOptions"],
description = ["Additional space separated Bazel client startup options used when invoking Bazel"],
Expand Down Expand Up @@ -87,6 +95,7 @@ class GenerateHashesCommand : Callable<Int> {
hasherModule(
workspacePath,
bazelPath,
contentHashPath,
bazelStartupOptions,
bazelCommandOptions,
keepGoing,
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main/kotlin/com/bazel_diff/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.bazel_diff.hash.BuildGraphHasher
import com.bazel_diff.hash.RuleHasher
import com.bazel_diff.hash.SourceFileHasher
import com.bazel_diff.hash.TargetHasher
import com.bazel_diff.io.ContentHashProvider
import com.bazel_diff.log.Logger
import com.bazel_diff.log.StdoutLogger
import com.google.gson.GsonBuilder
Expand All @@ -17,6 +18,7 @@ import java.nio.file.Path
fun hasherModule(
workingDirectory: Path,
bazelPath: Path,
contentHashPath: Path?,
startupOptions: List<String>,
commandOptions: List<String>,
keepGoing: Boolean?,
Expand All @@ -38,6 +40,7 @@ fun hasherModule(
single { RuleHasher() }
single { SourceFileHasher() }
single(named("working-directory")) { workingDirectory }
single { ContentHashProvider(contentHashPath) }
}

fun loggingModule(verbose: Boolean) = module {
Expand Down
42 changes: 34 additions & 8 deletions cli/src/main/kotlin/com/bazel_diff/hash/SourceFileHasher.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bazel_diff.hash

import com.bazel_diff.bazel.BazelSourceFileTarget
import com.bazel_diff.io.ContentHashProvider
import com.bazel_diff.log.Logger
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
Expand All @@ -9,21 +10,46 @@ import java.nio.file.Path
import java.nio.file.Paths

class SourceFileHasher : KoinComponent {
private val workingDirectory: Path by inject(qualifier = named("working-directory"))
private val logger: Logger by inject()
private val workingDirectory: Path
private val logger: Logger
private val relativeFilenameToContentHash: Map<String, String>?
init {
val logger: Logger by inject()
this.logger = logger
}

constructor() {
val workingDirectory: Path by inject(qualifier = named("working-directory"))
this.workingDirectory = workingDirectory
val contentHashProvider: ContentHashProvider by inject()
relativeFilenameToContentHash = contentHashProvider.filenameToHash
}

constructor(workingDirectory: Path, relativeFilenameToContentHash: Map<String, String>?) {
this.workingDirectory = workingDirectory
this.relativeFilenameToContentHash = relativeFilenameToContentHash
}

fun digest(sourceFileTarget: BazelSourceFileTarget): ByteArray {
return sha256 {
val name = sourceFileTarget.name
if (name.startsWith("//")) {
val filenameSubstring = name.substring(2)
val filenamePath = filenameSubstring.replaceFirst(":".toRegex(), "/")
val absoluteFilePath = Paths.get(workingDirectory.toString(), filenamePath)
val file = absoluteFilePath.toFile()
if (file.exists() && file.isFile) {
putFile(file)
val filenamePath = filenameSubstring.replaceFirst(
":".toRegex(),
if (filenameSubstring.startsWith(":")) "" else "/"
Comment on lines +38 to +40
Copy link
Contributor Author

@fa93hws fa93hws May 18, 2022

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 be foo/bar instead of /foo/bar.

)
if (relativeFilenameToContentHash?.contains(filenamePath) == true) {
val contentHash = relativeFilenameToContentHash.getValue(filenamePath)
safePutBytes(contentHash.toByteArray())
} else {
logger.w { "File $absoluteFilePath not found" }
val absoluteFilePath = Paths.get(workingDirectory.toString(), filenamePath)
val file = absoluteFilePath.toFile()
if (file.exists() && file.isFile) {
putFile(file)
} else {
logger.w { "File $absoluteFilePath not found" }
}
}
safePutBytes(sourceFileTarget.seed)
safePutBytes(name.toByteArray())
Expand Down
18 changes: 18 additions & 0 deletions cli/src/main/kotlin/com/bazel_diff/io/ContentHashProvider.kt
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)
}
}
7 changes: 4 additions & 3 deletions cli/src/test/kotlin/com/bazel_diff/Modules.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.bazel_diff

import com.bazel_diff.bazel.BazelClient
import com.bazel_diff.bazel.BazelQueryService
import com.bazel_diff.hash.BuildGraphHasher
import com.bazel_diff.hash.RuleHasher
import com.bazel_diff.hash.SourceFileHasher
import com.bazel_diff.hash.TargetHasher
import com.bazel_diff.io.ContentHashProvider
import com.bazel_diff.log.Logger
import com.bazel_diff.log.StdoutLogger
import com.google.gson.GsonBuilder
import org.koin.core.module.Module
import org.koin.core.qualifier.named
import org.koin.dsl.module
import java.nio.file.Path
import java.nio.file.Paths

fun testModule(): Module = module {
single<Logger> { SilentLogger }
Expand All @@ -22,6 +21,8 @@ fun testModule(): Module = module {
single { RuleHasher() }
single { SourceFileHasher() }
single { GsonBuilder().setPrettyPrinting().create() }
single(named("working-directory")) { Paths.get("working-directory") }
single { ContentHashProvider(null) }
}

object SilentLogger : Logger {
Expand Down
143 changes: 143 additions & 0 deletions cli/src/test/kotlin/com/bazel_diff/hash/SourceFileHasherTest.kt
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)
}
}
1 change: 1 addition & 0 deletions cli/src/test/kotlin/com/bazel_diff/hash/fixture/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('123')
50 changes: 50 additions & 0 deletions cli/src/test/kotlin/com/bazel_diff/io/ContentHashProviderTest.kt
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"
)
}
}
4 changes: 4 additions & 0 deletions cli/src/test/kotlin/com/bazel_diff/io/fixture/correct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "content-hash-for-foo",
"bar": "content-hash-for-bar"
}
Loading