1 /* 2 * Copyright (C) 2017 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 package com.android.tradefed.testtype.host; 17 18 import static com.google.common.base.Preconditions.checkNotNull; 19 20 import com.android.tradefed.build.IBuildInfo; 21 import com.android.tradefed.config.Option; 22 import com.android.tradefed.config.Option.Importance; 23 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 24 import com.android.tradefed.result.FileInputStreamSource; 25 import com.android.tradefed.result.ITestInvocationListener; 26 import com.android.tradefed.result.InputStreamSource; 27 import com.android.tradefed.result.LogDataType; 28 import com.android.tradefed.testtype.IBuildReceiver; 29 import com.android.tradefed.testtype.IRemoteTest; 30 import com.android.tradefed.util.FileUtil; 31 32 import com.google.common.annotations.VisibleForTesting; 33 34 import java.io.File; 35 import java.util.ArrayList; 36 import java.util.HashMap; 37 import java.util.List; 38 39 /** A dummy test that fowards coverage measurements from the build provider to the logger. */ 40 public final class CoverageMeasurementForwarder implements IRemoteTest, IBuildReceiver { 41 42 @Option( 43 name = "coverage-measurement", 44 description = 45 "The name of the build artifact to forward. The artifact should be a " 46 + "coverage measurement (either an .ec or .exec file) that to save as a " 47 + "test result. This option may be repeated.", 48 importance = Importance.IF_UNSET, 49 mandatory = false 50 ) 51 private List<String> mCoverageMeasurements = new ArrayList<>(); 52 53 private IBuildInfo mBuild; 54 55 /** Sets the --coverage-measurement option for testing. */ 56 @VisibleForTesting 57 void setCoverageMeasurements(List<String> coverageMeasurements) { 58 mCoverageMeasurements = coverageMeasurements; 59 } 60 61 @Override 62 public void setBuild(IBuildInfo buildInfo) { 63 mBuild = buildInfo; 64 } 65 66 /** Returns the {@link IBuildInfo} for this invocation. */ 67 private IBuildInfo getBuild() { 68 return mBuild; 69 } 70 71 @Override 72 public void run(ITestInvocationListener listener) { 73 if (mCoverageMeasurements.isEmpty()) { 74 return; 75 } 76 77 listener.testRunStarted("CoverageMeasurementedForwarder", 0); 78 for (String artifactName : mCoverageMeasurements) { 79 File coverageMeasurement = 80 checkNotNull( 81 getBuild().getFile(artifactName), 82 "Failed to get artifact '%s' from the build.", 83 artifactName); 84 try (InputStreamSource stream = new FileInputStreamSource(coverageMeasurement)) { 85 listener.testLog(artifactName, LogDataType.COVERAGE, stream); 86 } finally { 87 FileUtil.deleteFile(coverageMeasurement); 88 } 89 } 90 listener.testRunEnded(0, new HashMap<String, Metric>()); 91 } 92 } 93