]> git.saurik.com Git - apt.git/blob - test/libapt/commandline_test.cc
7f6c511df2056967b10b53fbc61ec1b23d89ecd2
[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 bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) {return false;}
21 std::vector<aptDispatchWithHelp> GetCommands() {return {};}
22
23
24 TEST(CommandLineTest,SaveInConfig)
25 {
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");
35 #undef APT_EXPECT_CMD
36 }
37 TEST(CommandLineTest,Parsing)
38 {
39 CommandLine::Args Args[] = {
40 { 't', 0, "Test::Worked", 0 },
41 { 'z', "zero", "Test::Zero", 0 },
42 {0,0,0,0}
43 };
44 ::Configuration c;
45 CommandLine CmdL(Args, &c);
46
47 char const * argv[] = { "test", "--zero", "-t" };
48 CmdL.Parse(3 , argv);
49 EXPECT_TRUE(c.FindB("Test::Worked", false));
50 EXPECT_TRUE(c.FindB("Test::Zero", false));
51
52 c.Clear("Test");
53 EXPECT_FALSE(c.FindB("Test::Worked", false));
54 EXPECT_FALSE(c.FindB("Test::Zero", false));
55
56 c.Set("Test::Zero", true);
57 EXPECT_TRUE(c.FindB("Test::Zero", false));
58
59 char const * argv2[] = { "test", "--no-zero", "-t" };
60 CmdL.Parse(3 , argv2);
61 EXPECT_TRUE(c.FindB("Test::Worked", false));
62 EXPECT_FALSE(c.FindB("Test::Zero", false));
63 }
64
65 TEST(CommandLineTest, BoolParsing)
66 {
67 CommandLine::Args Args[] = {
68 { 't', 0, "Test::Worked", 0 },
69 {0,0,0,0}
70 };
71 ::Configuration c;
72 CommandLine CmdL(Args, &c);
73
74 // the commandline parser used to use strtol() on the argument
75 // to check if the argument is a boolean expression - that
76 // stopped after the "0". this test ensures that we always check
77 // that the entire string was consumed by strtol
78 {
79 char const * argv[] = { "show", "-t", "0ad" };
80 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
81 EXPECT_TRUE(res);
82 ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad");
83 }
84
85 {
86 char const * argv[] = { "show", "-t", "0", "ad" };
87 bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv);
88 EXPECT_TRUE(res);
89 ASSERT_EQ(std::string(CmdL.FileList[0]), "ad");
90 }
91
92 }
93
94 bool DoVoid(CommandLine &) { return false; }
95
96 TEST(CommandLineTest,GetCommand)
97 {
98 CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} };
99 {
100 char const * argv[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" };
101 char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
102 EXPECT_STREQ("remove", com);
103 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
104 ::Configuration c;
105 CommandLine CmdL(Args.data(), &c);
106 ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
107 EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
108 EXPECT_TRUE(c.FindB("APT::Get::Download-Only"));
109 ASSERT_EQ(2, CmdL.FileSize());
110 EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
111 EXPECT_EQ(std::string(CmdL.FileList[1]), "foo");
112 }
113 {
114 char const * argv[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" };
115 char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
116 EXPECT_STREQ("remove", com);
117 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
118 ::Configuration c;
119 CommandLine CmdL(Args.data(), &c);
120 ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
121 EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
122 EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
123 ASSERT_EQ(3, CmdL.FileSize());
124 EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
125 EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
126 EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
127 }
128 {
129 char const * argv[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" };
130 char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
131 EXPECT_STREQ("remove", com);
132 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
133 ::Configuration c;
134 CommandLine CmdL(Args.data(), &c);
135 ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
136 EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
137 EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
138 ASSERT_EQ(CmdL.FileSize(), 3);
139 EXPECT_EQ(std::string(CmdL.FileList[0]), "remove");
140 EXPECT_EQ(std::string(CmdL.FileList[1]), "-d");
141 EXPECT_EQ(std::string(CmdL.FileList[2]), "foo");
142 }
143 {
144 char const * argv[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" };
145 char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv);
146 EXPECT_STREQ("install", com);
147 std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com);
148 ::Configuration c;
149 CommandLine CmdL(Args.data(), &c);
150 ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv));
151 EXPECT_EQ(c.Find("APT::Default-Release"), "unstable");
152 EXPECT_FALSE(c.FindB("APT::Get::Download-Only"));
153 ASSERT_EQ(CmdL.FileSize(), 4);
154 EXPECT_EQ(std::string(CmdL.FileList[0]), "install");
155 EXPECT_EQ(std::string(CmdL.FileList[1]), "remove");
156 EXPECT_EQ(std::string(CmdL.FileList[2]), "-d");
157 EXPECT_EQ(std::string(CmdL.FileList[3]), "foo");
158 }
159 }