]>
git.saurik.com Git - apt.git/blob - test/libapt/commandline_test.cc
3 #include <apt-pkg/cmndline.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-private/private-cmndline.h>
7 #include <gtest/gtest.h>
9 class CLT
: public CommandLine
{
11 std::string
static AsString(const char * const * const argv
,
12 unsigned int const argc
) {
13 std::string
const static conf
= "Commandline::AsString";
15 SaveInConfig(argc
, argv
);
16 return _config
->Find(conf
);
20 #define EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
22 TEST(CommandLineTest
,SaveInConfig
)
24 EXPECT_CMD("apt-get install -sf",
25 "apt-get", "install", "-sf");
26 EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
27 "apt-cache", "-s", "apt", "-so", "Debug::test=Test");
28 EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"",
29 "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test");
30 EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
31 "apt-cache", "-s", "apt", "--hallo", "test=1.0");
33 TEST(CommandLineTest
,Parsing
)
35 CommandLine::Args Args
[] = {
36 { 't', 0, "Test::Worked", 0 },
37 { 'z', "zero", "Test::Zero", 0 },
41 CommandLine
CmdL(Args
, &c
);
43 char const * argv
[] = { "test", "--zero", "-t" };
45 EXPECT_TRUE(c
.FindB("Test::Worked", false));
46 EXPECT_TRUE(c
.FindB("Test::Zero", false));
49 EXPECT_FALSE(c
.FindB("Test::Worked", false));
50 EXPECT_FALSE(c
.FindB("Test::Zero", false));
52 c
.Set("Test::Zero", true);
53 EXPECT_TRUE(c
.FindB("Test::Zero", false));
55 char const * argv2
[] = { "test", "--no-zero", "-t" };
56 CmdL
.Parse(3 , argv2
);
57 EXPECT_TRUE(c
.FindB("Test::Worked", false));
58 EXPECT_FALSE(c
.FindB("Test::Zero", false));
61 TEST(CommandLineTest
, BoolParsing
)
63 CommandLine::Args Args
[] = {
64 { 't', 0, "Test::Worked", 0 },
68 CommandLine
CmdL(Args
, &c
);
70 // the commandline parser used to use strtol() on the argument
71 // to check if the argument is a boolean expression - that
72 // stopped after the "0". this test ensures that we always check
73 // that the entire string was consumed by strtol
75 char const * argv
[] = { "show", "-t", "0ad" };
76 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
78 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "0ad");
82 char const * argv
[] = { "show", "-t", "0", "ad" };
83 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
85 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "ad");
90 bool DoVoid(CommandLine
&) { return false; }
92 TEST(CommandLineTest
,GetCommand
)
94 CommandLine::Dispatch Cmds
[] = { {"install",&DoVoid
}, {"remove", &DoVoid
}, {0,0} };
96 char const * argv
[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
97 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
98 EXPECT_STREQ("remove", com
);
99 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-get", com
);
101 CommandLine
CmdL(Args
.data(), &c
);
102 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
103 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
104 EXPECT_TRUE(c
.FindB("APT::Get::Download-Only"));
105 ASSERT_EQ(2, CmdL
.FileSize());
106 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
107 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "foo");
110 char const * argv
[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
111 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
112 EXPECT_STREQ("remove", com
);
113 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-get", com
);
115 CommandLine
CmdL(Args
.data(), &c
);
116 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
117 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
118 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
119 ASSERT_EQ(3, CmdL
.FileSize());
120 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
121 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "-d");
122 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "foo");
125 char const * argv
[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
126 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
127 EXPECT_STREQ("remove", com
);
128 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-get", com
);
130 CommandLine
CmdL(Args
.data(), &c
);
131 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
132 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
133 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
134 ASSERT_EQ(CmdL
.FileSize(), 3);
135 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
136 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "-d");
137 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "foo");
140 char const * argv
[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
141 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
142 EXPECT_STREQ("install", com
);
143 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-get", com
);
145 CommandLine
CmdL(Args
.data(), &c
);
146 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
147 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
148 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
149 ASSERT_EQ(CmdL
.FileSize(), 4);
150 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "install");
151 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "remove");
152 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "-d");
153 EXPECT_EQ(std::string(CmdL
.FileList
[3]), "foo");