forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_process_unittest.cc
130 lines (112 loc) · 3.71 KB
/
exec_process_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2014 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 "tools/gn/exec_process.h"
#include "base/command_line.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
#include "base/strings/utf_string_conversions.h"
#endif
namespace internal {
// TODO(cjhopman): Enable these tests when windows ExecProcess handles stderr.
// 'python' is not runnable on Windows. Adding ["cmd", "/c"] fails because
// CommandLine does unusual reordering of args.
#if !defined(OS_WIN)
namespace {
bool ExecPython(const std::string& command,
std::string* std_out,
std::string* std_err,
int* exit_code) {
base::ScopedTempDir temp_dir;
base::CommandLine::StringVector args;
#if defined(OS_WIN)
args.push_back(L"python");
args.push_back(L"-c");
args.push_back(base::UTF8ToUTF16(command));
#else
args.push_back("python");
args.push_back("-c");
args.push_back(command);
#endif
return ExecProcess(
base::CommandLine(args), temp_dir.path(), std_out, std_err, exit_code);
}
} // namespace
TEST(ExecProcessTest, TestExitCode) {
std::string std_out, std_err;
int exit_code;
ASSERT_TRUE(
ExecPython("import sys; sys.exit(0)", &std_out, &std_err, &exit_code));
EXPECT_EQ(0, exit_code);
ASSERT_TRUE(
ExecPython("import sys; sys.exit(1)", &std_out, &std_err, &exit_code));
EXPECT_EQ(1, exit_code);
ASSERT_TRUE(
ExecPython("import sys; sys.exit(253)", &std_out, &std_err, &exit_code));
EXPECT_EQ(253, exit_code);
ASSERT_TRUE(
ExecPython("throw Exception()", &std_out, &std_err, &exit_code));
EXPECT_EQ(1, exit_code);
}
// Test that large output is handled correctly. There are various ways that this
// could potentially fail. For example, non-blocking Linux pipes have a 65536
// byte buffer and, if stdout is non-blocking, python will throw an IOError when
// a write exceeds the buffer size.
TEST(ExecProcessTest, TestLargeOutput) {
base::ScopedTempDir temp_dir;
std::string std_out, std_err;
int exit_code;
ASSERT_TRUE(ExecPython(
"import sys; print 'o' * 1000000", &std_out, &std_err, &exit_code));
EXPECT_EQ(0, exit_code);
EXPECT_EQ(1000001u, std_out.size());
}
TEST(ExecProcessTest, TestStdoutAndStderrOutput) {
base::ScopedTempDir temp_dir;
std::string std_out, std_err;
int exit_code;
ASSERT_TRUE(ExecPython(
"import sys; print 'o' * 10000; print >>sys.stderr, 'e' * 10000",
&std_out,
&std_err,
&exit_code));
EXPECT_EQ(0, exit_code);
EXPECT_EQ(10001u, std_out.size());
EXPECT_EQ(10001u, std_err.size());
std_out.clear();
std_err.clear();
ASSERT_TRUE(ExecPython(
"import sys; print >>sys.stderr, 'e' * 10000; print 'o' * 10000",
&std_out,
&std_err,
&exit_code));
EXPECT_EQ(0, exit_code);
EXPECT_EQ(10001u, std_out.size());
EXPECT_EQ(10001u, std_err.size());
}
TEST(ExecProcessTest, TestOneOutputClosed) {
std::string std_out, std_err;
int exit_code;
ASSERT_TRUE(ExecPython("import sys; sys.stderr.close(); print 'o' * 10000",
&std_out,
&std_err,
&exit_code));
EXPECT_EQ(0, exit_code);
EXPECT_EQ(10001u, std_out.size());
EXPECT_EQ(std_err.size(), 0u);
std_out.clear();
std_err.clear();
ASSERT_TRUE(ExecPython(
"import sys; sys.stdout.close(); print >>sys.stderr, 'e' * 10000",
&std_out,
&std_err,
&exit_code));
EXPECT_EQ(0, exit_code);
EXPECT_EQ(0u, std_out.size());
EXPECT_EQ(10001u, std_err.size());
}
#endif
} // namespace internal