blob: 865005a2b687a1e9b2b514c029fc57f9c2810b4e [file] [log] [blame]
Julio Merinoe3e3bfa2016-12-08 22:22:12 +00001// 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
lpino233b72d2017-07-05 11:08:40 -040015#include "src/main/cpp/workspace_layout.h"
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000016
17#include <fcntl.h>
18
lpino233b72d2017-07-05 11:08:40 -040019#include <memory>
20
21#include "src/main/cpp/blaze_util_platform.h"
22#include "src/main/cpp/util/file.h"
ccalvarinac69da02018-06-05 15:27:26 -070023#include "src/main/cpp/util/path.h"
ccalvarin8e9f4a82018-03-23 08:19:37 -070024#include "googletest/include/gtest/gtest.h"
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000025
26namespace blaze {
27
28class WorkspaceLayoutTest : public ::testing::Test {
29 protected:
Laszlo Csomor0d107492019-03-13 09:04:27 -070030 WorkspaceLayoutTest()
31 : build_root_(blaze_util::JoinPath(blaze::GetPathEnv("TEST_TMPDIR"),
32 "build_root")),
33 workspace_layout_(new WorkspaceLayout()) {}
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000034
lpino233b72d2017-07-05 11:08:40 -040035 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 Merinoe3e3bfa2016-12-08 22:22:12 +000039 }
40
lpino233b72d2017-07-05 11:08:40 -040041 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 Merinoe3e3bfa2016-12-08 22:22:12 +000051 }
52
lpino233b72d2017-07-05 11:08:40 -040053 const std::string build_root_;
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000054 const std::unique_ptr<WorkspaceLayout> workspace_layout_;
55};
56
57TEST_F(WorkspaceLayoutTest, GetWorkspace) {
58 // "" is returned when there's no workspace path.
lpino233b72d2017-07-05 11:08:40 -040059 std::string cwd = "foo/bar";
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000060 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
lpino233b72d2017-07-05 11:08:40 -040067 cwd = blaze_util::JoinPath(build_root_, cwd);
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000068 ASSERT_EQ(build_root_, workspace_layout_->GetWorkspace(cwd));
69}
70
Julio Merinoe3e3bfa2016-12-08 22:22:12 +000071} // namespace blaze