blob: 52479d56efaa1743da8d6be5f96ec221e6ed4f26 [file] [log] [blame]
Vsevolod Tolstopyatov44e625d2019-05-24 11:26:09 +03001/*
Aurimas Liutikascd3cb312021-05-12 21:56:16 +00002 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Vsevolod Tolstopyatov44e625d2019-05-24 11:26:09 +03003 */
4
5package kotlinx.coroutines.validator
6
7import com.google.gson.*
8import org.apache.commons.compress.archivers.tar.*
9import org.junit.*
Vsevolod Tolstopyatov44e625d2019-05-24 11:26:09 +030010import java.io.*
11import java.util.zip.*
Dmitry Khalanskiy91e4d772020-02-11 18:29:19 +030012import org.junit.Assert.*
Vsevolod Tolstopyatov44e625d2019-05-24 11:26:09 +030013
14class NpmPublicationValidator {
dkhalanskyjb36512762020-02-21 17:31:05 +030015 private val VERSION = System.getenv("deployVersion")!!
16 private val BUILD_DIR = System.getenv("projectRoot")!!
Vsevolod Tolstopyatov44e625d2019-05-24 11:26:09 +030017 private val NPM_ARTIFACT = "$BUILD_DIR/kotlinx-coroutines-core/build/npm/kotlinx-coroutines-core-$VERSION.tgz"
18
19 @Test
20 fun testPackageJson() {
21 println("Checking dependencies of $NPM_ARTIFACT")
22 val visited = visit("package.json") {
23 val json = JsonParser().parse(content()).asJsonObject
24 assertEquals(VERSION, json["version"].asString)
25 assertNull(json["dependencies"])
26 val peerDependencies = json["peerDependencies"].asJsonObject
27 assertEquals(1, peerDependencies.size())
28 assertNotNull(peerDependencies["kotlin"])
29 }
30 assertEquals(1, visited)
31 }
32
33 @Test
34 fun testAtomicfuDependencies() {
35 println("Checking contents of $NPM_ARTIFACT")
36 val visited = visit(".js") {
37 val content = content()
38 assertFalse(content, content.contains("atomicfu", true))
39 assertFalse(content, content.contains("atomicint", true))
40 assertFalse(content, content.contains("atomicboolean", true))
41 }
42 assertEquals(2, visited)
43 }
44
45 private fun InputStream.content(): String {
46 val bais = ByteArrayOutputStream()
47 val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
48 var read = read(buffer, 0, DEFAULT_BUFFER_SIZE)
49 while (read >= 0) {
50 bais.write(buffer, 0, read)
51 read = read(buffer, 0, DEFAULT_BUFFER_SIZE)
52 }
53 return bais.toString()
54 }
55
56 private inline fun visit(fileSuffix: String, block: InputStream.(entry: TarArchiveEntry) -> Unit): Int {
57 var visited = 0
58 TarArchiveInputStream(GZIPInputStream(FileInputStream(NPM_ARTIFACT))).use { tais ->
59 var entry: TarArchiveEntry? = tais.nextTarEntry ?: return 0
60 do {
61 if (entry!!.name.endsWith(fileSuffix)) {
62 ++visited
63 tais.block(entry)
64 }
65 entry = tais.nextTarEntry
66 } while (entry != null)
67
68 return visited
69 }
70 }
71}