aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2007-11-08 16:52:16 +0000
committerAvatar David Leverton <levertond@googlemail.com> 2007-11-08 16:52:16 +0000
commit9a12a516009982fb3e95ec0f7d21ae53556a99ff (patch)
tree7c107ad532334d04652b32800cb6bd595d0af038
parent3192b6fdd8dd9c68179c7de8410e2a418e17087a (diff)
downloadpaludis-9a12a516009982fb3e95ec0f7d21ae53556a99ff.tar.gz
paludis-9a12a516009982fb3e95ec0f7d21ae53556a99ff.tar.xz
Handle carriage returns better in outputwrapper.
-rw-r--r--paludis/util/output_wrapper.cc14
-rw-r--r--paludis/util/output_wrapper_TEST.cc166
-rwxr-xr-xpaludis/util/output_wrapper_TEST_setup.sh30
3 files changed, 204 insertions, 6 deletions
diff --git a/paludis/util/output_wrapper.cc b/paludis/util/output_wrapper.cc
index ac4bc0f..dd6cee0 100644
--- a/paludis/util/output_wrapper.cc
+++ b/paludis/util/output_wrapper.cc
@@ -156,7 +156,7 @@ main(int argc, char *argv[])
for (std::string::size_type p(0) ; p < to_write.length() ; ++p)
{
- if (to_write.at(p) != '\n')
+ if (to_write.at(p) != '\n' && to_write.at(p) != '\r')
{
if (! stdout_had_non_blanks)
{
@@ -171,7 +171,8 @@ main(int argc, char *argv[])
}
else if (! stdout_had_non_blanks)
{
- ++stdout_blanks;
+ if (to_write.at(p) == '\n')
+ ++stdout_blanks;
continue;
}
@@ -182,7 +183,7 @@ main(int argc, char *argv[])
stdout_prefix_shown = true;
}
- if (to_write.at(p) == '\n')
+ if (to_write.at(p) == '\n' || to_write.at(p) == '\r')
{
stdout_had_interesting_char = false;
stdout_prefix_shown = false;
@@ -206,7 +207,7 @@ main(int argc, char *argv[])
for (std::string::size_type p(0) ; p < to_write.length() ; ++p)
{
- if (to_write.at(p) != '\n')
+ if (to_write.at(p) != '\n' && to_write.at(p) != '\r')
{
if (! stderr_had_non_blanks)
{
@@ -221,7 +222,8 @@ main(int argc, char *argv[])
}
else if (! stderr_had_non_blanks)
{
- ++stderr_blanks;
+ if (to_write.at(p) == '\n')
+ ++stderr_blanks;
continue;
}
@@ -232,7 +234,7 @@ main(int argc, char *argv[])
stderr_prefix_shown = true;
}
- if (to_write.at(p) == '\n')
+ if (to_write.at(p) == '\n' || to_write.at(p) == '\r')
{
stderr_had_interesting_char = false;
stderr_prefix_shown = false;
diff --git a/paludis/util/output_wrapper_TEST.cc b/paludis/util/output_wrapper_TEST.cc
index d921ad8..9828b29 100644
--- a/paludis/util/output_wrapper_TEST.cc
+++ b/paludis/util/output_wrapper_TEST.cc
@@ -269,5 +269,171 @@ namespace test_cases
TEST_CHECK_EQUAL(0, p.exit_status());
}
} test_discard_wrap_blank_output_not_blank;
+
+ struct CarriageReturnTest : TestCase
+ {
+ CarriageReturnTest() : TestCase("carriage return") { }
+
+ void run()
+ {
+ PStream p(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' -- "
+ "bash output_wrapper_TEST_dir/carriage_return.bash 2>&1"));
+ std::multiset<std::string> lines;
+ std::string line;
+ while (std::getline(p, line))
+ {
+ std::string::size_type c(line.length());
+ while (std::string::npos != (c = line.rfind('\r', c)))
+ line.replace(c, 1, "\\r");
+ lines.insert(line);
+ }
+
+ TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
+ TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(10));
+ TEST_CHECK(lines.count("o p foo\\ro p bar"));
+ TEST_CHECK(lines.count("o p foo\\r"));
+ TEST_CHECK(lines.count("o p foo\\r\\ro p bar"));
+ TEST_CHECK(lines.count("\\ro p foo"));
+ TEST_CHECK(lines.count("e p foo\\re p bar"));
+ TEST_CHECK(lines.count("e p foo\\r"));
+ TEST_CHECK(lines.count("e p foo\\r\\re p bar"));
+ TEST_CHECK(lines.count("\\re p foo"));
+ TEST_CHECK_EQUAL(lines.count("\\r"), static_cast<std::size_t>(2));
+ TEST_CHECK_EQUAL(0, p.exit_status());
+ }
+ } test_carriage_return;
+
+ struct CarriageReturnWrapBlankTest : TestCase
+ {
+ CarriageReturnWrapBlankTest() : TestCase("carriage return wrap blank") { }
+
+ void run()
+ {
+ PStream p(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --wrap-blanks -- "
+ "bash output_wrapper_TEST_dir/carriage_return.bash 2>&1"));
+ std::multiset<std::string> lines;
+ std::string line;
+ while (std::getline(p, line))
+ {
+ std::string::size_type c(line.length());
+ while (std::string::npos != (c = line.rfind('\r', c)))
+ line.replace(c, 1, "\\r");
+ lines.insert(line);
+ }
+
+ TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
+ TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(10));
+ TEST_CHECK(lines.count("o p foo\\ro p bar"));
+ TEST_CHECK(lines.count("o p foo\\ro p "));
+ TEST_CHECK(lines.count("o p foo\\ro p \\ro p bar"));
+ TEST_CHECK(lines.count("o p \\ro p foo"));
+ TEST_CHECK(lines.count("o p \\ro p "));
+ TEST_CHECK(lines.count("e p foo\\re p bar"));
+ TEST_CHECK(lines.count("e p foo\\re p "));
+ TEST_CHECK(lines.count("e p foo\\re p \\re p bar"));
+ TEST_CHECK(lines.count("e p \\re p foo"));
+ TEST_CHECK(lines.count("e p \\re p "));
+ TEST_CHECK_EQUAL(0, p.exit_status());
+ }
+ } test_carriage_return_wrap_blank;
+
+ struct CarriageReturnDiscardBlankOutputBlankTest : TestCase
+ {
+ CarriageReturnDiscardBlankOutputBlankTest() : TestCase("carriage return discard blank output blank") { }
+
+ void run()
+ {
+ PStream p(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output -- "
+ "bash output_wrapper_TEST_dir/carriage_return_blank.bash 2>&1"));
+ std::multiset<std::string> lines;
+ std::string line;
+ while (std::getline(p, line))
+ {
+ std::string::size_type c(line.length());
+ while (std::string::npos != (c = line.rfind('\r', c)))
+ line.replace(c, 1, "\\r");
+ lines.insert(line);
+ }
+
+ TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
+ TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(0));
+ }
+ } test_carriage_return_discard_blank_output_blank;
+
+ struct CarriageReturnDiscardBlankWrapBlankOutputBlankTest : TestCase
+ {
+ CarriageReturnDiscardBlankWrapBlankOutputBlankTest() : TestCase("carriage return discard blank wrap blank output blank") { }
+
+ void run()
+ {
+ PStream p(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output --wrap-blanks -- "
+ "bash output_wrapper_TEST_dir/carriage_return_blank.bash 2>&1"));
+ std::multiset<std::string> lines;
+ std::string line;
+ while (std::getline(p, line))
+ {
+ std::string::size_type c(line.length());
+ while (std::string::npos != (c = line.rfind('\r', c)))
+ line.replace(c, 1, "\\r");
+ lines.insert(line);
+ }
+
+ TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
+ TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(0));
+ }
+ } test_carriage_return_discard_blank_wrap_blank_output_blank;
+
+ struct CarriageReturnDiscardBlankOutputNotBlankTest : TestCase
+ {
+ CarriageReturnDiscardBlankOutputNotBlankTest() : TestCase("carriage return discard blank output not blank") { }
+
+ void run()
+ {
+ PStream p(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output -- "
+ "bash output_wrapper_TEST_dir/carriage_return_nonblank.bash 2>&1"));
+ std::multiset<std::string> lines;
+ std::string line;
+ while (std::getline(p, line))
+ {
+ std::string::size_type c(line.length());
+ while (std::string::npos != (c = line.rfind('\r', c)))
+ line.replace(c, 1, "\\r");
+ lines.insert(line);
+ }
+
+ TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
+ TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(6));
+ TEST_CHECK_EQUAL(lines.count(""), static_cast<std::size_t>(4));
+ TEST_CHECK(lines.count("o p hello"));
+ TEST_CHECK(lines.count("e p hello"));
+ }
+ } test_carriage_return_discard_blank_output_not_blank;
+
+ struct CarriageReturnDiscardBlankWrapBlankOutputNotBlankTest : TestCase
+ {
+ CarriageReturnDiscardBlankWrapBlankOutputNotBlankTest() : TestCase("carriage return discard blank wrap blank output not blank") { }
+
+ void run()
+ {
+ PStream p(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output --wrap-blanks -- "
+ "bash output_wrapper_TEST_dir/carriage_return_nonblank.bash 2>&1"));
+ std::multiset<std::string> lines;
+ std::string line;
+ while (std::getline(p, line))
+ {
+ std::string::size_type c(line.length());
+ while (std::string::npos != (c = line.rfind('\r', c)))
+ line.replace(c, 1, "\\r");
+ lines.insert(line);
+ }
+
+ TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
+ TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(6));
+ TEST_CHECK_EQUAL(lines.count("o p "), static_cast<std::size_t>(2));
+ TEST_CHECK_EQUAL(lines.count("e p "), static_cast<std::size_t>(2));
+ TEST_CHECK(lines.count("o p hello"));
+ TEST_CHECK(lines.count("e p hello"));
+ }
+ } test_carriage_return_discard_blank_wrap_blank_output_not_blank;
}
diff --git a/paludis/util/output_wrapper_TEST_setup.sh b/paludis/util/output_wrapper_TEST_setup.sh
index b467150..229a74d 100755
--- a/paludis/util/output_wrapper_TEST_setup.sh
+++ b/paludis/util/output_wrapper_TEST_setup.sh
@@ -106,4 +106,34 @@ echo monkey
echo
END
+cat <<END > carriage_return.bash
+echo -e 'foo\rbar'
+echo -e 'foo\r'
+echo -e 'foo\r\rbar'
+echo -e '\rfoo'
+echo -e '\r'
+echo -e 'foo\rbar' 1>&2
+echo -e 'foo\r' 1>&2
+echo -e 'foo\r\rbar' 1>&2
+echo -e '\rfoo' 1>&2
+echo -e '\r' 1>&2
+END
+
+cat <<END > carriage_return_blank.bash
+echo
+echo -e '\r'
+echo
+echo 1>&2
+echo -e '\r' 1>&2
+echo 1>&2
+END
+
+cat <<END > carriage_return_nonblank.bash
+echo
+echo -e '\r'
+echo hello
+echo 1>&2
+echo -e '\r' 1>&2
+echo hello 1>&2
+END