Merge "Write files in a temporary directory" am: efa03939f0 am: 49eec50a4a am: 28a89f1860

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1920049

Change-Id: I3e26413428dea2deaeaa25146c65fae71c688718
This commit is contained in:
Wei Su
2022-01-11 17:20:56 +00:00
committed by Automerger Merge Worker
2 changed files with 31 additions and 38 deletions

View File

@@ -19,12 +19,11 @@
#include "android-base/file.h" #include "android-base/file.h"
#include "android-base/stringprintf.h" #include "android-base/stringprintf.h"
#include "android-base/utf8.h" #include "android-base/utf8.h"
#include "format/proto/ProtoDeserialize.h"
#include "io/StringStream.h" #include "io/StringStream.h"
#include "io/ZipArchive.h" #include "io/ZipArchive.h"
#include "java/AnnotationProcessor.h" #include "java/AnnotationProcessor.h"
#include "test/Test.h" #include "test/Test.h"
#include "format/proto/ProtoDeserialize.h"
namespace aapt { namespace aapt {
@@ -59,55 +58,56 @@ TEST_F(CompilerTest, MultiplePeriods) {
std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()), const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()),
"integration-tests", "CompileTest", "res"}); "integration-tests", "CompileTest", "res"});
const std::string kOutDir = testing::TempDir();
// Resource files without periods in the file name should not throw errors // Resource files without periods in the file name should not throw errors
const std::string path0 = BuildPath({kResDir, "values", "values.xml"}); const std::string path0 = BuildPath({kResDir, "values", "values.xml"});
const std::string path0_out = BuildPath({kResDir, "values_values.arsc.flat"}); const std::string path0_out = BuildPath({kOutDir, "values_values.arsc.flat"});
::android::base::utf8::unlink(path0_out.c_str()); ::android::base::utf8::unlink(path0_out.c_str());
ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ false, diag), 0); ASSERT_EQ(TestCompile(path0, kOutDir, /** legacy */ false, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ true, diag), 0); ASSERT_EQ(TestCompile(path0, kOutDir, /** legacy */ true, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
const std::string path1 = BuildPath({kResDir, "drawable", "image.png"}); const std::string path1 = BuildPath({kResDir, "drawable", "image.png"});
const std::string path1_out = BuildPath({kResDir, "drawable_image.png.flat"}); const std::string path1_out = BuildPath({kOutDir, "drawable_image.png.flat"});
::android::base::utf8::unlink(path1_out.c_str()); ::android::base::utf8::unlink(path1_out.c_str());
ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ false, diag), 0); ASSERT_EQ(TestCompile(path1, kOutDir, /** legacy */ false, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ true, diag), 0); ASSERT_EQ(TestCompile(path1, kOutDir, /** legacy */ true, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
const std::string path2 = BuildPath({kResDir, "drawable", "image.9.png"}); const std::string path2 = BuildPath({kResDir, "drawable", "image.9.png"});
const std::string path2_out = BuildPath({kResDir, "drawable_image.9.png.flat"}); const std::string path2_out = BuildPath({kOutDir, "drawable_image.9.png.flat"});
::android::base::utf8::unlink(path2_out.c_str()); ::android::base::utf8::unlink(path2_out.c_str());
ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ false, diag), 0); ASSERT_EQ(TestCompile(path2, kOutDir, /** legacy */ false, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ true, diag), 0); ASSERT_EQ(TestCompile(path2, kOutDir, /** legacy */ true, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
// Resource files with periods in the file name should fail on non-legacy compilations // Resource files with periods in the file name should fail on non-legacy compilations
const std::string path3 = BuildPath({kResDir, "values", "values.all.xml"}); const std::string path3 = BuildPath({kResDir, "values", "values.all.xml"});
const std::string path3_out = BuildPath({kResDir, "values_values.all.arsc.flat"}); const std::string path3_out = BuildPath({kOutDir, "values_values.all.arsc.flat"});
::android::base::utf8::unlink(path3_out.c_str()); ::android::base::utf8::unlink(path3_out.c_str());
ASSERT_NE(TestCompile(path3, kResDir, /** legacy */ false, diag), 0); ASSERT_NE(TestCompile(path3, kOutDir, /** legacy */ false, diag), 0);
ASSERT_NE(::android::base::utf8::unlink(path3_out.c_str()), 0); ASSERT_NE(::android::base::utf8::unlink(path3_out.c_str()), 0);
ASSERT_EQ(TestCompile(path3, kResDir, /** legacy */ true, diag), 0); ASSERT_EQ(TestCompile(path3, kOutDir, /** legacy */ true, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path3_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path3_out.c_str()), 0);
const std::string path4 = BuildPath({kResDir, "drawable", "image.small.png"}); const std::string path4 = BuildPath({kResDir, "drawable", "image.small.png"});
const std::string path4_out = BuildPath({kResDir, "drawable_image.small.png.flat"}); const std::string path4_out = BuildPath({kOutDir, "drawable_image.small.png.flat"});
::android::base::utf8::unlink(path4_out.c_str()); ::android::base::utf8::unlink(path4_out.c_str());
ASSERT_NE(TestCompile(path4, kResDir, /** legacy */ false, diag), 0); ASSERT_NE(TestCompile(path4, kOutDir, /** legacy */ false, diag), 0);
ASSERT_NE(::android::base::utf8::unlink(path4_out.c_str()), 0); ASSERT_NE(::android::base::utf8::unlink(path4_out.c_str()), 0);
ASSERT_EQ(TestCompile(path4, kResDir, /** legacy */ true, diag), 0); ASSERT_EQ(TestCompile(path4, kOutDir, /** legacy */ true, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path4_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path4_out.c_str()), 0);
const std::string path5 = BuildPath({kResDir, "drawable", "image.small.9.png"}); const std::string path5 = BuildPath({kResDir, "drawable", "image.small.9.png"});
const std::string path5_out = BuildPath({kResDir, "drawable_image.small.9.png.flat"}); const std::string path5_out = BuildPath({kOutDir, "drawable_image.small.9.png.flat"});
::android::base::utf8::unlink(path5_out.c_str()); ::android::base::utf8::unlink(path5_out.c_str());
ASSERT_NE(TestCompile(path5, kResDir, /** legacy */ false, diag), 0); ASSERT_NE(TestCompile(path5, kOutDir, /** legacy */ false, diag), 0);
ASSERT_NE(::android::base::utf8::unlink(path5_out.c_str()), 0); ASSERT_NE(::android::base::utf8::unlink(path5_out.c_str()), 0);
ASSERT_EQ(TestCompile(path5, kResDir, /** legacy */ true, diag), 0); ASSERT_EQ(TestCompile(path5, kOutDir, /** legacy */ true, diag), 0);
ASSERT_EQ(::android::base::utf8::unlink(path5_out.c_str()), 0); ASSERT_EQ(::android::base::utf8::unlink(path5_out.c_str()), 0);
} }
@@ -116,9 +116,7 @@ TEST_F(CompilerTest, DirInput) {
std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()), const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()),
"integration-tests", "CompileTest", "DirInput", "res"}); "integration-tests", "CompileTest", "DirInput", "res"});
const std::string kOutputFlata = const std::string kOutputFlata = BuildPath({testing::TempDir(), "compiled.flata"});
BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests",
"CompileTest", "DirInput", "compiled.flata"});
::android::base::utf8::unlink(kOutputFlata.c_str()); ::android::base::utf8::unlink(kOutputFlata.c_str());
std::vector<android::StringPiece> args; std::vector<android::StringPiece> args;
@@ -147,9 +145,7 @@ TEST_F(CompilerTest, ZipInput) {
const std::string kResZip = const std::string kResZip =
BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests", BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests",
"CompileTest", "ZipInput", "res.zip"}); "CompileTest", "ZipInput", "res.zip"});
const std::string kOutputFlata = const std::string kOutputFlata = BuildPath({testing::TempDir(), "compiled.flata"});
BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests",
"CompileTest", "ZipInput", "compiled.flata"});
::android::base::utf8::unlink(kOutputFlata.c_str()); ::android::base::utf8::unlink(kOutputFlata.c_str());
@@ -257,9 +253,9 @@ TEST_F(CompilerTest, DoNotTranslateTest) {
TEST_F(CompilerTest, RelativePathTest) { TEST_F(CompilerTest, RelativePathTest) {
StdErrDiagnostics diag; StdErrDiagnostics diag;
const std::string res_path = BuildPath( const std::string res_path =
{android::base::Dirname(android::base::GetExecutablePath()), BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests",
"integration-tests", "CompileTest", "res"}); "CompileTest", "res"});
const std::string path_values_colors = GetTestPath("values/colors.xml"); const std::string path_values_colors = GetTestPath("values/colors.xml");
WriteFile(path_values_colors, "<resources>" WriteFile(path_values_colors, "<resources>"
@@ -272,9 +268,8 @@ TEST_F(CompilerTest, RelativePathTest) {
"<TextBox android:id=\"@+id/text_one\" android:background=\"@color/color_one\"/>" "<TextBox android:id=\"@+id/text_one\" android:background=\"@color/color_one\"/>"
"</LinearLayout>"); "</LinearLayout>");
const std::string compiled_files_dir = BuildPath( const std::string compiled_files_dir =
{android::base::Dirname(android::base::GetExecutablePath()), BuildPath({testing::TempDir(), "integration-tests", "CompileTest", "compiled"});
"integration-tests", "CompileTest", "compiled"});
CHECK(file::mkdirs(compiled_files_dir.data())); CHECK(file::mkdirs(compiled_files_dir.data()));
const std::string path_values_colors_out = const std::string path_values_colors_out =
@@ -283,9 +278,8 @@ TEST_F(CompilerTest, RelativePathTest) {
BuildPath({compiled_files_dir, "layout_layout_one.flat"}); BuildPath({compiled_files_dir, "layout_layout_one.flat"});
::android::base::utf8::unlink(path_values_colors_out.c_str()); ::android::base::utf8::unlink(path_values_colors_out.c_str());
::android::base::utf8::unlink(path_layout_layout_one_out.c_str()); ::android::base::utf8::unlink(path_layout_layout_one_out.c_str());
const std::string apk_path = BuildPath( const std::string apk_path =
{android::base::Dirname(android::base::GetExecutablePath()), BuildPath({testing::TempDir(), "integration-tests", "CompileTest", "out.apk"});
"integration-tests", "CompileTest", "out.apk"});
const std::string source_set_res = BuildPath({"main", "res"}); const std::string source_set_res = BuildPath({"main", "res"});
const std::string relative_path_values_colors = const std::string relative_path_values_colors =

View File

@@ -67,8 +67,7 @@ void ClearDirectory(const android::StringPiece& path) {
} }
void TestDirectoryFixture::SetUp() { void TestDirectoryFixture::SetUp() {
temp_dir_ = file::BuildPath({android::base::GetExecutableDirectory(), temp_dir_ = file::BuildPath({testing::TempDir(), "_temp",
"_temp",
testing::UnitTest::GetInstance()->current_test_case()->name(), testing::UnitTest::GetInstance()->current_test_case()->name(),
testing::UnitTest::GetInstance()->current_test_info()->name()}); testing::UnitTest::GetInstance()->current_test_info()->name()});
ASSERT_TRUE(file::mkdirs(temp_dir_)); ASSERT_TRUE(file::mkdirs(temp_dir_));