• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.atest;
17 
18 import com.google.common.base.Strings;
19 
20 import java.io.File;
21 import java.nio.file.Files;
22 import java.nio.file.Paths;
23 
24 public class AtestUtils {
25 
26     private static final String EMPTY_STRING_ERROR = " can't be empty.\n";
27 
28     /**
29      * Checks if the directory contains test mapping file.
30      *
31      * @param path the directory to check.
32      * @return true if the path contains test mapping file.
33      */
hasTestMapping(String path)34     public static boolean hasTestMapping(String path) {
35         return Files.exists(Paths.get(path, Constants.TEST_MAPPING_FILE_NAME));
36     }
37 
38     /**
39      * Gets the Android root path by project path.
40      *
41      * @param projectPath the base path of user's project.
42      * @return the Android root path or null.
43      */
getAndroidRoot(String projectPath)44     public static String getAndroidRoot(String projectPath) {
45         File currentFolder = new File(projectPath);
46         File parentFolder = currentFolder.getParentFile();
47         File checkFolder = new File(currentFolder, Constants.BUILD_ENVIRONMENT);
48         while (parentFolder != null) {
49             if (checkFolder.exists()) {
50                 return currentFolder.getPath();
51             } else {
52                 currentFolder = parentFolder;
53                 parentFolder = currentFolder.getParentFile();
54                 checkFolder = new File(currentFolder, Constants.BUILD_ENVIRONMENT);
55             }
56         }
57         return null;
58     }
59 
60     /**
61      * Checks if there are any empty strings.
62      *
63      * @param targets strings to be checked.
64      * @return true if there are any empty arguments.
65      */
checkEmpty(String... targets)66     public static boolean checkEmpty(String... targets) {
67         for (String target : targets) {
68             if (Strings.isNullOrEmpty(target)) {
69                 return true;
70             }
71         }
72         return false;
73     }
74 
75     /**
76      * Gets error message from the Atest arguments.
77      *
78      * @param lunchTarget the lunch target.
79      * @param testTarget the Atest test target.
80      * @param workPath the work path to run the command.
81      * @return the error message shown for users.
82      */
checkError(String lunchTarget, String testTarget, String workPath)83     public static String checkError(String lunchTarget, String testTarget, String workPath) {
84         StringBuilder errorMessage = new StringBuilder("Please check:\n");
85         if (Strings.isNullOrEmpty(testTarget)) {
86             errorMessage.append("- Atest target" + EMPTY_STRING_ERROR);
87         }
88         if (Strings.isNullOrEmpty(lunchTarget)) {
89             errorMessage.append("- lunch target" + EMPTY_STRING_ERROR);
90         }
91         if (Strings.isNullOrEmpty(workPath)) {
92             errorMessage.append(
93                     "- Atest can only execute when your project is under Android "
94                             + "source directory.");
95         }
96         return errorMessage.toString();
97     }
98 }
99