OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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" |
| 10 |
| 11 namespace internal { |
| 12 |
| 13 namespace { |
| 14 bool ExecPython(const std::string& command, |
| 15 std::string* std_out, |
| 16 std::string* std_err, |
| 17 int* exit_code) { |
| 18 base::ScopedTempDir temp_dir; |
| 19 base::CommandLine::StringVector args; |
| 20 #if defined(OS_WIN) |
| 21 args.push_back(L"python"); |
| 22 args.push_back(L"-c"); |
| 23 args.push_back(base::UTF8ToUTF16(command)); |
| 24 #else |
| 25 args.push_back("python"); |
| 26 args.push_back("-c"); |
| 27 args.push_back(command); |
| 28 #endif |
| 29 return ExecProcess( |
| 30 base::CommandLine(args), temp_dir.path(), std_out, std_err, exit_code); |
| 31 } |
| 32 } // namespace |
| 33 |
| 34 TEST(ExecProcessTest, TestExitCode) { |
| 35 std::string std_out, std_err; |
| 36 int exit_code; |
| 37 |
| 38 ASSERT_TRUE( |
| 39 ExecPython("import sys; sys.exit(0)", &std_out, &std_err, &exit_code)); |
| 40 EXPECT_EQ(0, exit_code); |
| 41 |
| 42 ASSERT_TRUE( |
| 43 ExecPython("import sys; sys.exit(1)", &std_out, &std_err, &exit_code)); |
| 44 EXPECT_EQ(1, exit_code); |
| 45 |
| 46 ASSERT_TRUE( |
| 47 ExecPython("import sys; sys.exit(253)", &std_out, &std_err, &exit_code)); |
| 48 EXPECT_EQ(253, exit_code); |
| 49 |
| 50 ASSERT_TRUE( |
| 51 ExecPython("throw Exception()", &std_out, &std_err, &exit_code)); |
| 52 EXPECT_EQ(1, exit_code); |
| 53 } |
| 54 |
| 55 // Test that large output is handled correctly. There are various ways that this |
| 56 // could potentially fail. For example, non-blocking Linux pipes have a 65536 |
| 57 // byte buffer and, if stdout is non-blocking, python will throw an IOError when |
| 58 // a write exceeds the buffer size. |
| 59 TEST(ExecProcessTest, TestLargeOutput) { |
| 60 base::ScopedTempDir temp_dir; |
| 61 std::string std_out, std_err; |
| 62 int exit_code; |
| 63 |
| 64 ASSERT_TRUE(ExecPython( |
| 65 "import sys; print 'o' * 1000000", &std_out, &std_err, &exit_code)); |
| 66 EXPECT_EQ(0, exit_code); |
| 67 EXPECT_EQ(1000001u, std_out.size()); |
| 68 } |
| 69 |
| 70 TEST(ExecProcessTest, TestStdoutAndStderrOutput) { |
| 71 base::ScopedTempDir temp_dir; |
| 72 std::string std_out, std_err; |
| 73 int exit_code; |
| 74 |
| 75 ASSERT_TRUE(ExecPython( |
| 76 "import sys; print 'o' * 10000; print >>sys.stderr, 'e' * 10000", |
| 77 &std_out, |
| 78 &std_err, |
| 79 &exit_code)); |
| 80 EXPECT_EQ(0, exit_code); |
| 81 EXPECT_EQ(10001u, std_out.size()); |
| 82 EXPECT_EQ(10001u, std_err.size()); |
| 83 |
| 84 std_out.clear(); |
| 85 std_err.clear(); |
| 86 ASSERT_TRUE(ExecPython( |
| 87 "import sys; print >>sys.stderr, 'e' * 10000; print 'o' * 10000", |
| 88 &std_out, |
| 89 &std_err, |
| 90 &exit_code)); |
| 91 EXPECT_EQ(0, exit_code); |
| 92 EXPECT_EQ(10001u, std_out.size()); |
| 93 EXPECT_EQ(10001u, std_err.size()); |
| 94 } |
| 95 |
| 96 TEST(ExecProcessTest, TestOneOutputClosed) { |
| 97 std::string std_out, std_err; |
| 98 int exit_code; |
| 99 |
| 100 ASSERT_TRUE(ExecPython("import sys; sys.stderr.close(); print 'o' * 10000", |
| 101 &std_out, |
| 102 &std_err, |
| 103 &exit_code)); |
| 104 EXPECT_EQ(0, exit_code); |
| 105 EXPECT_EQ(10001u, std_out.size()); |
| 106 EXPECT_EQ(std_err.size(), 0u); |
| 107 |
| 108 std_out.clear(); |
| 109 std_err.clear(); |
| 110 ASSERT_TRUE(ExecPython( |
| 111 "import sys; sys.stdout.close(); print >>sys.stderr, 'e' * 10000", |
| 112 &std_out, |
| 113 &std_err, |
| 114 &exit_code)); |
| 115 EXPECT_EQ(0, exit_code); |
| 116 EXPECT_EQ(0u, std_out.size()); |
| 117 EXPECT_EQ(10001u, std_err.size()); |
| 118 } |
| 119 } // namespace internal |
OLD | NEW |