1 /*
2  * Copyright (C) 2023 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.adservices.shared.testing.common;
17 
18 import static com.android.adservices.shared.util.LogUtil.DEBUG;
19 
20 import android.os.Environment;
21 
22 import com.android.adservices.shared.util.LogUtil;
23 
24 import java.io.File;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 
29 /** Provides helpers for file-related operations. */
30 public final class FileHelper {
31     private static final String SD_CARD_DIR = "/sdcard";
32     private static final String ADSERVICES_TEST_DIR =
33             Environment.DIRECTORY_DOCUMENTS + "/adservices-tests";
34 
35     // TODO(b/313646338): add unit tests
36     /** Writes a text file to {@link #getAdServicesTestsOutputDir()}. */
writeFile(String filename, String contents)37     public static void writeFile(String filename, String contents) {
38         String userFriendlyFilename = filename;
39         try {
40             File dir = getAdServicesTestsOutputDir();
41             Path filePath = Paths.get(dir.getAbsolutePath(), filename);
42             userFriendlyFilename = filePath.toString();
43             LogUtil.i("Creating file %s", userFriendlyFilename);
44             Files.createFile(filePath);
45             byte[] bytes = contents.getBytes();
46             if (DEBUG) {
47                 LogUtil.d("Writing %s bytes to %s", bytes.length, filePath);
48             }
49             Files.write(filePath, bytes);
50             if (DEBUG) {
51                 LogUtil.d("Saul Goodman!");
52             }
53         } catch (Exception e) {
54             LogUtil.e(e, "Failed to save %s", userFriendlyFilename);
55         }
56     }
57 
58     // TODO(b/313646338): add unit tests
59     /**
60      * Writes a file to {@value #SD_CARD_DIR} under {@value #ADSERVICES_TEST_DIR}
61      *
62      * <p>NOTE: add a {@code com.android.tradefed.device.metric.FilePullerLogCollector} in the test
63      * manifest, pointing to {@code /sdcard/Documents/adservices-tests}, so tests written to this
64      * directory surface as artifacts when the test fails in the cloud.
65      */
getAdServicesTestsOutputDir()66     public static File getAdServicesTestsOutputDir() {
67         String path = SD_CARD_DIR + "/" + ADSERVICES_TEST_DIR;
68         File dir = new File(path);
69         if (dir.exists()) {
70             return dir;
71         }
72         LogUtil.d("Directory %s doesn't exist, creating it", path);
73         if (dir.mkdirs()) {
74             LogUtil.i("Created directory %s", path);
75             return dir;
76         }
77         throw new IllegalStateException("Could not create directory " + path);
78     }
79 
FileHelper()80     private FileHelper() {
81         throw new UnsupportedOperationException("Provides only static methods");
82     }
83 }
84