python_utils_unittest.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2011 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. #include "net/test/python_utils.h"
  5. #include <memory>
  6. #include <string>
  7. #include "base/command_line.h"
  8. #include "base/environment.h"
  9. #include "base/files/file_path.h"
  10. #include "base/process/launch.h"
  11. #include "base/strings/string_util.h"
  12. #include "base/strings/stringprintf.h"
  13. #include "build/build_config.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. TEST(PythonUtils, SetPythonPathInEnvironment) {
  16. base::EnvironmentMap env;
  17. SetPythonPathInEnvironment({base::FilePath(FILE_PATH_LITERAL("test/path1")),
  18. base::FilePath(FILE_PATH_LITERAL("test/path2"))},
  19. &env);
  20. #if BUILDFLAG(IS_WIN)
  21. EXPECT_EQ(FILE_PATH_LITERAL("test/path1;test/path2"),
  22. env[FILE_PATH_LITERAL("PYTHONPATH")]);
  23. #else
  24. EXPECT_EQ("test/path1:test/path2", env["PYTHONPATH"]);
  25. #endif
  26. EXPECT_NE(env.end(), env.find(FILE_PATH_LITERAL("VPYTHON_CLEAR_PYTHONPATH")));
  27. EXPECT_EQ(base::NativeEnvironmentString(),
  28. env[FILE_PATH_LITERAL("VPYTHON_CLEAR_PYTHONPATH")]);
  29. }
  30. TEST(PythonUtils, PythonRunTime) {
  31. base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM);
  32. EXPECT_TRUE(GetPythonCommand(&cmd_line));
  33. // Run a python command to print a string and make sure the output is what
  34. // we want.
  35. cmd_line.AppendArg("-c");
  36. std::string input("PythonUtilsTest");
  37. std::string python_cmd = base::StringPrintf("print('%s');", input.c_str());
  38. cmd_line.AppendArg(python_cmd);
  39. std::string output;
  40. EXPECT_TRUE(base::GetAppOutput(cmd_line, &output));
  41. base::TrimWhitespaceASCII(output, base::TRIM_TRAILING, &output);
  42. EXPECT_EQ(input, output);
  43. }
  44. TEST(PythonUtils, Python3RunTime) {
  45. base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM);
  46. EXPECT_TRUE(GetPython3Command(&cmd_line));
  47. // Run a python command to print a string and make sure the output is what
  48. // we want.
  49. cmd_line.AppendArg("-c");
  50. std::string input("PythonUtilsTest");
  51. std::string python_cmd = base::StringPrintf("print('%s');", input.c_str());
  52. cmd_line.AppendArg(python_cmd);
  53. std::string output;
  54. EXPECT_TRUE(base::GetAppOutput(cmd_line, &output));
  55. base::TrimWhitespaceASCII(output, base::TRIM_TRAILING, &output);
  56. EXPECT_EQ(input, output);
  57. }