]> git.saurik.com Git - apt.git/commitdiff
add a simple unit test for acquire progress
authorDavid Kalnischkies <david@kalnischkies.de>
Sun, 15 Mar 2015 21:34:54 +0000 (22:34 +0100)
committerDavid Kalnischkies <david@kalnischkies.de>
Fri, 10 Apr 2015 20:29:08 +0000 (22:29 +0200)
This isn't testing much of the 'complex' parts,
but its better than nothing for now.

Git-Dch: Ignore

apt-private/acqprogress.cc
apt-private/acqprogress.h
apt-private/private-install.cc
apt-private/private-update.cc
cmdline/apt-get.cc
cmdline/apt-helper.cc
test/libapt/acqprogress_test.cc [new file with mode: 0644]

index e834d7d6ac8e9bcb411565b8154f8d032a5217bf..0c606e48e85b823c9d9e03310f1242070b726115 100644 (file)
@@ -1,10 +1,9 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: acqprogress.cc,v 1.24 2003/04/27 01:56:48 doogie Exp $
 /* ######################################################################
 
-   Acquire Progress - Command line progress meter 
-   
+   Acquire Progress - Command line progress meter
+
    ##################################################################### */
                                                                        /*}}}*/
 // Include files                                                       /*{{{*/
 #include <apti18n.h>
                                                                        /*}}}*/
 
-using namespace std;
-
 // AcqTextStatus::AcqTextStatus - Constructor                          /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int const Quiet) :
-    pkgAcquireStatus(), ScreenWidth(ScreenWidth), LastLineLength(0), ID(0), Quiet(Quiet)
+AcqTextStatus::AcqTextStatus(std::ostream &out, unsigned int &ScreenWidth,unsigned int const Quiet) :
+    pkgAcquireStatus(), out(out), ScreenWidth(ScreenWidth), LastLineLength(0), ID(0), Quiet(Quiet)
 {
    // testcases use it to disable pulses without disabling other user messages
    if (Quiet == 0 && _config->FindB("quiet::NoUpdate", false) == true)
@@ -62,8 +59,8 @@ void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
 
    clearLastLine();
 
-   cout << _("Hit ") << Itm.Description;
-   cout << endl;
+   out << _("Hit ") << Itm.Description;
+   out << std::endl;
    Update = true;
 }
                                                                        /*}}}*/
@@ -83,10 +80,10 @@ void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
 
    clearLastLine();
 
-   cout << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
+   out << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
    if (Itm.Owner->FileSize != 0)
-      cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
-   cout << endl;
+      out << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
+   out << std::endl;
 }
                                                                        /*}}}*/
 // AcqTextStatus::Done - Completed a download                          /*{{{*/
@@ -113,15 +110,15 @@ void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
 
    if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
    {
-      cout << _("Ign ") << Itm.Description << endl;
+      out << _("Ign ") << Itm.Description << std::endl;
       if (Itm.Owner->ErrorText.empty() == false &&
            _config->FindB("Acquire::Progress::Ignore::ShowErrorText", false) == true)
-        cout << "  " << Itm.Owner->ErrorText << endl;
+        out << "  " << Itm.Owner->ErrorText << std::endl;
    }
    else
    {
-      cout << _("Err ") << Itm.Description << endl;
-      cout << "  " << Itm.Owner->ErrorText << endl;
+      out << _("Err ") << Itm.Description << std::endl;
+      out << "  " << Itm.Owner->ErrorText << std::endl;
    }
 
    Update = true;
@@ -143,7 +140,7 @@ void AcqTextStatus::Stop()
       return;
 
    if (FetchedBytes != 0 && _error->PendingError() == false)
-      ioprintf(cout,_("Fetched %sB in %s (%sB/s)\n"),
+      ioprintf(out,_("Fetched %sB in %s (%sB/s)\n"),
               SizeToStr(FetchedBytes).c_str(),
               TimeToStr(ElapsedTime).c_str(),
               SizeToStr(CurrentCPS).c_str());
@@ -152,7 +149,7 @@ void AcqTextStatus::Stop()
 // AcqTextStatus::Pulse - Regular event pulse                          /*{{{*/
 // ---------------------------------------------------------------------
 /* This draws the current progress. Each line has an overall percent
-   meter and a per active item status meter along with an overall 
+   meter and a per active item status meter along with an overall
    bandwidth and ETA indicator. */
 bool AcqTextStatus::Pulse(pkgAcquire *Owner)
 {
@@ -250,14 +247,14 @@ bool AcqTextStatus::Pulse(pkgAcquire *Owner)
 
    // Draw the current status
    if (_config->FindB("Apt::Color", false) == true)
-      cout << _config->Find("APT::Color::Yellow");
+      out << _config->Find("APT::Color::Yellow");
    if (LastLineLength > Line.length())
       clearLastLine();
    else
-      cout << '\r';
-   cout << Line << flush;
+      out << '\r';
+   out << Line << std::flush;
    if (_config->FindB("Apt::Color", false) == true)
-      cout << _config->Find("APT::Color::Neutral") << flush;
+      out << _config->Find("APT::Color::Neutral") << std::flush;
 
    LastLineLength = Line.length();
    Update = false;
@@ -268,7 +265,7 @@ bool AcqTextStatus::Pulse(pkgAcquire *Owner)
 // AcqTextStatus::MediaChange - Media need to be swapped               /*{{{*/
 // ---------------------------------------------------------------------
 /* Prompt for a media swap */
-bool AcqTextStatus::MediaChange(string Media,string Drive)
+bool AcqTextStatus::MediaChange(std::string Media, std::string Drive)
 {
    // If we do not output on a terminal and one of the options to avoid user
    // interaction is given, we assume that no user is present who could react
@@ -281,7 +278,7 @@ bool AcqTextStatus::MediaChange(string Media,string Drive)
       return false;
 
    clearLastLine();
-   ioprintf(cout,_("Media change: please insert the disc labeled\n"
+   ioprintf(out,_("Media change: please insert the disc labeled\n"
                   " '%s'\n"
                   "in the drive '%s' and press enter\n"),
            Media.c_str(),Drive.c_str());
@@ -301,16 +298,16 @@ bool AcqTextStatus::MediaChange(string Media,string Drive)
 }
                                                                        /*}}}*/
 void AcqTextStatus::clearLastLine() {                                  /*{{{*/
-   if (Quiet > 0)
+   if (Quiet > 0 || LastLineLength == 0)
       return;
 
    // do not try to clear more than the (now smaller) screen
    if (LastLineLength > ScreenWidth)
       LastLineLength = ScreenWidth;
 
-   std::cout << '\r';
+   out << '\r';
    for (size_t i = 0; i < LastLineLength; ++i)
-      std::cout << ' ';
-   std::cout << '\r' << std::flush;
+      out << ' ';
+   out << '\r' << std::flush;
 }
                                                                        /*}}}*/
index ab8170126c02b5a44286b79ae8f0319e93f1a4dc..7cf990c65a5ee3eb2534351e9cbaa91ba92b5df0 100644 (file)
 #include <apt-pkg/macros.h>
 
 #include <string>
+#include <iostream>
 
 class APT_PUBLIC AcqTextStatus : public pkgAcquireStatus
 {
+   std::ostream &out;
    unsigned int &ScreenWidth;
    size_t LastLineLength;
    unsigned long ID;
@@ -35,7 +37,7 @@ class APT_PUBLIC AcqTextStatus : public pkgAcquireStatus
 
    bool Pulse(pkgAcquire *Owner);
 
-   AcqTextStatus(unsigned int &ScreenWidth,unsigned int const Quiet);
+   AcqTextStatus(std::ostream &out, unsigned int &ScreenWidth,unsigned int const Quiet);
 };
 
 #endif
index 79a22edf42d883005b1754f53044bbf109707125..acc6d42c28b01371d65ba387c6b60b5a13fdbf2b 100644 (file)
@@ -116,7 +116,7 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask, bool Safety)
       return false;
 
    // Create the download object
-   AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
+   AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
    pkgAcquire Fetcher(&Stat);
    if (_config->FindB("APT::Get::Print-URIs", false) == true)
    {
index df77ac33a91b6230a06c90a9e7b4bab78bbbb4d3..73a82e988f2e468a3ded13e413d03cba44511b46 100644 (file)
@@ -38,8 +38,8 @@ bool DoUpdate(CommandLine &CmdL)
    pkgSourceList *List = Cache.GetSourceList();
 
    // Create the progress
-   AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
-      
+   AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
+
    // Just print out the uris an exit if the --print-uris flag was used
    if (_config->FindB("APT::Get::Print-URIs") == true)
    {
index a0d78257caf1cb663c1dd60f6e220a2055b94f36..1a4f4c5a847d94791146ba09e2c9f36a7aefecae 100644 (file)
@@ -624,7 +624,7 @@ static bool DoDownload(CommandLine &CmdL)
    if (verset.empty() == true)
       return false;
 
-   AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet", 0));
+   AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
    pkgAcquire Fetcher(&Stat);
 
    pkgRecords Recs(Cache);
@@ -729,7 +729,7 @@ static bool DoSource(CommandLine &CmdL)
       return false;
 
    // Create the download object
-   AcqTextStatus Stat(ScreenWidth,_config->FindI("quiet",0));
+   AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
    pkgAcquire Fetcher(&Stat);
 
    SPtrArray<DscFile> Dsc = new DscFile[CmdL.FileSize()];
@@ -1550,7 +1550,7 @@ static bool DoChangelog(CommandLine &CmdL)
       return Success;
    }
 
-   AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
+   AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
    Fetcher.SetLog(&Stat);
 
    bool const downOnly = _config->FindB("APT::Get::Download-Only", false);
index 65db857adaaf9fa8558d7f1dddc659b7b0e1b510..a97fc903f35f33bf225621cbd6a22b0bd34dde7f 100644 (file)
@@ -48,7 +48,7 @@ static bool DoDownloadFile(CommandLine &CmdL)
    if (CmdL.FileSize() <= 2)
       return _error->Error(_("Must specify at least one pair url/filename"));
 
-   AcqTextStatus Stat(ScreenWidth, _config->FindI("quiet",0));
+   AcqTextStatus Stat(std::cout, ScreenWidth,_config->FindI("quiet",0));
    pkgAcquire Fetcher(&Stat);
 
    size_t fileind = 0;
diff --git a/test/libapt/acqprogress_test.cc b/test/libapt/acqprogress_test.cc
new file mode 100644 (file)
index 0000000..288e05a
--- /dev/null
@@ -0,0 +1,170 @@
+#include <config.h>
+#include <apt-pkg/acquire.h>
+#include <apt-pkg/acquire-item.h>
+#include <apt-pkg/configuration.h>
+#include <apt-private/acqprogress.h>
+#include <string>
+#include <sstream>
+#include <gtest/gtest.h>
+
+class TestItem: public pkgAcquire::Item
+{
+public:
+   TestItem(pkgAcquire * const Acq) : pkgAcquire::Item(Acq, "", NULL) {}
+
+   virtual std::string DescURI() { return ""; }
+
+};
+
+TEST(AcqProgress, IMSHit)
+{
+   std::ostringstream out;
+   unsigned int width = 80;
+   AcqTextStatus Stat(out, width, 0);
+   Stat.Start();
+
+   pkgAcquire::ItemDesc hit;
+   hit.URI = "http://example.org/file";
+   hit.Description = "Example File from example.org";
+   hit.ShortDesc = "Example File";
+   hit.Owner = NULL;
+
+   EXPECT_EQ("", out.str());
+   Stat.IMSHit(hit);
+   EXPECT_EQ("Hit Example File from example.org\n", out.str());
+   Stat.IMSHit(hit);
+   EXPECT_EQ("Hit Example File from example.org\n"
+            "Hit Example File from example.org\n", out.str());
+   Stat.Stop();
+   EXPECT_EQ("Hit Example File from example.org\n"
+            "Hit Example File from example.org\n", out.str());
+}
+TEST(AcqProgress, FetchNoFileSize)
+{
+   std::ostringstream out;
+   unsigned int width = 80;
+   AcqTextStatus Stat(out, width, 0);
+   Stat.Start();
+
+   pkgAcquire Acq(&Stat);
+   pkgAcquire::ItemDesc fetch;
+   fetch.URI = "http://example.org/file";
+   fetch.Description = "Example File from example.org";
+   fetch.ShortDesc = "Example File";
+   TestItem fetchO(&Acq);
+   fetch.Owner = &fetchO;
+
+   EXPECT_EQ("", out.str());
+   Stat.Fetch(fetch);
+   EXPECT_EQ("Get:1 Example File from example.org\n", out.str());
+   Stat.Fetch(fetch);
+   EXPECT_EQ("Get:1 Example File from example.org\n"
+            "Get:2 Example File from example.org\n", out.str());
+   Stat.Stop();
+   EXPECT_EQ("Get:1 Example File from example.org\n"
+            "Get:2 Example File from example.org\n", out.str());
+}
+TEST(AcqProgress, FetchFileSize)
+{
+   std::ostringstream out;
+   unsigned int width = 80;
+   AcqTextStatus Stat(out, width, 0);
+   Stat.Start();
+
+   pkgAcquire Acq(&Stat);
+   pkgAcquire::ItemDesc fetch;
+   fetch.URI = "http://example.org/file";
+   fetch.Description = "Example File from example.org";
+   fetch.ShortDesc = "Example File";
+   TestItem fetchO(&Acq);
+   fetchO.FileSize = 100;
+   fetch.Owner = &fetchO;
+
+   EXPECT_EQ("", out.str());
+   Stat.Fetch(fetch);
+   EXPECT_EQ("Get:1 Example File from example.org [100 B]\n", out.str());
+   fetchO.FileSize = 42;
+   Stat.Fetch(fetch);
+   EXPECT_EQ("Get:1 Example File from example.org [100 B]\n"
+            "Get:2 Example File from example.org [42 B]\n", out.str());
+   Stat.Stop();
+   EXPECT_EQ("Get:1 Example File from example.org [100 B]\n"
+            "Get:2 Example File from example.org [42 B]\n", out.str());
+}
+TEST(AcqProgress, Fail)
+{
+   std::ostringstream out;
+   unsigned int width = 80;
+   AcqTextStatus Stat(out, width, 0);
+   Stat.Start();
+
+   pkgAcquire Acq(&Stat);
+   pkgAcquire::ItemDesc fetch;
+   fetch.URI = "http://example.org/file";
+   fetch.Description = "Example File from example.org";
+   fetch.ShortDesc = "Example File";
+   TestItem fetchO(&Acq);
+   fetchO.FileSize = 100;
+   fetchO.Status = pkgAcquire::Item::StatIdle;
+   fetch.Owner = &fetchO;
+
+   EXPECT_EQ("", out.str());
+   Stat.Fail(fetch);
+   EXPECT_EQ("", out.str());
+   fetchO.Status = pkgAcquire::Item::StatDone;
+   Stat.Fail(fetch);
+   EXPECT_EQ("Ign Example File from example.org\n", out.str());
+   fetchO.Status = pkgAcquire::Item::StatError;
+   fetchO.ErrorText = "An error test!";
+   Stat.Fail(fetch);
+   EXPECT_EQ("Ign Example File from example.org\n"
+            "Err Example File from example.org\n"
+            "  An error test!\n", out.str());
+   _config->Set("Acquire::Progress::Ignore::ShowErrorText", true);
+   fetchO.Status = pkgAcquire::Item::StatDone;
+   Stat.Fail(fetch);
+   EXPECT_EQ("Ign Example File from example.org\n"
+            "Err Example File from example.org\n"
+            "  An error test!\n"
+            "Ign Example File from example.org\n"
+            "  An error test!\n", out.str());
+   _config->Set("Acquire::Progress::Ignore::ShowErrorText", true);
+   Stat.Stop();
+   EXPECT_EQ("Ign Example File from example.org\n"
+            "Err Example File from example.org\n"
+            "  An error test!\n"
+            "Ign Example File from example.org\n"
+            "  An error test!\n", out.str());
+}
+TEST(AcqProgress, Pulse)
+{
+   std::ostringstream out;
+   unsigned int width = 80;
+   AcqTextStatus Stat(out, width, 0);
+   _config->Set("APT::Sandbox::User", ""); // ensure we aren't sandboxing
+
+   pkgAcquire Acq(&Stat);
+   pkgAcquire::ItemDesc fetch;
+   fetch.URI = "http://example.org/file";
+   fetch.Description = "Example File from example.org";
+   fetch.ShortDesc = "Example File";
+   TestItem fetchO(&Acq);
+   fetchO.FileSize = 100;
+   fetchO.Status = pkgAcquire::Item::StatFetching;
+   fetch.Owner = &fetchO;
+
+   // make screen smaller and bigger again while running
+   EXPECT_TRUE(Stat.Pulse(&Acq));
+   EXPECT_EQ("\r0% [Working]", out.str());
+   width = 8;
+   EXPECT_TRUE(Stat.Pulse(&Acq));
+   EXPECT_EQ("\r0% [Working]"
+            "\r        "
+            "\r0% [Work", out.str());
+   width = 80;
+   EXPECT_TRUE(Stat.Pulse(&Acq));
+   EXPECT_EQ("\r0% [Working]"
+            "\r        "
+            "\r0% [Work"
+            "\r0% [Working]", out.str());
+}