Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +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 <stdlib.h> |
| 16 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 17 | #include "src/main/cpp/startup_options.h" |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | namespace blaze { |
| 21 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 22 | class StartupOptionsTest : public ::testing::Test { |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 23 | protected: |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 24 | StartupOptionsTest() = default; |
| 25 | ~StartupOptionsTest() = default; |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 26 | |
| 27 | void SetUp() override { |
| 28 | // This knowingly ignores the possibility of these environment variables |
| 29 | // being unset because we expect our test runner to set them in all cases. |
| 30 | // Otherwise, we'll crash here, but this keeps our code simpler. |
| 31 | old_home_ = getenv("HOME"); |
| 32 | old_test_tmpdir_ = getenv("TEST_TMPDIR"); |
| 33 | } |
| 34 | |
| 35 | void TearDown() override { |
| 36 | setenv("HOME", old_home_.c_str(), 1); |
| 37 | setenv("TEST_TMPDIR", old_test_tmpdir_.c_str(), 1); |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | std::string old_home_; |
| 42 | std::string old_test_tmpdir_; |
| 43 | }; |
| 44 | |
Julio Merino | 41b5688 | 2016-09-15 13:48:10 +0000 | [diff] [blame] | 45 | TEST_F(StartupOptionsTest, ProductName) { |
| 46 | blaze::StartupOptions startup_options; |
| 47 | ASSERT_EQ("Bazel", startup_options.product_name); |
| 48 | } |
| 49 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 50 | TEST_F(StartupOptionsTest, OutputRootPreferTestTmpdirIfSet) { |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 51 | setenv("HOME", "/nonexistent/home", 1); |
| 52 | setenv("TEST_TMPDIR", "/nonexistent/tmpdir", 1); |
| 53 | |
Julio Merino | 41b5688 | 2016-09-15 13:48:10 +0000 | [diff] [blame] | 54 | blaze::StartupOptions startup_options; |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 55 | ASSERT_EQ("/nonexistent/tmpdir", startup_options.output_root); |
| 56 | } |
| 57 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 58 | TEST_F(StartupOptionsTest, OutputRootUseHomeDirectory) { |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 59 | setenv("HOME", "/nonexistent/home", 1); |
| 60 | unsetenv("TEST_TMPDIR"); |
| 61 | |
Julio Merino | 41b5688 | 2016-09-15 13:48:10 +0000 | [diff] [blame] | 62 | blaze::StartupOptions startup_options; |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 63 | ASSERT_EQ("/nonexistent/home/.cache/bazel", startup_options.output_root); |
| 64 | } |
| 65 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 66 | TEST_F(StartupOptionsTest, OutputRootUseBuiltin) { |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 67 | // We cannot just unsetenv("HOME") because the logic to compute the output |
| 68 | // root falls back to using the passwd database if HOME is null... and mocking |
| 69 | // that out is hard. |
| 70 | setenv("HOME", "", 1); |
| 71 | unsetenv("TEST_TMPDIR"); |
| 72 | |
Julio Merino | 41b5688 | 2016-09-15 13:48:10 +0000 | [diff] [blame] | 73 | blaze::StartupOptions startup_options; |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 74 | ASSERT_EQ("/tmp", startup_options.output_root); |
| 75 | } |
| 76 | |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 77 | TEST_F(StartupOptionsTest, IsNullaryTest) { |
| 78 | blaze::StartupOptions startup_options; |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 79 | EXPECT_TRUE(startup_options.IsNullary("--master_bazelrc")); |
| 80 | EXPECT_TRUE(startup_options.IsNullary("--nomaster_bazelrc")); |
| 81 | EXPECT_FALSE(startup_options.IsNullary("")); |
| 82 | EXPECT_FALSE(startup_options.IsNullary("--")); |
| 83 | EXPECT_FALSE(startup_options.IsNullary("--master_bazelrcascasc")); |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 84 | string error_msg = std::string("In argument '--master_bazelrc=foo': option ") |
| 85 | + std::string("'--master_bazelrc' does not take a value"); |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 86 | EXPECT_DEATH(startup_options.IsNullary("--master_bazelrc=foo"), |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 87 | error_msg.c_str()); |
| 88 | } |
| 89 | |
| 90 | TEST_F(StartupOptionsTest, IsUnaryTest) { |
| 91 | blaze::StartupOptions startup_options; |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 92 | EXPECT_FALSE(startup_options.IsUnary("")); |
| 93 | EXPECT_FALSE(startup_options.IsUnary("--")); |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 94 | |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 95 | EXPECT_TRUE(startup_options.IsUnary("--blazerc=foo")); |
| 96 | EXPECT_TRUE(startup_options.IsUnary("--blazerc")); |
| 97 | EXPECT_TRUE(startup_options.IsUnary("--blazerc=")); |
| 98 | EXPECT_TRUE(startup_options.IsUnary("--blazerc")); |
| 99 | EXPECT_FALSE(startup_options.IsUnary("--blazercfooblah")); |
Luis Fernando Pino Duque | 8949c07 | 2016-10-28 15:17:22 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Julio Merino | 69a8d72 | 2016-09-13 22:29:39 +0000 | [diff] [blame] | 102 | } // namespace blaze |