Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ internal fun preparedClassStartupIr(case: CallsClassStartCase, scope: Path, outp
val fingerprint = startupIrFingerprint(case)
val cache = root.resolve(fingerprint)
val lockPath = root.resolve("$fingerprint.lock")
FileChannel.open(lockPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE).use { channel ->
val ir = FileChannel.open(lockPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE).use { channel ->
channel.lock().use {
return prepareLockedStartupIr(cache = cache, scope = scope, output = output, fingerprint = fingerprint)
prepareLockedStartupIr(cache = cache, scope = scope, output = output, fingerprint = fingerprint)
}
}
val missing = case.requiredIrSources.filterNot { source -> Files.isRegularFile(ir.resolve("$source.json")) }
require(missing.isEmpty()) { "Frontend did not emit IR for required sources: ${missing.sorted()}" }
return ir
}

private fun prepareLockedStartupIr(cache: Path, scope: Path, output: Path, fingerprint: String): Path {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ internal data class CallsClassStartCase(
val acquisitionReceipt: String? = null,
val acquisitionComplete: Boolean = false,
val scenario: CallsClassLifecycleScenario? = null,
val requiredIrSources: Set<String> = emptySet(),
) {
init {
require(classId.matches(Regex("[A-Za-z0-9_-]+")))
require(identity.isNotBlank() && className.isNotBlank())
require(projectRevision.matches(Regex("[0-9a-f]{40}")))
require(sourceFile in inputSha256) { "The full target source must be frozen" }
require(inputSha256.keys.containsAll(requiredIrSources)) { "Required IR sources must be frozen inputs" }
inputSha256.forEach { (name, digest) ->
val path = Path.of(name)
require(!path.isAbsolute && path.normalize() == path && !path.startsWith(".."))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.usvm.ts.calls

import org.junit.jupiter.api.io.TempDir
import java.nio.file.Files
import java.nio.file.Path
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

class CallsRequiredIrSourcesTest {
@TempDir
lateinit var directory: Path

@Test
fun `check declared source emission for new and cached IR`() {
val scope = Files.createDirectory(directory.resolve("source"))
Files.writeString(scope.resolve("Parent.ts"), "export class Parent { result() { return 1; } }")
val child = "import { Parent } from './Parent'; export class Child extends Parent {}"
Files.writeString(scope.resolve("Child.ts"), child)
val frozen = startupIrFiles(scope)
val oneRoot = """{"files":["Child.ts"]}"""
val base = CallsClassStartCase(
classId = "child",
identity = "fixture@Child",
projectRevision = "a".repeat(n = 40),
sourceRoot = scope.toString(),
sourceFile = "Child.ts",
className = "Child",
inputSha256 = frozen,
frontendConfig = oneRoot,
)
Files.writeString(scope.resolve("tsconfig.json"), oneRoot)
val first = Files.createDirectory(directory.resolve("entry-only"))
val everything = frozen.keys
val required = base.copy(requiredIrSources = everything)

val ir = preparedClassStartupIr(case = base, scope = scope, output = first)

assertTrue(Files.isRegularFile(ir.resolve("Child.ts.json")))
assertEquals(startupIrFingerprint(base), startupIrFingerprint(required))
val cached = Files.createDirectory(directory.resolve("cached"))
first.resolve("ir-cache").toFile().copyRecursively(cached.resolve("ir-cache").toFile())
val failure = assertFailsWith<IllegalArgumentException> {
preparedClassStartupIr(case = required, scope = scope, output = cached)
}
assertContains(failure.message.orEmpty(), "Parent.ts")
assertTrue(Files.isRegularFile(cached.resolve("frontend-identity.json")))

val bothRoots = """{"files":["Child.ts","Parent.ts"]}"""
Files.writeString(scope.resolve("tsconfig.json"), bothRoots)
val full = Files.createDirectory(directory.resolve("both"))
val fullIr = preparedClassStartupIr(
case = required.copy(frontendConfig = bothRoots),
scope = scope,
output = full,
)
assertTrue(Files.isRegularFile(fullIr.resolve("Parent.ts.json")))

assertFailsWith<IllegalArgumentException> { base.copy(requiredIrSources = setOf("Unfrozen.ts")) }
}
}
Loading