|
5 | 5 | package kotlinx.coroutines.experimental.tools |
6 | 6 |
|
7 | 7 | import org.junit.* |
8 | | -import org.junit.rules.* |
| 8 | +import org.junit.runner.* |
| 9 | +import org.junit.runners.* |
9 | 10 | import java.io.* |
| 11 | +import java.util.* |
10 | 12 | import java.util.jar.* |
11 | | - |
12 | | -class PublicApiTest { |
13 | | - |
14 | | - /* |
15 | | - * How to add a test for your module kotlinx-coroutines-foo? |
16 | | - * |
17 | | - * Dump public declarations via PublicApiDump.kt and create file |
18 | | - * reference-public-api/kotlinx-coroutines-foo.txt with dumped declarations. |
19 | | - * |
20 | | - * Then add test: |
21 | | - * |
22 | | - * @Test |
23 | | - * fun kotlinxCorountesFoo() { // <- name pattern should match txt file from reference-public-api |
24 | | - * snapshotAPIAndCompare($relative_path_to_module) |
25 | | - * } |
26 | | - */ |
27 | | - |
28 | | - @Rule |
29 | | - @JvmField |
30 | | - val testName = TestName() |
31 | | - |
32 | | - @Test |
33 | | - fun kotlinxCoroutinesCore() { |
34 | | - snapshotAPIAndCompare("core/kotlinx-coroutines-core", nonPublicPackages = listOf( |
35 | | - "kotlinx.coroutines.experimental.internal", |
36 | | - "kotlinx.coroutines.experimental.scheduling")) |
37 | | - } |
38 | | - |
39 | | - @Test |
40 | | - fun kotlinxCoroutinesReactive() { |
41 | | - snapshotAPIAndCompare("reactive/kotlinx-coroutines-reactive") |
42 | | - } |
43 | | - |
44 | | - @Test |
45 | | - fun kotlinxCoroutinesReactor() { |
46 | | - snapshotAPIAndCompare("reactive/kotlinx-coroutines-reactor") |
47 | | - } |
48 | | - |
49 | | - @Test |
50 | | - fun kotlinxCoroutinesRx1() { |
51 | | - snapshotAPIAndCompare("reactive/kotlinx-coroutines-rx1") |
52 | | - } |
53 | | - |
54 | | - @Test |
55 | | - fun kotlinxCoroutinesRx2() { |
56 | | - snapshotAPIAndCompare("reactive/kotlinx-coroutines-rx2") |
57 | | - } |
58 | | - |
59 | | - @Test |
60 | | - fun kotlinxCoroutinesGuava() { |
61 | | - snapshotAPIAndCompare("integration/kotlinx-coroutines-guava") |
62 | | - } |
63 | | - |
64 | | - @Test |
65 | | - fun kotlinxCoroutinesJdk8() { |
66 | | - snapshotAPIAndCompare("integration/kotlinx-coroutines-jdk8") |
67 | | - } |
68 | | - |
69 | | - |
70 | | - @Test |
71 | | - fun kotlinxCoroutinesNio() { |
72 | | - snapshotAPIAndCompare("integration/kotlinx-coroutines-nio") |
73 | | - } |
74 | | - |
75 | | - @Test |
76 | | - fun kotlinxCoroutinesQuasar() { |
77 | | - snapshotAPIAndCompare("integration/kotlinx-coroutines-quasar") |
78 | | - } |
79 | | - |
80 | | - @Test |
81 | | - fun kotlinxCoroutinesAndroid() { |
82 | | - snapshotAPIAndCompare("ui/kotlinx-coroutines-android") |
83 | | - } |
84 | | - |
85 | | - |
86 | | - @Test |
87 | | - fun kotlinxCoroutinesJavafx() { |
88 | | - snapshotAPIAndCompare("ui/kotlinx-coroutines-javafx") |
| 13 | +import kotlin.collections.ArrayList |
| 14 | + |
| 15 | +@RunWith(Parameterized::class) |
| 16 | +class PublicApiTest( |
| 17 | + private val rootDir: String, |
| 18 | + private val moduleName: String |
| 19 | +) { |
| 20 | + companion object { |
| 21 | + private val apiProps = ClassLoader.getSystemClassLoader() |
| 22 | + .getResource("api.properties").openStream().use { Properties().apply { load(it) } } |
| 23 | + private val nonPublicPackages = apiProps.getProperty("packages.internal")!!.split(" ") |
| 24 | + |
| 25 | + @Parameterized.Parameters(name = "{1}") |
| 26 | + @JvmStatic |
| 27 | + fun modules(): List<Array<Any>> { |
| 28 | + val moduleRoots = apiProps.getProperty("module.roots").split(" ") |
| 29 | + val moduleMarker = apiProps.getProperty("module.marker")!! |
| 30 | + val moduleIgnore = apiProps.getProperty("module.ignore")!!.split(" ").toSet() |
| 31 | + val modules = ArrayList<Array<Any>>() |
| 32 | + for (rootDir in moduleRoots) { |
| 33 | + File("../$rootDir").listFiles( FileFilter { it.isDirectory })?.forEach { dir -> |
| 34 | + if (dir.name !in moduleIgnore && File(dir, moduleMarker).exists()) { |
| 35 | + modules += arrayOf<Any>(rootDir, dir.name) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return modules |
| 40 | + } |
89 | 41 | } |
90 | 42 |
|
91 | 43 | @Test |
92 | | - fun kotlinxCoroutinesSwing() { |
93 | | - snapshotAPIAndCompare("ui/kotlinx-coroutines-swing") |
94 | | - } |
95 | | - |
96 | | - private fun snapshotAPIAndCompare(basePath: String, jarPattern: String = basePath.substring(basePath.indexOf("/") + 1), |
97 | | - publicPackages: List<String> = emptyList(), nonPublicPackages: List<String> = emptyList()) { |
98 | | - val base = File("../$basePath/build/libs").absoluteFile.normalize() |
99 | | - val jarFile = getJarPath(base, jarPattern) |
100 | | - val kotlinJvmMappingsFiles = listOf(base.resolve("../visibilities.json")) |
101 | | - |
102 | | - val publicPackagePrefixes = publicPackages.map { it.replace('.', '/') + '/' } |
| 44 | + fun testApi() { |
| 45 | + val libsDir = File("../$rootDir/$moduleName/build/libs").absoluteFile.normalize() |
| 46 | + val jarFile = getJarPath(libsDir) |
| 47 | + val kotlinJvmMappingsFiles = listOf(libsDir.resolve("../visibilities.json")) |
103 | 48 | val visibilities = |
104 | 49 | kotlinJvmMappingsFiles |
105 | | - .map { readKotlinVisibilities(it).filterKeys { name -> publicPackagePrefixes.none { name.startsWith(it) } } } |
| 50 | + .map { readKotlinVisibilities(it) } |
106 | 51 | .reduce { m1, m2 -> m1 + m2 } |
107 | | - |
108 | 52 | val api = getBinaryAPI(JarFile(jarFile), visibilities).filterOutNonPublic(nonPublicPackages) |
109 | | - |
110 | | - val target = File("reference-public-api") |
111 | | - .resolve(testName.methodName.replaceCamelCaseWithDashedLowerCase() + ".txt") |
112 | | - |
113 | | - api.dumpAndCompareWith(target) |
| 53 | + api.dumpAndCompareWith(File("reference-public-api").resolve("$moduleName.txt")) |
114 | 54 | } |
115 | 55 |
|
116 | | - private fun getJarPath(base: File, jarPattern: String, kotlinVersion: String? = null): File { |
117 | | - val versionPattern = kotlinVersion?.let { "-" + Regex.escape(it) } ?: ".+" |
118 | | - val regex = Regex("$jarPattern$versionPattern\\.jar") |
119 | | - val files = (base.listFiles() ?: throw Exception("Cannot list files in $base")) |
| 56 | + private fun getJarPath(libsDir: File): File { |
| 57 | + val regex = Regex("$moduleName-.+\\.jar") |
| 58 | + val files = (libsDir.listFiles() ?: throw Exception("Cannot list files in $libsDir")) |
120 | 59 | .filter { it.name.let { |
121 | 60 | it matches regex |
122 | 61 | && !it.endsWith("-sources.jar") |
123 | 62 | && !it.endsWith("-javadoc.jar") |
124 | 63 | && !it.endsWith("-tests.jar")} } |
125 | | - |
126 | | - return files.singleOrNull() ?: throw Exception("No single file matching $regex in $base:\n${files.joinToString("\n")}") |
| 64 | + return files.singleOrNull() ?: throw Exception("No single file matching $regex in $libsDir:\n${files.joinToString("\n")}") |
127 | 65 | } |
128 | 66 | } |
0 commit comments