1 /*
<lambda>null2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import androidx.benchmark.darwin.gradle.skia.Metrics
18 import androidx.benchmark.darwin.gradle.xcode.GsonHelpers
19 import androidx.benchmark.darwin.gradle.xcode.XcResultParser
20 import com.google.common.truth.Truth.assertThat
21 import org.junit.Assume.assumeTrue
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.junit.runners.JUnit4
25 
26 @RunWith(JUnit4::class)
27 class XcResultParserTest {
28     @Test
29     fun parseXcResultFileTest() {
30         val operatingSystem = System.getProperty("os.name")
31         // Only run this test on an `Mac OS X` machine.
32         assumeTrue(operatingSystem.contains("Mac", ignoreCase = true))
33         val xcResultFile = testData("sample-xcode.xcresult")
34         val parser =
35             XcResultParser(xcResultFile) { args ->
36                 val builder = ProcessBuilder(*args.toTypedArray())
37                 val process = builder.start()
38                 val resultCode = process.waitFor()
39                 require(resultCode == 0) {
40                     "Process terminated unexpectedly (${args.joinToString(separator = " ")})"
41                 }
42                 process.inputStream.use { it.reader().readText() }
43             }
44         val (record, summaries) = parser.parseResults()
45         // Usually corresponds to the size of the test suite
46         // In the case of KMP benchmarks, this is always 1 per module.
47         assertThat(record.actions.testReferences().size).isEqualTo(1)
48         assertThat(record.actions.isSuccessful()).isTrue()
49         // Metrics typically correspond to the number of tests
50         assertThat(record.metrics.size()).isEqualTo(2)
51         assertThat(summaries.isNotEmpty()).isTrue()
52         val metrics = Metrics.buildMetrics(record, summaries, referenceSha = null)
53         val json = GsonHelpers.gsonBuilder().setPrettyPrinting().create().toJson(metrics)
54         println()
55         println(json)
56         println()
57         assertThat(json).isNotEmpty()
58     }
59 }
60