1 /*
2 * Copyright (C) 2021 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 #include "tools.h"
18
19 #include <algorithm>
20 #include <filesystem>
21 #include <iterator>
22
23 #include "android-base/file.h"
24 #include "base/common_art_test.h"
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27
28 namespace art {
29 namespace tools {
30 namespace {
31
32 using ::android::base::WriteStringToFile;
33 using ::testing::UnorderedElementsAre;
34
CreateFile(const std::string & filename)35 void CreateFile(const std::string& filename) {
36 std::filesystem::path path(filename);
37 std::filesystem::create_directories(path.parent_path());
38 ASSERT_TRUE(WriteStringToFile(/*content=*/"", filename));
39 }
40
41 class ArtToolsTest : public CommonArtTest {
42 protected:
SetUp()43 void SetUp() override {
44 CommonArtTest::SetUp();
45 scratch_dir_ = std::make_unique<ScratchDir>();
46 scratch_path_ = scratch_dir_->GetPath();
47 // Remove the trailing '/';
48 scratch_path_.resize(scratch_path_.length() - 1);
49 }
50
TearDown()51 void TearDown() override {
52 scratch_dir_.reset();
53 CommonArtTest::TearDown();
54 }
55
56 std::unique_ptr<ScratchDir> scratch_dir_;
57 std::string scratch_path_;
58 };
59
TEST_F(ArtToolsTest,Glob)60 TEST_F(ArtToolsTest, Glob) {
61 CreateFile(scratch_path_ + "/abc/def/000.txt");
62 CreateFile(scratch_path_ + "/abc/def/ghi/123.txt");
63 CreateFile(scratch_path_ + "/abc/def/ghi/456.txt");
64 CreateFile(scratch_path_ + "/abc/def/ghi/456.pdf");
65 CreateFile(scratch_path_ + "/abc/def/ghi/jkl/456.txt");
66 CreateFile(scratch_path_ + "/789.txt");
67 CreateFile(scratch_path_ + "/abc/789.txt");
68 CreateFile(scratch_path_ + "/abc/aaa/789.txt");
69 CreateFile(scratch_path_ + "/abc/aaa/bbb/789.txt");
70 CreateFile(scratch_path_ + "/abc/mno/123.txt");
71 CreateFile(scratch_path_ + "/abc/aaa/mno/123.txt");
72 CreateFile(scratch_path_ + "/abc/aaa/bbb/mno/123.txt");
73 CreateFile(scratch_path_ + "/abc/aaa/bbb/mno/ccc/123.txt");
74 CreateFile(scratch_path_ + "/pqr/123.txt");
75 CreateFile(scratch_path_ + "/abc/pqr/123.txt");
76 CreateFile(scratch_path_ + "/abc/aaa/pqr/123.txt");
77 CreateFile(scratch_path_ + "/abc/aaa/bbb/pqr/123.txt");
78 CreateFile(scratch_path_ + "/abc/aaa/bbb/pqr/ccc/123.txt");
79 CreateFile(scratch_path_ + "/abc/aaa/bbb/pqr/ccc/ddd/123.txt");
80
81 // This symlink will cause infinite recursion. It should not be followed.
82 std::filesystem::create_directory_symlink(scratch_path_ + "/abc/aaa/bbb/pqr",
83 scratch_path_ + "/abc/aaa/bbb/pqr/lnk");
84
85 // This is a directory. It should not be included in the results.
86 std::filesystem::create_directory(scratch_path_ + "/abc/def/ghi/000.txt");
87
88 std::vector<std::string> patterns = {
89 scratch_path_ + "/abc/def/000.txt",
90 scratch_path_ + "/abc/def/ghi/*.txt",
91 scratch_path_ + "/abc/**/789.txt",
92 scratch_path_ + "/abc/**/mno/*.txt",
93 scratch_path_ + "/abc/**/pqr/**",
94 };
95
96 EXPECT_THAT(Glob(patterns, scratch_path_),
97 UnorderedElementsAre(scratch_path_ + "/abc/def/000.txt",
98 scratch_path_ + "/abc/def/ghi/123.txt",
99 scratch_path_ + "/abc/def/ghi/456.txt",
100 scratch_path_ + "/abc/789.txt",
101 scratch_path_ + "/abc/aaa/789.txt",
102 scratch_path_ + "/abc/aaa/bbb/789.txt",
103 scratch_path_ + "/abc/mno/123.txt",
104 scratch_path_ + "/abc/aaa/mno/123.txt",
105 scratch_path_ + "/abc/aaa/bbb/mno/123.txt",
106 scratch_path_ + "/abc/pqr/123.txt",
107 scratch_path_ + "/abc/aaa/pqr/123.txt",
108 scratch_path_ + "/abc/aaa/bbb/pqr/123.txt",
109 scratch_path_ + "/abc/aaa/bbb/pqr/ccc/123.txt",
110 scratch_path_ + "/abc/aaa/bbb/pqr/ccc/ddd/123.txt"));
111 }
112
113 } // namespace
114 } // namespace tools
115 } // namespace art
116