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"
12 #include "base/strings/utf_string_conversions.h"
17 // TODO(cjhopman): Enable these tests when windows ExecProcess handles stderr.
18 // 'python' is not runnable on Windows. Adding ["cmd", "/c"] fails because
19 // CommandLine does unusual reordering of args.
22 bool ExecPython(const std::string
& command
,
26 base::ScopedTempDir temp_dir
;
27 base::CommandLine::StringVector args
;
29 args
.push_back(L
"python");
30 args
.push_back(L
"-c");
31 args
.push_back(base::UTF8ToUTF16(command
));
33 args
.push_back("python");
35 args
.push_back(command
);
38 base::CommandLine(args
), temp_dir
.path(), std_out
, std_err
, exit_code
);
42 TEST(ExecProcessTest
, TestExitCode
) {
43 std::string std_out
, std_err
;
47 ExecPython("import sys; sys.exit(0)", &std_out
, &std_err
, &exit_code
));
48 EXPECT_EQ(0, exit_code
);
51 ExecPython("import sys; sys.exit(1)", &std_out
, &std_err
, &exit_code
));
52 EXPECT_EQ(1, exit_code
);
55 ExecPython("import sys; sys.exit(253)", &std_out
, &std_err
, &exit_code
));
56 EXPECT_EQ(253, exit_code
);
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
;
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
;
83 ASSERT_TRUE(ExecPython(
84 "import sys; print 'o' * 10000; print >>sys.stderr, 'e' * 10000",
88 EXPECT_EQ(0, exit_code
);
89 EXPECT_EQ(10001u, std_out
.size());
90 EXPECT_EQ(10001u, std_err
.size());
94 ASSERT_TRUE(ExecPython(
95 "import sys; print >>sys.stderr, 'e' * 10000; print 'o' * 10000",
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
;
108 ASSERT_TRUE(ExecPython("import sys; sys.stderr.close(); print 'o' * 10000",
112 EXPECT_EQ(0, exit_code
);
113 EXPECT_EQ(10001u, std_out
.size());
114 EXPECT_EQ(std_err
.size(), 0u);
118 ASSERT_TRUE(ExecPython(
119 "import sys; sys.stdout.close(); print >>sys.stderr, 'e' * 10000",
123 EXPECT_EQ(0, exit_code
);
124 EXPECT_EQ(0u, std_out
.size());
125 EXPECT_EQ(10001u, std_err
.size());
128 } // namespace internal