kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 10 | |
| 11 | #include <cstdio> |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 12 | #include "fileutils.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | #ifdef WIN32 |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 16 | #include <direct.h> |
| 17 | #define GET_CURRENT_DIR _getcwd |
| 18 | static const char* kPathDelimiter = "\\"; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 19 | #else |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | #define GET_CURRENT_DIR getcwd |
| 22 | static const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 28 | // Test fixture to restore the working directory between each test, since some |
| 29 | // of them change it with chdir during execution (not restored by the |
| 30 | // gtest framework). |
| 31 | class FileUtilsTest: public testing::Test { |
| 32 | protected: |
| 33 | FileUtilsTest() { |
| 34 | original_working_dir_ = GetWorkingDir(); |
| 35 | } |
| 36 | virtual ~FileUtilsTest() {} |
| 37 | void SetUp() { |
| 38 | chdir(original_working_dir_.c_str()); |
| 39 | } |
| 40 | void TearDown() {} |
| 41 | private: |
| 42 | std::string original_working_dir_; |
| 43 | static std::string GetWorkingDir() { |
| 44 | char path_buffer[FILENAME_MAX]; |
| 45 | EXPECT_TRUE(GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) |
| 46 | << "Cannot get current working directory!"; |
| 47 | return std::string(path_buffer); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | // Tests that the project root path is returned for the default working |
| 52 | // directory that is automatically set when the test executable is launched. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 53 | // The test is not fully testing the implementation, since we cannot be sure |
| 54 | // of where the executable was launched from. |
| 55 | // The test will fail if the top level directory is not named "trunk". |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 56 | TEST_F(FileUtilsTest, ProjectRootPathFromUnchangedWorkingDir) { |
| 57 | std::string path = ProjectRootPath(); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 58 | std::string expected_end = "trunk"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 59 | expected_end = kPathDelimiter + expected_end + kPathDelimiter; |
| 60 | ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end)); |
| 61 | } |
| 62 | |
| 63 | // Similar to the above test, but for the output dir |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 64 | TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) { |
| 65 | std::string path = OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 66 | std::string expected_end = "out"; |
| 67 | expected_end = kPathDelimiter + expected_end + kPathDelimiter; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 68 | ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end)); |
| 69 | } |
| 70 | |
| 71 | // Tests setting the current working directory to a directory three levels |
| 72 | // deeper from the current one. Then testing that the project path returned |
| 73 | // is still the same, when the function under test is called again. |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 74 | TEST_F(FileUtilsTest, ProjectRootPathFromDeeperWorkingDir) { |
| 75 | std::string path = ProjectRootPath(); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 76 | std::string original_working_dir = path; // This is the correct project root |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 77 | // Change to a subdirectory path (the full path doesn't have to exist). |
| 78 | path += "foo/bar/baz"; |
| 79 | chdir(path.c_str()); |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 80 | ASSERT_EQ(original_working_dir, ProjectRootPath()); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 81 | } |
| 82 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 83 | // Similar to the above test, but for the output dir |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 84 | TEST_F(FileUtilsTest, OutputPathFromDeeperWorkingDir) { |
| 85 | std::string path = OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 86 | std::string original_working_dir = path; |
| 87 | path += "foo/bar/baz"; |
| 88 | chdir(path.c_str()); |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 89 | ASSERT_EQ(original_working_dir, OutputPath()); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 90 | } |
| 91 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 92 | // Tests with current working directory set to a directory higher up in the |
| 93 | // directory tree than the project root dir. This case shall return a specified |
| 94 | // error string as a directory (which will be an invalid path). |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 95 | TEST_F(FileUtilsTest, ProjectRootPathFromRootWorkingDir) { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 96 | // Change current working dir to the root of the current file system |
| 97 | // (this will always be "above" our project root dir). |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 98 | chdir(kPathDelimiter); |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 99 | ASSERT_EQ(kCannotFindProjectRootDir, ProjectRootPath()); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 100 | } |
| 101 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 102 | // Similar to the above test, but for the output dir |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 103 | TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) { |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 104 | chdir(kPathDelimiter); |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 105 | ASSERT_EQ("./", OutputPath()); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 106 | } |
| 107 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 108 | } // namespace test |
| 109 | } // namespace webrtc |