blob: 236b4b4da37132cbe2e7aa998f2f7aa778e2c399 [file] [log] [blame]
[email protected]0ac38352013-11-13 06:01:381// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dcheng4af48582016-04-19 00:29:355#include <memory>
6
[email protected]0ac38352013-11-13 06:01:387#include "base/environment.h"
8#include "base/files/file_path.h"
[email protected]0ac38352013-11-13 06:01:389#include "chrome/common/env_vars.h"
10#include "chrome/common/logging_chrome.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13class ChromeLoggingTest : public testing::Test {
14 public:
15 // Stores the current value of the log file name environment
16 // variable and sets the variable to new_value.
ki.stfuf38f9312015-09-27 14:44:3717 void SaveEnvironmentVariable(const std::string& new_value) {
dcheng4af48582016-04-19 00:29:3518 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]0ac38352013-11-13 06:01:3819 if (!env->GetVar(env_vars::kLogFileName, &environment_filename_))
20 environment_filename_ = "";
21
22 env->SetVar(env_vars::kLogFileName, new_value);
23 }
24
25 // Restores the value of the log file nave environment variable
26 // previously saved by SaveEnvironmentVariable().
27 void RestoreEnvironmentVariable() {
dcheng4af48582016-04-19 00:29:3528 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]0ac38352013-11-13 06:01:3829 env->SetVar(env_vars::kLogFileName, environment_filename_);
30 }
31
32 private:
33 std::string environment_filename_; // Saves real environment value.
34};
35
36// Tests the log file name getter without an environment variable.
37TEST_F(ChromeLoggingTest, LogFileName) {
38 SaveEnvironmentVariable(std::string());
39
40 base::FilePath filename = logging::GetLogFileName();
41 ASSERT_NE(base::FilePath::StringType::npos,
42 filename.value().find(FILE_PATH_LITERAL("chrome_debug.log")));
43
44 RestoreEnvironmentVariable();
45}
46
47// Tests the log file name getter with an environment variable.
48TEST_F(ChromeLoggingTest, EnvironmentLogFileName) {
49 SaveEnvironmentVariable("test value");
50
51 base::FilePath filename = logging::GetLogFileName();
52 ASSERT_EQ(base::FilePath(FILE_PATH_LITERAL("test value")).value(),
53 filename.value());
54
55 RestoreEnvironmentVariable();
56}