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 #if defined(OS_WIN) |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #endif |
| 14 |
| 15 namespace internal { |
| 16 |
| 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); |
| 35 } |
| 36 } // namespace |
| 37 |
| 38 TEST(ExecProcessTest, TestExitCode) { |
| 39 std::string std_out, std_err; |
| 40 int exit_code; |
| 41 |
| 42 ASSERT_TRUE( |
| 43 ExecPython("import sys; sys.exit(0)", &std_out, &std_err, &exit_code)); |
| 44 EXPECT_EQ(0, exit_code); |
| 45 |
| 46 ASSERT_TRUE( |
| 47 ExecPython("import sys; sys.exit(1)", &std_out, &std_err, &exit_code)); |
| 48 EXPECT_EQ(1, exit_code); |
| 49 |
| 50 ASSERT_TRUE( |
| 51 ExecPython("import sys; sys.exit(253)", &std_out, &std_err, &exit_code)); |
| 52 EXPECT_EQ(253, exit_code); |
| 53 |
| 54 ASSERT_TRUE( |
| 55 ExecPython("throw Exception()", &std_out, &std_err, &exit_code)); |
| 56 EXPECT_EQ(1, exit_code); |
| 57 } |
| 58 |
| 59 // Test that large output is handled correctly. There are various ways that this |
| 60 // could potentially fail. For example, non-blocking Linux pipes have a 65536 |
| 61 // byte buffer and, if stdout is non-blocking, python will throw an IOError when |
| 62 // a write exceeds the buffer size. |
| 63 TEST(ExecProcessTest, TestLargeOutput) { |
| 64 base::ScopedTempDir temp_dir; |
| 65 std::string std_out, std_err; |
| 66 int exit_code; |
| 67 |
| 68 ASSERT_TRUE(ExecPython( |
| 69 "import sys; print 'o' * 1000000", &std_out, &std_err, &exit_code)); |
| 70 EXPECT_EQ(0, exit_code); |
| 71 EXPECT_EQ(1000001u, std_out.size()); |
| 72 } |
| 73 |
| 74 // TODO(cjhopman): Enable these tests when windows ExecProcess handles stderr. |
| 75 #if !defined(OS_WIN) |
| 76 TEST(ExecProcessTest, TestStdoutAndStderrOutput) { |
| 77 base::ScopedTempDir temp_dir; |
| 78 std::string std_out, std_err; |
| 79 int exit_code; |
| 80 |
| 81 ASSERT_TRUE(ExecPython( |
| 82 "import sys; print 'o' * 10000; print >>sys.stderr, 'e' * 10000", |
| 83 &std_out, |
| 84 &std_err, |
| 85 &exit_code)); |
| 86 EXPECT_EQ(0, exit_code); |
| 87 EXPECT_EQ(10001u, std_out.size()); |
| 88 EXPECT_EQ(10001u, std_err.size()); |
| 89 |
| 90 std_out.clear(); |
| 91 std_err.clear(); |
| 92 ASSERT_TRUE(ExecPython( |
| 93 "import sys; print >>sys.stderr, 'e' * 10000; print 'o' * 10000", |
| 94 &std_out, |
| 95 &std_err, |
| 96 &exit_code)); |
| 97 EXPECT_EQ(0, exit_code); |
| 98 EXPECT_EQ(10001u, std_out.size()); |
| 99 EXPECT_EQ(10001u, std_err.size()); |
| 100 } |
| 101 |
| 102 TEST(ExecProcessTest, TestOneOutputClosed) { |
| 103 std::string std_out, std_err; |
| 104 int exit_code; |
| 105 |
| 106 ASSERT_TRUE(ExecPython("import sys; sys.stderr.close(); print 'o' * 10000", |
| 107 &std_out, |
| 108 &std_err, |
| 109 &exit_code)); |
| 110 EXPECT_EQ(0, exit_code); |
| 111 EXPECT_EQ(10001u, std_out.size()); |
| 112 EXPECT_EQ(std_err.size(), 0u); |
| 113 |
| 114 std_out.clear(); |
| 115 std_err.clear(); |
| 116 ASSERT_TRUE(ExecPython( |
| 117 "import sys; sys.stdout.close(); print >>sys.stderr, 'e' * 10000", |
| 118 &std_out, |
| 119 &std_err, |
| 120 &exit_code)); |
| 121 EXPECT_EQ(0, exit_code); |
| 122 EXPECT_EQ(0u, std_out.size()); |
| 123 EXPECT_EQ(10001u, std_err.size()); |
| 124 } |
| 125 #endif |
| 126 } // namespace internal |
OLD | NEW |