]>
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 TEST(CommandLineTest
,SaveInConfig
)
22 #define APT_EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
23 APT_EXPECT_CMD("apt-get install -sf",
24 "apt-get", "install", "-sf");
25 APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
26 "apt-cache", "-s", "apt", "-so", "Debug::test=Test");
27 APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"",
28 "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test");
29 APT_EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
30 "apt-cache", "-s", "apt", "--hallo", "test=1.0");
33 TEST(CommandLineTest
,Parsing
)
35 CommandLine::Args Args
[] = {
36 { 't', 0, "Test::Worked", 0 },
37 { 'T', "testing", "Test::Worked", CommandLine::HasArg
},
38 { 'z', "zero", "Test::Zero", 0 },
39 { 'o', "option", 0, CommandLine::ArbItem
},
43 CommandLine
CmdL(Args
, &c
);
45 char const * argv
[] = { "test", "--zero", "-t" };
47 EXPECT_TRUE(c
.FindB("Test::Worked", false));
48 EXPECT_TRUE(c
.FindB("Test::Zero", false));
51 EXPECT_FALSE(c
.FindB("Test::Worked", false));
52 EXPECT_FALSE(c
.FindB("Test::Zero", false));
54 c
.Set("Test::Zero", true);
55 EXPECT_TRUE(c
.FindB("Test::Zero", false));
57 char const * argv2
[] = { "test", "--no-zero", "-t" };
58 CmdL
.Parse(3 , argv2
);
59 EXPECT_TRUE(c
.FindB("Test::Worked", false));
60 EXPECT_FALSE(c
.FindB("Test::Zero", false));
64 char const * argv
[] = { "test", "-T", "yes" };
66 EXPECT_TRUE(c
.FindB("Test::Worked", false));
67 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
68 EXPECT_EQ(0, CmdL
.FileSize());
72 char const * argv
[] = { "test", "-T=yes" };
74 EXPECT_TRUE(c
.Exists("Test::Worked"));
75 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
76 EXPECT_EQ(0, CmdL
.FileSize());
80 char const * argv
[] = { "test", "-T=", "yes" };
82 EXPECT_TRUE(c
.Exists("Test::Worked"));
83 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
84 EXPECT_EQ(1, CmdL
.FileSize());
89 char const * argv
[] = { "test", "--testing", "yes" };
91 EXPECT_TRUE(c
.FindB("Test::Worked", false));
92 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
93 EXPECT_EQ(0, CmdL
.FileSize());
97 char const * argv
[] = { "test", "--testing=yes" };
99 EXPECT_TRUE(c
.Exists("Test::Worked"));
100 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
101 EXPECT_EQ(0, CmdL
.FileSize());
105 char const * argv
[] = { "test", "--testing=", "yes" };
106 CmdL
.Parse(3 , argv
);
107 EXPECT_TRUE(c
.Exists("Test::Worked"));
108 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
109 EXPECT_EQ(1, CmdL
.FileSize());
114 char const * argv
[] = { "test", "-o", "test::worked=yes" };
115 CmdL
.Parse(3 , argv
);
116 EXPECT_TRUE(c
.FindB("Test::Worked", false));
117 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
121 char const * argv
[] = { "test", "-o", "test::worked=" };
122 CmdL
.Parse(3 , argv
);
123 EXPECT_TRUE(c
.Exists("Test::Worked"));
124 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
128 char const * argv
[] = { "test", "-o", "test::worked=", "yes" };
129 CmdL
.Parse(4 , argv
);
130 EXPECT_TRUE(c
.Exists("Test::Worked"));
131 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
136 TEST(CommandLineTest
, BoolParsing
)
138 CommandLine::Args Args
[] = {
139 { 't', 0, "Test::Worked", 0 },
143 CommandLine
CmdL(Args
, &c
);
145 // the commandline parser used to use strtol() on the argument
146 // to check if the argument is a boolean expression - that
147 // stopped after the "0". this test ensures that we always check
148 // that the entire string was consumed by strtol
150 char const * argv
[] = { "show", "-t", "0ad" };
151 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
153 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "0ad");
157 char const * argv
[] = { "show", "-t", "0", "ad" };
158 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
160 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "ad");
165 static bool DoVoid(CommandLine
&) { return false; }
167 TEST(CommandLineTest
,GetCommand
)
169 CommandLine::Dispatch Cmds
[] = { {"install",&DoVoid
}, {"remove", &DoVoid
}, {0,0} };
171 char const * argv
[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
172 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
173 EXPECT_STREQ("remove", com
);
174 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
176 CommandLine
CmdL(Args
.data(), &c
);
177 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
178 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
179 EXPECT_TRUE(c
.FindB("APT::Get::Download-Only"));
180 ASSERT_EQ(2, CmdL
.FileSize());
181 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
182 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "foo");
185 char const * argv
[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
186 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
187 EXPECT_STREQ("remove", com
);
188 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
190 CommandLine
CmdL(Args
.data(), &c
);
191 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
192 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
193 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
194 ASSERT_EQ(3, CmdL
.FileSize());
195 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
196 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "-d");
197 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "foo");
200 char const * argv
[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
201 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
202 EXPECT_STREQ("remove", com
);
203 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
205 CommandLine
CmdL(Args
.data(), &c
);
206 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
207 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
208 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
209 ASSERT_EQ(CmdL
.FileSize(), 3);
210 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
211 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "-d");
212 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "foo");
215 char const * argv
[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
216 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
217 EXPECT_STREQ("install", com
);
218 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
220 CommandLine
CmdL(Args
.data(), &c
);
221 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
222 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
223 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
224 ASSERT_EQ(CmdL
.FileSize(), 4);
225 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "install");
226 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "remove");
227 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "-d");
228 EXPECT_EQ(std::string(CmdL
.FileList
[3]), "foo");