summaryrefslogtreecommitdiffstats
path: root/net/test
diff options
context:
space:
mode:
authorlzheng@chromium.org <lzheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-16 17:00:38 +0000
committerlzheng@chromium.org <lzheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-16 17:00:38 +0000
commit3b27adfc7ed75f2b5501039895ea9ec05f7266df (patch)
treee23d348bf776ee5efef5d7ebeff28ecacf1f21b0 /net/test
parentc22edd63437194368b171c30cd6be75ba035c2e5 (diff)
downloadchromium_src-3b27adfc7ed75f2b5501039895ea9ec05f7266df.zip
chromium_src-3b27adfc7ed75f2b5501039895ea9ec05f7266df.tar.gz
chromium_src-3b27adfc7ed75f2b5501039895ea9ec05f7266df.tar.bz2
Refactor test_server so some python related functions could be shared by other test servers.
BUG=none TEST=python_utils_unittest.cc Review URL: http://codereview.chromium.org/3366026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59671 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/test')
-rw-r--r--net/test/python_utils.cc51
-rw-r--r--net/test/python_utils.h23
-rw-r--r--net/test/python_utils_unittest.cc51
-rw-r--r--net/test/test_server.cc1
-rw-r--r--net/test/test_server.h3
-rw-r--r--net/test/test_server_posix.cc16
-rw-r--r--net/test/test_server_win.cc16
7 files changed, 126 insertions, 35 deletions
diff --git a/net/test/python_utils.cc b/net/test/python_utils.cc
new file mode 100644
index 0000000..5f574fe
--- /dev/null
+++ b/net/test/python_utils.cc
@@ -0,0 +1,51 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/test/python_utils.h"
+
+#include "base/base_paths.h"
+#include "base/environment.h"
+#include "base/file_path.h"
+#include "base/path_service.h"
+#include "base/scoped_ptr.h"
+#include "base/utf_string_conversions.h"
+
+const char kPythonPathEnv[] = "PYTHONPATH";
+
+void AppendToPythonPath(const FilePath& dir) {
+ scoped_ptr<base::Environment> env(base::Environment::Create());
+ std::string old_path;
+ std::string dir_path;
+#if defined(OS_WIN)
+ dir_path = WideToUTF8(dir.value());
+#elif defined(OS_POSIX)
+ dir_path = dir.value();
+#endif
+ if (!env->GetVar(kPythonPathEnv, &old_path)) {
+ env->SetVar(kPythonPathEnv, dir_path.c_str());
+ } else if (old_path.find(dir_path) == std::string::npos) {
+ std::string new_path(old_path);
+#if defined(OS_WIN)
+ new_path.append(";");
+#elif defined(OS_POSIX)
+ new_path.append(":");
+#endif
+ new_path.append(dir_path.c_str());
+ env->SetVar(kPythonPathEnv, new_path);
+ }
+}
+
+bool GetPythonRunTime(FilePath* dir) {
+#if defined(OS_WIN)
+ if (!PathService::Get(base::DIR_SOURCE_ROOT, dir))
+ return false;
+ *dir = FilePath(FILE_PATH_LITERAL("third_party"))
+ .Append(FILE_PATH_LITERAL("python_24"))
+ .Append(FILE_PATH_LITERAL("python.exe"));
+#elif defined(OS_POSIX)
+ *dir = FilePath("python");
+#endif
+ return true;
+}
+
diff --git a/net/test/python_utils.h b/net/test/python_utils.h
new file mode 100644
index 0000000..215569c
--- /dev/null
+++ b/net/test/python_utils.h
@@ -0,0 +1,23 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_TEST_PYTHON_UTILS_H_
+#define NET_TEST_PYTHON_UTILS_H_
+#pragma once
+
+#include "base/compiler_specific.h"
+
+class FilePath;
+
+// This is the python path variable name.
+extern const char kPythonPathEnv[];
+
+// Appends the dir to python path environment variable.
+void AppendToPythonPath(const FilePath& dir);
+
+// Returns the path that should be used to launch Python.
+bool GetPythonRunTime(FilePath* path) WARN_UNUSED_RESULT;
+
+#endif // NET_TEST_PYTHON_UTILS_H_
+
diff --git a/net/test/python_utils_unittest.cc b/net/test/python_utils_unittest.cc
new file mode 100644
index 0000000..4026840
--- /dev/null
+++ b/net/test/python_utils_unittest.cc
@@ -0,0 +1,51 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/environment.h"
+#include "base/file_path.h"
+#include "base/scoped_ptr.h"
+#include "net/test/python_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(PythonUtils, Append) {
+ const FilePath::CharType kAppendDir1[] =
+ FILE_PATH_LITERAL("test/path_append1");
+ const FilePath::CharType kAppendDir2[] =
+ FILE_PATH_LITERAL("test/path_append2");
+
+ scoped_ptr<base::Environment> env(base::Environment::Create());
+
+ std::string python_path;
+ FilePath append_path1(kAppendDir1);
+ FilePath append_path2(kAppendDir2);
+
+ // Get a clean start
+ env->UnSetVar(kPythonPathEnv);
+
+ // Append the path
+ AppendToPythonPath(append_path1);
+ env->GetVar(kPythonPathEnv, &python_path);
+ ASSERT_EQ(python_path, "test/path_append1");
+
+ // Append the safe path again, nothing changes
+ AppendToPythonPath(append_path2);
+ env->GetVar(kPythonPathEnv, &python_path);
+#if defined(OS_WIN)
+ ASSERT_EQ(std::string("test/path_append1;test/path_append2"), python_path);
+#elif defined(OS_POSIX)
+ ASSERT_EQ(std::string("test/path_append1:test/path_append2"), python_path);
+#endif
+}
+
+TEST(PythonUtils, PythonRunTime) {
+ FilePath dir;
+ EXPECT_TRUE(GetPythonRunTime(&dir));
+#if defined(OS_WIN)
+ EXPECT_NE(std::wstring::npos,
+ dir.value().find(L"third_party\\python_24\\python.exe"));
+#elif defined(OS_POSIX)
+ EXPECT_NE(std::string::npos, dir.value().find("python"));
+#endif
+}
+
diff --git a/net/test/test_server.cc b/net/test/test_server.cc
index e727791..4c698b0 100644
--- a/net/test/test_server.cc
+++ b/net/test/test_server.cc
@@ -28,6 +28,7 @@
#include "net/base/test_completion_callback.h"
#include "net/socket/tcp_client_socket.h"
#include "net/socket/tcp_pinger.h"
+#include "net/test/python_utils.h"
#include "testing/platform_test.h"
namespace {
diff --git a/net/test/test_server.h b/net/test/test_server.h
index 5f51571..8b6dc63 100644
--- a/net/test/test_server.h
+++ b/net/test/test_server.h
@@ -72,9 +72,6 @@ class TestServer {
const std::string& password);
private:
- // Appends |dir| to PYTHONPATH.
- static void AppendToPythonPath(const FilePath& dir);
-
// Modify PYTHONPATH to contain libraries we need.
bool SetPythonPath() WARN_UNUSED_RESULT;
diff --git a/net/test/test_server_posix.cc b/net/test/test_server_posix.cc
index 783bdc3..7741d89 100644
--- a/net/test/test_server_posix.cc
+++ b/net/test/test_server_posix.cc
@@ -9,22 +9,6 @@
#include "base/string_number_conversions.h"
namespace net {
-
-// static
-void TestServer::AppendToPythonPath(const FilePath& dir) {
- const char kPythonPath[] = "PYTHONPATH";
- const char* oldpath = getenv(kPythonPath);
- // setenv() leaks memory intentionally on Mac
- if (!oldpath) {
- setenv(kPythonPath, dir.value().c_str(), 1);
- } else if (!strstr(oldpath, dir.value().c_str())) {
- std::string newpath(oldpath);
- newpath.append(":");
- newpath.append(dir.value());
- setenv(kPythonPath, newpath.c_str(), 1);
- }
-}
-
bool TestServer::LaunchPython(const FilePath& testserver_path) {
std::vector<std::string> command_line;
command_line.push_back("python");
diff --git a/net/test/test_server_win.cc b/net/test/test_server_win.cc
index b806cba..7e8443f 100644
--- a/net/test/test_server_win.cc
+++ b/net/test/test_server_win.cc
@@ -74,22 +74,6 @@ bool LaunchTestServerAsJob(const std::wstring& cmdline,
} // namespace
namespace net {
-
-// static
-void TestServer::AppendToPythonPath(const FilePath& dir) {
- const wchar_t kPythonPath[] = L"PYTHONPATH";
- // TODO(dkegel): handle longer PYTHONPATH variables
- wchar_t oldpath[4096];
- if (GetEnvironmentVariable(kPythonPath, oldpath, arraysize(oldpath)) == 0) {
- SetEnvironmentVariableW(kPythonPath, dir.value().c_str());
- } else if (!wcsstr(oldpath, dir.value().c_str())) {
- std::wstring newpath(oldpath);
- newpath.append(L";");
- newpath.append(dir.value());
- SetEnvironmentVariableW(kPythonPath, newpath.c_str());
- }
-}
-
bool TestServer::LaunchPython(const FilePath& testserver_path) {
FilePath python_exe;
if (!PathService::Get(base::DIR_SOURCE_ROOT, &python_exe))