blob: ec89b6279f298035f12d38802e18c59306b8c63a [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]9af13ce2010-09-16 21:45:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakja9850e12016-04-18 22:28:085#include "net/test/python_utils.h"
6
7#include <memory>
[email protected]874efe02010-10-05 18:07:388#include <string>
9
10#include "base/command_line.h"
[email protected]9af13ce2010-09-16 21:45:4811#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
[email protected]66c53142013-07-23 14:08:5213#include "base/process/launch.h"
[email protected]4dc3ad4f2013-06-11 07:15:5014#include "base/strings/string_util.h"
15#include "base/strings/stringprintf.h"
[email protected]9af13ce2010-09-16 21:45:4816#include "testing/gtest/include/gtest/gtest.h"
17
pccf54b98f2016-11-14 23:21:4718TEST(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]9af13ce2010-09-16 21:45:4827TEST(PythonUtils, Append) {
[email protected]6cdfd7f2013-02-08 20:40:1528 const base::FilePath::CharType kAppendDir1[] =
[email protected]9af13ce2010-09-16 21:45:4829 FILE_PATH_LITERAL("test/path_append1");
[email protected]6cdfd7f2013-02-08 20:40:1530 const base::FilePath::CharType kAppendDir2[] =
[email protected]9af13ce2010-09-16 21:45:4831 FILE_PATH_LITERAL("test/path_append2");
32
danakja9850e12016-04-18 22:28:0833 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]9af13ce2010-09-16 21:45:4834
35 std::string python_path;
[email protected]6cdfd7f2013-02-08 20:40:1536 base::FilePath append_path1(kAppendDir1);
37 base::FilePath append_path2(kAppendDir2);
[email protected]9af13ce2010-09-16 21:45:4838
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 Ulanovec1d3de2017-09-19 19:27:4757TEST(PythonUtils, PythonRunTime) {
[email protected]7ceb35882014-06-03 00:01:0758 base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM);
[email protected]2bbc7f22012-09-12 21:53:1559 EXPECT_TRUE(GetPythonCommand(&cmd_line));
[email protected]9af13ce2010-09-16 21:45:4860
[email protected]874efe02010-10-05 18:07:3861 // Run a python command to print a string and make sure the output is what
62 // we want.
[email protected]874efe02010-10-05 18:07:3863 cmd_line.AppendArg("-c");
64 std::string input("PythonUtilsTest");
[email protected]7d3cbc92013-03-18 22:33:0465 std::string python_cmd = base::StringPrintf("print '%s';", input.c_str());
[email protected]874efe02010-10-05 18:07:3866 cmd_line.AppendArg(python_cmd);
67 std::string output;
68 EXPECT_TRUE(base::GetAppOutput(cmd_line, &output));
tfarina023b1dcc2015-12-06 13:25:4169 base::TrimWhitespaceASCII(output, base::TRIM_TRAILING, &output);
[email protected]874efe02010-10-05 18:07:3870 EXPECT_EQ(input, output);
71}