Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 15 | #include "src/main/cpp/workspace_layout.h" |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 16 | |
| 17 | #include <fcntl.h> |
| 18 | |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
| 21 | #include "src/main/cpp/blaze_util_platform.h" |
| 22 | #include "src/main/cpp/util/file.h" |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 23 | #include "src/main/cpp/util/path.h" |
ccalvarin | 8e9f4a8 | 2018-03-23 08:19:37 -0700 | [diff] [blame] | 24 | #include "googletest/include/gtest/gtest.h" |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 25 | |
| 26 | namespace blaze { |
| 27 | |
| 28 | class WorkspaceLayoutTest : public ::testing::Test { |
| 29 | protected: |
Laszlo Csomor | 0d10749 | 2019-03-13 09:04:27 -0700 | [diff] [blame] | 30 | WorkspaceLayoutTest() |
| 31 | : build_root_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"), |
| 32 | "build_root")), |
| 33 | workspace_layout_(new WorkspaceLayout()) {} |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 34 | |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 35 | void SetUp() override { |
| 36 | ASSERT_TRUE(blaze_util::MakeDirectories(build_root_, 0755)); |
| 37 | ASSERT_TRUE(blaze_util::WriteFile( |
| 38 | "", blaze_util::JoinPath(build_root_, "WORKSPACE"), 0755)); |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 39 | } |
| 40 | |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 41 | void TearDown() override { |
| 42 | // TODO(bazel-team): The code below deletes all the files in the workspace |
| 43 | // but it intentionally skips directories. As a consequence, there may be |
| 44 | // empty directories from test to test. Remove this once |
| 45 | // blaze_util::DeleteDirectories(path) exists. |
| 46 | std::vector<std::string> files_in_workspace; |
| 47 | blaze_util::GetAllFilesUnder(build_root_, &files_in_workspace); |
| 48 | for (const std::string& file : files_in_workspace) { |
| 49 | blaze_util::UnlinkPath(file); |
| 50 | } |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 51 | } |
| 52 | |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 53 | const std::string build_root_; |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 54 | const std::unique_ptr<WorkspaceLayout> workspace_layout_; |
| 55 | }; |
| 56 | |
| 57 | TEST_F(WorkspaceLayoutTest, GetWorkspace) { |
| 58 | // "" is returned when there's no workspace path. |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 59 | std::string cwd = "foo/bar"; |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 60 | ASSERT_EQ("", workspace_layout_->GetWorkspace(cwd)); |
| 61 | ASSERT_FALSE(workspace_layout_->InWorkspace(cwd)); |
| 62 | |
| 63 | cwd = build_root_; |
| 64 | ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd)); |
| 65 | ASSERT_TRUE(workspace_layout_->InWorkspace(build_root_)); |
| 66 | |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 67 | cwd = blaze_util::JoinPath(build_root_, cwd); |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 68 | ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd)); |
| 69 | } |
| 70 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 71 | } // namespace blaze |