]>
Commit | Line | Data |
---|---|---|
453b82a3 DK |
1 | #include <config.h> |
2 | ||
ae2be086 | 3 | #include <apt-pkg/cmndline.h> |
453b82a3 | 4 | #include <apt-pkg/configuration.h> |
c505fa33 | 5 | #include <apt-private/private-cmndline.h> |
ae2be086 | 6 | |
f00832cc DK |
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 | #define EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); } | |
21 | ||
22 | TEST(CommandLineTest,SaveInConfig) | |
23 | { | |
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"); | |
32 | } | |
33 | TEST(CommandLineTest,Parsing) | |
ae2be086 DH |
34 | { |
35 | CommandLine::Args Args[] = { | |
36 | { 't', 0, "Test::Worked", 0 }, | |
37 | { 'z', "zero", "Test::Zero", 0 }, | |
38 | {0,0,0,0} | |
39 | }; | |
f00832cc DK |
40 | ::Configuration c; |
41 | CommandLine CmdL(Args, &c); | |
7a6d9076 | 42 | |
ae2be086 DH |
43 | char const * argv[] = { "test", "--zero", "-t" }; |
44 | CmdL.Parse(3 , argv); | |
f00832cc DK |
45 | EXPECT_TRUE(c.FindB("Test::Worked", false)); |
46 | EXPECT_TRUE(c.FindB("Test::Zero", false)); | |
ae2be086 | 47 | |
f00832cc DK |
48 | c.Clear("Test"); |
49 | EXPECT_FALSE(c.FindB("Test::Worked", false)); | |
50 | EXPECT_FALSE(c.FindB("Test::Zero", false)); | |
7a6d9076 | 51 | |
f00832cc DK |
52 | c.Set("Test::Zero", true); |
53 | EXPECT_TRUE(c.FindB("Test::Zero", false)); | |
7a6d9076 DK |
54 | |
55 | char const * argv2[] = { "test", "--no-zero", "-t" }; | |
56 | CmdL.Parse(3 , argv2); | |
f00832cc DK |
57 | EXPECT_TRUE(c.FindB("Test::Worked", false)); |
58 | EXPECT_FALSE(c.FindB("Test::Zero", false)); | |
ae2be086 | 59 | } |
08be0ca3 MV |
60 | |
61 | TEST(CommandLineTest, BoolParsing) | |
62 | { | |
63 | CommandLine::Args Args[] = { | |
64 | { 't', 0, "Test::Worked", 0 }, | |
65 | {0,0,0,0} | |
66 | }; | |
67 | ::Configuration c; | |
68 | CommandLine CmdL(Args, &c); | |
69 | ||
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 | |
74 | { | |
75 | char const * argv[] = { "show", "-t", "0ad" }; | |
76 | bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv); | |
77 | EXPECT_TRUE(res); | |
78 | ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad"); | |
79 | } | |
80 | ||
81 | { | |
82 | char const * argv[] = { "show", "-t", "0", "ad" }; | |
83 | bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv); | |
84 | EXPECT_TRUE(res); | |
85 | ASSERT_EQ(std::string(CmdL.FileList[0]), "ad"); | |
86 | } | |
87 | ||
88 | } | |
c505fa33 DK |
89 | |
90 | bool DoVoid(CommandLine &) { return false; } | |
91 | ||
92 | TEST(CommandLineTest,GetCommand) | |
93 | { | |
94 | CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} }; | |
95 | { | |
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); | |
100 | ::Configuration c; | |
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"); | |
108 | } | |
109 | { | |
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); | |
114 | ::Configuration c; | |
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"); | |
123 | } | |
124 | { | |
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); | |
129 | ::Configuration c; | |
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"); | |
138 | } | |
139 | { | |
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); | |
144 | ::Configuration c; | |
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"); | |
154 | } | |
155 | } |