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