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 | |
| 15 | #include "third_party/bazel/src/main/cpp/workspace_layout.h" |
| 16 | |
| 17 | #include <fcntl.h> |
| 18 | |
| 19 | #include "file/base/file.h" |
| 20 | #include "file/base/filesystem.h" |
| 21 | #include "file/base/helpers.h" |
| 22 | #include "file/base/path.h" |
| 23 | #include "strings/strcat.h" |
| 24 | #include "gtest/gtest.h" |
| 25 | #include "third_party/bazel/src/main/cpp/util/file.h" |
| 26 | |
| 27 | namespace blaze { |
| 28 | |
| 29 | class WorkspaceLayoutTest : public ::testing::Test { |
| 30 | protected: |
| 31 | WorkspaceLayoutTest() : workspace_layout_(new WorkspaceLayout()) {} |
| 32 | |
| 33 | void SetUp() { |
| 34 | build_root_ = file::JoinPath(FLAGS_test_tmpdir, "build_root"); |
| 35 | CHECK_OK(RecursivelyCreateDir(build_root_, file::Defaults())); |
| 36 | CHECK_OK(file::SetContents( |
| 37 | file::JoinPath(build_root_, "WORKSPACE"), "", file::Defaults())); |
| 38 | |
| 39 | // Create fake javac so that Blaze can find the javabase |
| 40 | string javac = file::JoinPath(FLAGS_test_tmpdir, "javac"); |
| 41 | CHECK_OK(file::SetContents(javac, "", file::Defaults())); |
| 42 | CHECK_GE(chmod(javac.c_str(), 0755), 0); |
| 43 | |
| 44 | string path(getenv("PATH")); |
| 45 | string test_tmpdir(getenv("TEST_TMPDIR")); |
| 46 | path = test_tmpdir + ":" + path; |
| 47 | setenv("PATH", path.c_str(), 1); |
| 48 | } |
| 49 | |
| 50 | void TearDown() { |
| 51 | file::RecursivelyDelete(build_root_, file::Defaults()).IgnoreError(); |
| 52 | } |
| 53 | |
| 54 | string build_root_; |
| 55 | const std::unique_ptr<WorkspaceLayout> workspace_layout_; |
| 56 | }; |
| 57 | |
| 58 | TEST_F(WorkspaceLayoutTest, GetWorkspace) { |
| 59 | // "" is returned when there's no workspace path. |
| 60 | string cwd = "foo/bar"; |
| 61 | ASSERT_EQ("", workspace_layout_->GetWorkspace(cwd)); |
| 62 | ASSERT_FALSE(workspace_layout_->InWorkspace(cwd)); |
| 63 | |
| 64 | cwd = build_root_; |
| 65 | ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd)); |
| 66 | ASSERT_TRUE(workspace_layout_->InWorkspace(build_root_)); |
| 67 | |
| 68 | cwd = file::JoinPath(build_root_, "foo/bar"); |
| 69 | ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd)); |
| 70 | } |
| 71 | |
| 72 | } // namespace blaze |