From 3f8664036e63d2a2d58120ab6c1e8a0a09937c71 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 18 Nov 2017 03:48:59 +0100 Subject: support COLUMNS environment variable in apt tools apt usually gets the width of the window from the terminal or failing that has a default value, but especially for testing it can be handy to control the size as you can't be sure that variable sized content will always be linebreaked as expected in the testcases. --- apt-private/private-output.cc | 20 ++++++++++++++++++-- test/integration/test-00-commands-have-help | 13 +++++++++++++ test/integration/test-kernel-helper-autoremove | 17 +++++++++++++---- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/apt-private/private-output.cc b/apt-private/private-output.cc index 6bc18516d..eb9a34abe 100644 --- a/apt-private/private-output.cc +++ b/apt-private/private-output.cc @@ -67,8 +67,24 @@ bool InitOutput(std::basic_streambuf * const out) /*{{{*/ c1out.rdbuf(devnull.rdbuf()); // deal with window size changes - signal(SIGWINCH,SigWinch); - SigWinch(0); + auto cols = getenv("COLUMNS"); + if (cols != nullptr) + { + char * colends; + auto const sw = strtoul(cols, &colends, 10); + if (*colends != '\0' || sw == 0) + { + _error->Warning("Environment variable COLUMNS was ignored as it has an invalid value: \"%s\"", cols); + cols = nullptr; + } + else + ScreenWidth = sw; + } + if (cols == nullptr) + { + signal(SIGWINCH,SigWinch); + SigWinch(0); + } if(!isatty(1)) { diff --git a/test/integration/test-00-commands-have-help b/test/integration/test-00-commands-have-help index 2d8fa1105..4a0cc64d4 100755 --- a/test/integration/test-00-commands-have-help +++ b/test/integration/test-00-commands-have-help @@ -86,3 +86,16 @@ testmoo '@1484822790' 'Have you mooed today?' testmoo '@1484822791' 'Have you mooed today?' testmoo '@1484822792' 'Have you mooed today?' testmoo '@1484822793' 'Have you mooed today?' + +# that also helps with reproducibility of output… +export COLUMNS=80 +testsuccess aptget moo +export COLUMNS=0 +testwarning aptget moo +export COLUMNS=80a +testwarning aptget moo +export COLUMNS=a80 +testwarning aptget moo +export COLUMNS= +testwarning aptget moo +unset COLUMNS diff --git a/test/integration/test-kernel-helper-autoremove b/test/integration/test-kernel-helper-autoremove index a70841d9d..8cde3432f 100755 --- a/test/integration/test-kernel-helper-autoremove +++ b/test/integration/test-kernel-helper-autoremove @@ -116,16 +116,17 @@ msgmsg "run without parameter" testprotected msgtest 'Check kernel autoremoval protection list does not include' 'old kernel' testfailure --nomsg grep '^\^linux-image-1\\\.0\\\.0-2-generic\$$' protected.list +export COLUMNS=99999 testsuccessequal "Reading package lists... Building dependency tree... Reading state information... The following packages will be REMOVED: - linux-headers-1000000-1-generic linux-image-1.0.0-2-generic - ${CURRENTKERNEL}-dbg + linux-headers-1000000-1-generic linux-image-1.0.0-2-generic ${CURRENTKERNEL}-dbg 0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded. Remv linux-headers-1000000-1-generic [100.0.0-1] Remv linux-image-1.0.0-2-generic [1.0.0-2] Remv ${CURRENTKERNEL}-dbg [5-1]" aptget autoremove -s +unset COLUMNS msgmsg "install unknown kernel" # even if installed/uname reports a kernel which we can't find via dpkg, @@ -136,32 +137,39 @@ msgtest 'Check kernel autoremoval protection list does not include' 'old kernel' testfailure --nomsg grep '^\^linux-image-1\\\.0\\\.0-2-generic\$$' protected.list msgtest 'Check kernel autoremoval protection list does include' 'unknown installed kernel' testsuccess --nomsg grep '^\^linux-image-1\\\.0\\\.0-2-ungeneric\$$' protected.list +export COLUMNS=9 testsuccessequal "Reading package lists... Building dependency tree... Reading state information... The following packages will be REMOVED: - linux-headers-1000000-1-generic linux-image-1.0.0-2-generic + linux-headers-1000000-1-generic + linux-image-1.0.0-2-generic ${CURRENTKERNEL}-dbg 0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded. Remv linux-headers-1000000-1-generic [100.0.0-1] Remv linux-image-1.0.0-2-generic [1.0.0-2] Remv ${CURRENTKERNEL}-dbg [5-1]" aptget autoremove -s +unset COLUMNS msgmsg "install an old kernel" testprotected 1.0.0-2-generic msgtest 'Check kernel autoremoval protection list includes' 'installed kernel' testsuccess --nomsg grep '^\^linux-image-1\\\.0\\\.0-2-generic\$$' protected.list +export COLUMNS=9 testsuccessequal "Reading package lists... Building dependency tree... Reading state information... The following packages will be REMOVED: - linux-headers-1000000-1-generic ${CURRENTKERNEL}-dbg + linux-headers-1000000-1-generic + ${CURRENTKERNEL}-dbg 0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded. Remv linux-headers-1000000-1-generic [100.0.0-1] Remv ${CURRENTKERNEL}-dbg [5-1]" aptget autoremove -s +unset COLUMNS # rt kernel was put on hold while the protected list was generated testsuccess aptmark unhold "${CURRENTKERNEL}-rt" +export COLUMNS=99999 testsuccessequal "Reading package lists... Building dependency tree... Reading state information... @@ -170,3 +178,4 @@ The following packages will be REMOVED: 0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded. Remv linux-headers-1000000-1-generic [100.0.0-1] Remv ${CURRENTKERNEL}-dbg [5-1]" aptget autoremove -s +unset COLUMNS -- cgit v1.2.3