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 17 package com.android.tradefed.util; 18 19 import com.android.tradefed.log.LogUtil.CLog; 20 21 import com.google.common.annotations.VisibleForTesting; 22 23 import java.io.File; 24 import java.util.ArrayList; 25 import java.util.Arrays; 26 import java.util.HashSet; 27 import java.util.List; 28 import java.util.Set; 29 30 /** Utility class for making system calls. */ 31 public class SystemUtil { 32 33 @VisibleForTesting static SystemUtil singleton = new SystemUtil(); 34 35 // Environment variables for the test cases directory in target out directory and host out 36 // directory. 37 @VisibleForTesting 38 static final String ENV_ANDROID_TARGET_OUT_TESTCASES = "ANDROID_TARGET_OUT_TESTCASES"; 39 40 @VisibleForTesting 41 static final String ENV_ANDROID_HOST_OUT_TESTCASES = "ANDROID_HOST_OUT_TESTCASES"; 42 43 static final String ENV_ANDROID_PRODUCT_OUT = "ANDROID_PRODUCT_OUT"; 44 45 /** 46 * Get the value of an environment variable. 47 * 48 * <p>The wrapper function is created for mock in unit test. 49 * 50 * @param name the name of the environment variable. 51 * @return {@link String} value of the given environment variable. 52 */ 53 @VisibleForTesting 54 String getEnv(String name) { 55 return System.getenv(name); 56 } 57 58 /** 59 * Get a list of {@link File} of the test cases directories 60 * 61 * @return a list of {@link File} of directories of the test cases folder of build output, based 62 * on the value of environment variables. 63 */ 64 public static List<File> getTestCasesDirs() { 65 List<File> testCasesDirs = new ArrayList<File>(); 66 // TODO(b/36782030): Add ENV_ANDROID_HOST_OUT_TESTCASES back to the list. 67 Set<String> testCasesDirNames = 68 new HashSet<String>( 69 Arrays.asList(singleton.getEnv(ENV_ANDROID_TARGET_OUT_TESTCASES))); 70 for (String testCasesDirName : testCasesDirNames) { 71 if (testCasesDirName != null) { 72 File dir = new File(testCasesDirName); 73 if (dir.exists() && dir.isDirectory()) { 74 testCasesDirs.add(dir); 75 } else { 76 CLog.w( 77 "Path %s for test cases directory does not exist or it's not a " 78 + "directory.", 79 testCasesDirName); 80 } 81 } 82 } 83 return testCasesDirs; 84 } 85 86 /** 87 * Gets the product specific output dir from an Android build tree. Typically this location 88 * contains images for various device partitions, bootloader, radio and so on. 89 * 90 * <p>Note: the method does not guarantee that this path exists. 91 * 92 * @return the location of the output dir or <code>null</code> if the current build is not 93 */ 94 public static File getProductOutputDir() { 95 String path = singleton.getEnv(ENV_ANDROID_PRODUCT_OUT); 96 if (path == null) { 97 return null; 98 } else { 99 return new File(path); 100 } 101 } 102 } 103