[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 5 | #include "net/test/python_utils.h" |
| 6 | |
| 7 | #include <memory> |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include "base/command_line.h" |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 11 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
[email protected] | 66c5314 | 2013-07-23 14:08:52 | [diff] [blame] | 13 | #include "base/process/launch.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/strings/stringprintf.h" |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
pcc | f54b98f | 2016-11-14 23:21:47 | [diff] [blame] | 18 | TEST(PythonUtils, Clear) { |
| 19 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 20 | env->SetVar(kPythonPathEnv, "foo"); |
| 21 | EXPECT_TRUE(env->HasVar(kPythonPathEnv)); |
| 22 | |
| 23 | ClearPythonPath(); |
| 24 | EXPECT_FALSE(env->HasVar(kPythonPathEnv)); |
| 25 | } |
| 26 | |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 27 | TEST(PythonUtils, Append) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 28 | const base::FilePath::CharType kAppendDir1[] = |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 29 | FILE_PATH_LITERAL("test/path_append1"); |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 30 | const base::FilePath::CharType kAppendDir2[] = |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 31 | FILE_PATH_LITERAL("test/path_append2"); |
| 32 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 33 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 34 | |
| 35 | std::string python_path; |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 36 | base::FilePath append_path1(kAppendDir1); |
| 37 | base::FilePath append_path2(kAppendDir2); |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 38 | |
| 39 | // Get a clean start |
| 40 | env->UnSetVar(kPythonPathEnv); |
| 41 | |
| 42 | // Append the path |
| 43 | AppendToPythonPath(append_path1); |
| 44 | env->GetVar(kPythonPathEnv, &python_path); |
| 45 | ASSERT_EQ(python_path, "test/path_append1"); |
| 46 | |
| 47 | // Append the safe path again, nothing changes |
| 48 | AppendToPythonPath(append_path2); |
| 49 | env->GetVar(kPythonPathEnv, &python_path); |
| 50 | #if defined(OS_WIN) |
| 51 | ASSERT_EQ(std::string("test/path_append1;test/path_append2"), python_path); |
| 52 | #elif defined(OS_POSIX) |
| 53 | ASSERT_EQ(std::string("test/path_append1:test/path_append2"), python_path); |
| 54 | #endif |
| 55 | } |
| 56 | |
Sergey Ulanov | ec1d3de | 2017-09-19 19:27:47 | [diff] [blame] | 57 | TEST(PythonUtils, PythonRunTime) { |
[email protected] | 7ceb3588 | 2014-06-03 00:01:07 | [diff] [blame] | 58 | base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM); |
[email protected] | 2bbc7f2 | 2012-09-12 21:53:15 | [diff] [blame] | 59 | EXPECT_TRUE(GetPythonCommand(&cmd_line)); |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 60 | |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 61 | // Run a python command to print a string and make sure the output is what |
| 62 | // we want. |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 63 | cmd_line.AppendArg("-c"); |
| 64 | std::string input("PythonUtilsTest"); |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 65 | std::string python_cmd = base::StringPrintf("print '%s';", input.c_str()); |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 66 | cmd_line.AppendArg(python_cmd); |
| 67 | std::string output; |
| 68 | EXPECT_TRUE(base::GetAppOutput(cmd_line, &output)); |
tfarina | 023b1dcc | 2015-12-06 13:25:41 | [diff] [blame] | 69 | base::TrimWhitespaceASCII(output, base::TRIM_TRAILING, &output); |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 70 | EXPECT_EQ(input, output); |
| 71 | } |