]>
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 bool ShowHelp(CommandLine
&) {return false;}
21 std::vector
<aptDispatchWithHelp
> GetCommands() {return {};}
24 TEST(CommandLineTest
,SaveInConfig
)
26 #define APT_EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }
27 APT_EXPECT_CMD("apt-get install -sf",
28 "apt-get", "install", "-sf");
29 APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
30 "apt-cache", "-s", "apt", "-so", "Debug::test=Test");
31 APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"",
32 "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test");
33 APT_EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
34 "apt-cache", "-s", "apt", "--hallo", "test=1.0");
37 TEST(CommandLineTest
,Parsing
)
39 CommandLine::Args Args
[] = {
40 { 't', 0, "Test::Worked", 0 },
41 { 'T', "testing", "Test::Worked", CommandLine::HasArg
},
42 { 'z', "zero", "Test::Zero", 0 },
43 { 'o', "option", 0, CommandLine::ArbItem
},
47 CommandLine
CmdL(Args
, &c
);
49 char const * argv
[] = { "test", "--zero", "-t" };
51 EXPECT_TRUE(c
.FindB("Test::Worked", false));
52 EXPECT_TRUE(c
.FindB("Test::Zero", false));
55 EXPECT_FALSE(c
.FindB("Test::Worked", false));
56 EXPECT_FALSE(c
.FindB("Test::Zero", false));
58 c
.Set("Test::Zero", true);
59 EXPECT_TRUE(c
.FindB("Test::Zero", false));
61 char const * argv2
[] = { "test", "--no-zero", "-t" };
62 CmdL
.Parse(3 , argv2
);
63 EXPECT_TRUE(c
.FindB("Test::Worked", false));
64 EXPECT_FALSE(c
.FindB("Test::Zero", false));
68 char const * argv
[] = { "test", "-T", "yes" };
70 EXPECT_TRUE(c
.FindB("Test::Worked", false));
71 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
72 EXPECT_EQ(0, CmdL
.FileSize());
76 char const * argv
[] = { "test", "-T=yes" };
78 EXPECT_TRUE(c
.Exists("Test::Worked"));
79 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
80 EXPECT_EQ(0, CmdL
.FileSize());
84 char const * argv
[] = { "test", "-T=", "yes" };
86 EXPECT_TRUE(c
.Exists("Test::Worked"));
87 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
88 EXPECT_EQ(1, CmdL
.FileSize());
93 char const * argv
[] = { "test", "--testing", "yes" };
95 EXPECT_TRUE(c
.FindB("Test::Worked", false));
96 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
97 EXPECT_EQ(0, CmdL
.FileSize());
101 char const * argv
[] = { "test", "--testing=yes" };
102 CmdL
.Parse(2 , argv
);
103 EXPECT_TRUE(c
.Exists("Test::Worked"));
104 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
105 EXPECT_EQ(0, CmdL
.FileSize());
109 char const * argv
[] = { "test", "--testing=", "yes" };
110 CmdL
.Parse(3 , argv
);
111 EXPECT_TRUE(c
.Exists("Test::Worked"));
112 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
113 EXPECT_EQ(1, CmdL
.FileSize());
118 char const * argv
[] = { "test", "-o", "test::worked=yes" };
119 CmdL
.Parse(3 , argv
);
120 EXPECT_TRUE(c
.FindB("Test::Worked", false));
121 EXPECT_EQ("yes", c
.Find("Test::Worked", "no"));
125 char const * argv
[] = { "test", "-o", "test::worked=" };
126 CmdL
.Parse(3 , argv
);
127 EXPECT_TRUE(c
.Exists("Test::Worked"));
128 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
132 char const * argv
[] = { "test", "-o", "test::worked=", "yes" };
133 CmdL
.Parse(4 , argv
);
134 EXPECT_TRUE(c
.Exists("Test::Worked"));
135 EXPECT_EQ("no", c
.Find("Test::Worked", "no"));
140 TEST(CommandLineTest
, BoolParsing
)
142 CommandLine::Args Args
[] = {
143 { 't', 0, "Test::Worked", 0 },
147 CommandLine
CmdL(Args
, &c
);
149 // the commandline parser used to use strtol() on the argument
150 // to check if the argument is a boolean expression - that
151 // stopped after the "0". this test ensures that we always check
152 // that the entire string was consumed by strtol
154 char const * argv
[] = { "show", "-t", "0ad" };
155 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
157 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "0ad");
161 char const * argv
[] = { "show", "-t", "0", "ad" };
162 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
164 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "ad");
169 bool DoVoid(CommandLine
&) { return false; }
171 TEST(CommandLineTest
,GetCommand
)
173 CommandLine::Dispatch Cmds
[] = { {"install",&DoVoid
}, {"remove", &DoVoid
}, {0,0} };
175 char const * argv
[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
176 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
177 EXPECT_STREQ("remove", com
);
178 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
180 CommandLine
CmdL(Args
.data(), &c
);
181 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
182 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
183 EXPECT_TRUE(c
.FindB("APT::Get::Download-Only"));
184 ASSERT_EQ(2, CmdL
.FileSize());
185 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
186 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "foo");
189 char const * argv
[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
190 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
191 EXPECT_STREQ("remove", com
);
192 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
194 CommandLine
CmdL(Args
.data(), &c
);
195 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
196 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
197 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
198 ASSERT_EQ(3, CmdL
.FileSize());
199 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
200 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "-d");
201 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "foo");
204 char const * argv
[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
205 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
206 EXPECT_STREQ("remove", com
);
207 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
209 CommandLine
CmdL(Args
.data(), &c
);
210 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
211 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
212 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
213 ASSERT_EQ(CmdL
.FileSize(), 3);
214 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "remove");
215 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "-d");
216 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "foo");
219 char const * argv
[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
220 char const * com
= CommandLine::GetCommand(Cmds
, sizeof(argv
)/sizeof(argv
[0]), argv
);
221 EXPECT_STREQ("install", com
);
222 std::vector
<CommandLine::Args
> Args
= getCommandArgs(APT_CMD::APT_GET
, com
);
224 CommandLine
CmdL(Args
.data(), &c
);
225 ASSERT_TRUE(CmdL
.Parse(sizeof(argv
)/sizeof(argv
[0]), argv
));
226 EXPECT_EQ(c
.Find("APT::Default-Release"), "unstable");
227 EXPECT_FALSE(c
.FindB("APT::Get::Download-Only"));
228 ASSERT_EQ(CmdL
.FileSize(), 4);
229 EXPECT_EQ(std::string(CmdL
.FileList
[0]), "install");
230 EXPECT_EQ(std::string(CmdL
.FileList
[1]), "remove");
231 EXPECT_EQ(std::string(CmdL
.FileList
[2]), "-d");
232 EXPECT_EQ(std::string(CmdL
.FileList
[3]), "foo");