xref: /aosp_15_r20/external/cronet/net/test/python_utils_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/test/python_utils.h"
6 
7 #include <memory>
8 #include <string>
9 
10 #include "base/command_line.h"
11 #include "base/environment.h"
12 #include "base/files/file_path.h"
13 #include "base/process/launch.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "build/build_config.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
TEST(PythonUtils,SetPythonPathInEnvironment)19 TEST(PythonUtils, SetPythonPathInEnvironment) {
20   base::EnvironmentMap env;
21   SetPythonPathInEnvironment({base::FilePath(FILE_PATH_LITERAL("test/path1")),
22                               base::FilePath(FILE_PATH_LITERAL("test/path2"))},
23                              &env);
24 #if BUILDFLAG(IS_WIN)
25   EXPECT_EQ(FILE_PATH_LITERAL("test/path1;test/path2"),
26             env[FILE_PATH_LITERAL("PYTHONPATH")]);
27 #else
28   EXPECT_EQ("test/path1:test/path2", env["PYTHONPATH"]);
29 #endif
30   EXPECT_NE(env.end(), env.find(FILE_PATH_LITERAL("VPYTHON_CLEAR_PYTHONPATH")));
31   EXPECT_EQ(base::NativeEnvironmentString(),
32             env[FILE_PATH_LITERAL("VPYTHON_CLEAR_PYTHONPATH")]);
33 }
34 
TEST(PythonUtils,Python3RunTime)35 TEST(PythonUtils, Python3RunTime) {
36   base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM);
37   EXPECT_TRUE(GetPython3Command(&cmd_line));
38 
39   // Run a python command to print a string and make sure the output is what
40   // we want.
41   cmd_line.AppendArg("-c");
42   std::string input("PythonUtilsTest");
43   std::string python_cmd = base::StringPrintf("print('%s');", input.c_str());
44   cmd_line.AppendArg(python_cmd);
45   std::string output;
46   EXPECT_TRUE(base::GetAppOutput(cmd_line, &output));
47   base::TrimWhitespaceASCII(output, base::TRIM_TRAILING, &output);
48   EXPECT_EQ(input, output);
49 }
50