1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "testing/base/public/mozctest.h"
31 
32 #include "base/file_util.h"
33 #include "base/logging.h"
34 #include "base/system_util.h"
35 #include "base/util.h"
36 #include "testing/base/public/googletest.h"
37 
38 namespace mozc {
39 namespace testing {
40 
GetSourcePath(const std::vector<StringPiece> & components)41 string GetSourcePath(const std::vector<StringPiece> &components) {
42   std::vector<StringPiece> abs_components = {
43     FLAGS_test_srcdir,
44   };
45   abs_components.insert(abs_components.end(),
46                         components.begin(), components.end());
47   return FileUtil::JoinPath(abs_components);
48 }
49 
GetSourceFileOrDie(const std::vector<StringPiece> & components)50 string GetSourceFileOrDie(const std::vector<StringPiece> &components) {
51   const string path = GetSourcePath(components);
52   CHECK(FileUtil::FileExists(path)) << "File doesn't exist: " << path;
53   return path;
54 }
55 
GetSourceDirOrDie(const std::vector<StringPiece> & components)56 string GetSourceDirOrDie(const std::vector<StringPiece> &components) {
57   const string path = GetSourcePath(components);
58   CHECK(FileUtil::DirectoryExists(path)) << "Directory doesn't exist: " << path;
59   return path;
60 }
61 
GetSourceFilesInDirOrDie(const std::vector<StringPiece> & dir_components,const std::vector<StringPiece> & filenames)62 std::vector<string> GetSourceFilesInDirOrDie(
63     const std::vector<StringPiece> &dir_components,
64     const std::vector<StringPiece> &filenames) {
65   const string dir = GetSourceDirOrDie(dir_components);
66   std::vector<string> paths;
67   for (size_t i = 0; i < filenames.size(); ++i) {
68     paths.push_back(FileUtil::JoinPath({dir, filenames[i]}));
69     CHECK(FileUtil::FileExists(paths.back()))
70         << "File doesn't exist: " << paths.back();
71   }
72   return paths;
73 }
74 
ScopedTmpUserProfileDirectory()75 ScopedTmpUserProfileDirectory::ScopedTmpUserProfileDirectory()
76     : original_dir_(SystemUtil::GetUserProfileDirectory()) {
77   SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
78 }
79 
~ScopedTmpUserProfileDirectory()80 ScopedTmpUserProfileDirectory::~ScopedTmpUserProfileDirectory() {
81   SystemUtil::SetUserProfileDirectory(original_dir_);
82 }
83 
84 }  // namespace testing
85 }  // namespace mozc
86