]> git.saurik.com Git - apt.git/blob - test/libapt/commandline_test.cc
show the conflicting distribution warning again
[apt.git] / test / libapt / commandline_test.cc
1 #include <config.h>
2
3 #include <apt-pkg/cmndline.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-private/private-cmndline.h>
6
7 #include <gtest/gtest.h>
8
9 class CLT: public CommandLine {
10 public:
11 std::string static AsString(const char * const * const argv,
12 unsigned int const argc) {
13 std::string const static conf = "Commandline::AsString";
14 _config->Clear(conf);
15 SaveInConfig(argc, argv);
16 return _config->Find(conf);
17 }
18 };
19
20 TEST(CommandLineTest,SaveInConfig)
21 {
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");
31 #undef APT_EXPECT_CMD
32 }
33 TEST(CommandLineTest,Parsing)
34 {
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 },
40 {0,0,0,0}
41 };
42 ::Configuration c;
43 CommandLine CmdL(Args, &c);
44
45 char const * argv[] = { "test", "--zero", "-t" };
46 CmdL.Parse(3 , argv);
47 EXPECT_TRUE(c.FindB("Test::Worked", false));
48 EXPECT_TRUE(c.FindB("Test::Zero", false));
49
50 c.Clear("Test");
51 EXPECT_FALSE(c.FindB("Test::Worked", false));
52 EXPECT_FALSE(c.FindB("Test::Zero", false));
53
54 c.Set("Test::Zero", true);
55 EXPECT_TRUE(c.FindB("Test::Zero", false));
56
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));
61
62 c.Clear("Test");
63 {
64 char const * argv[] = { "test", "-T", "yes" };
65 CmdL.Parse(3 , argv);
66 EXPECT_TRUE(c.FindB("Test::Worked", false));
67 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
68 EXPECT_EQ(0, CmdL.FileSize());
69 }
70 c.Clear("Test");
71 {
72 char const * argv[] = { "test", "-T=yes" };
73 CmdL.Parse(2 , argv);
74 EXPECT_TRUE(c.Exists("Test::Worked"));
75 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
76 EXPECT_EQ(0, CmdL.FileSize());
77 }
78 c.Clear("Test");
79 {
80 char const * argv[] = { "test", "-T=", "yes" };
81 CmdL.Parse(3 , argv);
82 EXPECT_TRUE(c.Exists("Test::Worked"));
83 EXPECT_EQ("no", c.Find("Test::Worked", "no"));
84 EXPECT_EQ(1, CmdL.FileSize());
85 }
86
87 c.Clear("Test");
88 {
89 char const * argv[] = { "test", "--testing", "yes" };
90 CmdL.Parse(3 , argv);
91 EXPECT_TRUE(c.FindB("Test::Worked", false));
92 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
93 EXPECT_EQ(0, CmdL.FileSize());
94 }
95 c.Clear("Test");
96 {
97 char const * argv[] = { "test", "--testing=yes" };
98 CmdL.Parse(2 , argv);
99 EXPECT_TRUE(c.Exists("Test::Worked"));
100 EXPECT_EQ("yes", c.Find("Test::Worked", "no"));
101 EXPECT_EQ(0, CmdL.FileSize());
102 }
103 c.Clear("Test");
104 {
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());
110 }
111
112 c.Clear("Test");
113 {
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"));
118 }
119 c.Clear("Test");
120 {
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"));
125 }
126 c.Clear("Test");
127 {
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"));
132 }
133 c.Clear("Test");
134 }
135
136 TEST(CommandLineTest, BoolParsing)
137 {
138 CommandLine::Args Args[] = {
139 { 't', 0, "Test::Worked", 0 },
140 {0,0,0,0}
141 };
142 ::Configuration c;
143 CommandLine CmdL(Args, &c);
144
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
149 {
150 char const * argv[] = { "show", "-t", "0ad" };
151 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
152 EXPECT_TRUE(res);
153 ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad");
154 }
155
156 {
157 char const * argv[] = { "show", "-t", "0", "ad" };
158 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
159 EXPECT_TRUE(res);
160 ASSERT_EQ(std::string(CmdL.FileList[0]), "ad");
161 }
162
163 }
164
165 static bool DoVoid(CommandLine &) { return false; }
166
167 TEST(CommandLineTest,GetCommand)
168 {
169 CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} };
170 {
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);
175 ::Configuration c;
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");
183 }
184 {
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);
189 ::Configuration c;
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");
198 }
199 {
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);
204 ::Configuration c;
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");
213 }
214 {
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);
219 ::Configuration c;
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");
229 }
230 }