blob: 493256bda8c1b88cbe087a30a79a52fee4d5ade9 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
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.org4d8cd9d2011-11-09 11:24:14 +000010
11#include <cstdio>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000012#include "fileutils.h"
13#include "gtest/gtest.h"
14
15#ifdef WIN32
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000016#include <direct.h>
17#define GET_CURRENT_DIR _getcwd
18static const char* kPathDelimiter = "\\";
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000019#else
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000020#include <unistd.h>
21#define GET_CURRENT_DIR getcwd
22static const char* kPathDelimiter = "/";
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000023#endif
24
25namespace webrtc {
26namespace test {
27
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000028// 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).
31class 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.org7951e812011-10-13 12:24:41 +000053// 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.org0db7dc62011-11-13 01:34:05 +000056TEST_F(FileUtilsTest, ProjectRootPathFromUnchangedWorkingDir) {
57 std::string path = ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000058 std::string expected_end = "trunk";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000059 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.org0db7dc62011-11-13 01:34:05 +000064TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
65 std::string path = OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000066 std::string expected_end = "out";
67 expected_end = kPathDelimiter + expected_end + kPathDelimiter;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000068 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.org0db7dc62011-11-13 01:34:05 +000074TEST_F(FileUtilsTest, ProjectRootPathFromDeeperWorkingDir) {
75 std::string path = ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000076 std::string original_working_dir = path; // This is the correct project root
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000077 // 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.org0db7dc62011-11-13 01:34:05 +000080 ASSERT_EQ(original_working_dir, ProjectRootPath());
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000081}
82
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000083// Similar to the above test, but for the output dir
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000084TEST_F(FileUtilsTest, OutputPathFromDeeperWorkingDir) {
85 std::string path = OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000086 std::string original_working_dir = path;
87 path += "foo/bar/baz";
88 chdir(path.c_str());
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000089 ASSERT_EQ(original_working_dir, OutputPath());
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000090}
91
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000092// 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.org0db7dc62011-11-13 01:34:05 +000095TEST_F(FileUtilsTest, ProjectRootPathFromRootWorkingDir) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000096 // Change current working dir to the root of the current file system
97 // (this will always be "above" our project root dir).
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000098 chdir(kPathDelimiter);
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000099 ASSERT_EQ(kCannotFindProjectRootDir, ProjectRootPath());
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000100}
101
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000102// Similar to the above test, but for the output dir
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000103TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000104 chdir(kPathDelimiter);
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000105 ASSERT_EQ("./", OutputPath());
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000106}
107
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000108} // namespace test
109} // namespace webrtc