Vsevolod Tolstopyatov | 44e625d | 2019-05-24 11:26:09 +0300 | [diff] [blame] | 1 | /* |
Aurimas Liutikas | cd3cb31 | 2021-05-12 21:56:16 +0000 | [diff] [blame^] | 2 | * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Vsevolod Tolstopyatov | 44e625d | 2019-05-24 11:26:09 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.validator |
| 6 | |
| 7 | import com.google.gson.* |
| 8 | import org.apache.commons.compress.archivers.tar.* |
| 9 | import org.junit.* |
Vsevolod Tolstopyatov | 44e625d | 2019-05-24 11:26:09 +0300 | [diff] [blame] | 10 | import java.io.* |
| 11 | import java.util.zip.* |
Dmitry Khalanskiy | 91e4d77 | 2020-02-11 18:29:19 +0300 | [diff] [blame] | 12 | import org.junit.Assert.* |
Vsevolod Tolstopyatov | 44e625d | 2019-05-24 11:26:09 +0300 | [diff] [blame] | 13 | |
| 14 | class NpmPublicationValidator { |
dkhalanskyjb | 3651276 | 2020-02-21 17:31:05 +0300 | [diff] [blame] | 15 | private val VERSION = System.getenv("deployVersion")!! |
| 16 | private val BUILD_DIR = System.getenv("projectRoot")!! |
Vsevolod Tolstopyatov | 44e625d | 2019-05-24 11:26:09 +0300 | [diff] [blame] | 17 | 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 | } |