[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 | |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 5 | #include <string> |
| 6 | |
| 7 | #include "base/command_line.h" |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 8 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 11 | #include "base/process_util.h" |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/stringprintf.h" |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 14 | #include "net/test/python_utils.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | TEST(PythonUtils, Append) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 18 | const base::FilePath::CharType kAppendDir1[] = |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 19 | FILE_PATH_LITERAL("test/path_append1"); |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 20 | const base::FilePath::CharType kAppendDir2[] = |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 21 | FILE_PATH_LITERAL("test/path_append2"); |
| 22 | |
| 23 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 24 | |
| 25 | std::string python_path; |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 26 | base::FilePath append_path1(kAppendDir1); |
| 27 | base::FilePath append_path2(kAppendDir2); |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 28 | |
| 29 | // Get a clean start |
| 30 | env->UnSetVar(kPythonPathEnv); |
| 31 | |
| 32 | // Append the path |
| 33 | AppendToPythonPath(append_path1); |
| 34 | env->GetVar(kPythonPathEnv, &python_path); |
| 35 | ASSERT_EQ(python_path, "test/path_append1"); |
| 36 | |
| 37 | // Append the safe path again, nothing changes |
| 38 | AppendToPythonPath(append_path2); |
| 39 | env->GetVar(kPythonPathEnv, &python_path); |
| 40 | #if defined(OS_WIN) |
| 41 | ASSERT_EQ(std::string("test/path_append1;test/path_append2"), python_path); |
| 42 | #elif defined(OS_POSIX) |
| 43 | ASSERT_EQ(std::string("test/path_append1:test/path_append2"), python_path); |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | TEST(PythonUtils, PythonRunTime) { |
[email protected] | 2bbc7f2 | 2012-09-12 21:53:15 | [diff] [blame] | 48 | CommandLine cmd_line(CommandLine::NO_PROGRAM); |
| 49 | EXPECT_TRUE(GetPythonCommand(&cmd_line)); |
[email protected] | 9af13ce | 2010-09-16 21:45:48 | [diff] [blame] | 50 | |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 51 | // Run a python command to print a string and make sure the output is what |
| 52 | // we want. |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 53 | cmd_line.AppendArg("-c"); |
| 54 | std::string input("PythonUtilsTest"); |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame^] | 55 | std::string python_cmd = base::StringPrintf("print '%s';", input.c_str()); |
[email protected] | 874efe0 | 2010-10-05 18:07:38 | [diff] [blame] | 56 | cmd_line.AppendArg(python_cmd); |
| 57 | std::string output; |
| 58 | EXPECT_TRUE(base::GetAppOutput(cmd_line, &output)); |
| 59 | TrimWhitespace(output, TRIM_TRAILING, &output); |
| 60 | EXPECT_EQ(input, output); |
| 61 | } |