]>
Commit | Line | Data |
---|---|---|
453b82a3 DK |
1 | #include <config.h> |
2 | ||
ae2be086 | 3 | #include <apt-pkg/cmndline.h> |
453b82a3 | 4 | #include <apt-pkg/configuration.h> |
c4b91cbe | 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 | ||
8561c2fe | 20 | bool ShowHelp(CommandLine &) {return false;} |
6079b276 DK |
21 | std::vector<aptDispatchWithHelp> GetCommands() {return {};} |
22 | ||
f00832cc DK |
23 | |
24 | TEST(CommandLineTest,SaveInConfig) | |
25 | { | |
6079b276 DK |
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", | |
f00832cc | 28 | "apt-get", "install", "-sf"); |
6079b276 | 29 | APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=Test", |
f00832cc | 30 | "apt-cache", "-s", "apt", "-so", "Debug::test=Test"); |
6079b276 | 31 | APT_EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"", |
f00832cc | 32 | "apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test"); |
6079b276 | 33 | APT_EXPECT_CMD("apt-cache -s apt --hallo test=1.0", |
f00832cc | 34 | "apt-cache", "-s", "apt", "--hallo", "test=1.0"); |
6079b276 | 35 | #undef APT_EXPECT_CMD |
f00832cc DK |
36 | } |
37 | TEST(CommandLineTest,Parsing) | |
ae2be086 DH |
38 | { |
39 | CommandLine::Args Args[] = { | |
40 | { 't', 0, "Test::Worked", 0 }, | |
41 | { 'z', "zero", "Test::Zero", 0 }, | |
42 | {0,0,0,0} | |
43 | }; | |
f00832cc DK |
44 | ::Configuration c; |
45 | CommandLine CmdL(Args, &c); | |
7a6d9076 | 46 | |
ae2be086 DH |
47 | char const * argv[] = { "test", "--zero", "-t" }; |
48 | CmdL.Parse(3 , argv); | |
f00832cc DK |
49 | EXPECT_TRUE(c.FindB("Test::Worked", false)); |
50 | EXPECT_TRUE(c.FindB("Test::Zero", false)); | |
ae2be086 | 51 | |
f00832cc DK |
52 | c.Clear("Test"); |
53 | EXPECT_FALSE(c.FindB("Test::Worked", false)); | |
54 | EXPECT_FALSE(c.FindB("Test::Zero", false)); | |
7a6d9076 | 55 | |
f00832cc DK |
56 | c.Set("Test::Zero", true); |
57 | EXPECT_TRUE(c.FindB("Test::Zero", false)); | |
7a6d9076 DK |
58 | |
59 | char const * argv2[] = { "test", "--no-zero", "-t" }; | |
60 | CmdL.Parse(3 , argv2); | |
f00832cc DK |
61 | EXPECT_TRUE(c.FindB("Test::Worked", false)); |
62 | EXPECT_FALSE(c.FindB("Test::Zero", false)); | |
ae2be086 | 63 | } |
08be0ca3 MV |
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 | } | |
c4b91cbe DK |
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); | |
011188e3 | 103 | std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com); |
c4b91cbe DK |
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); | |
011188e3 | 117 | std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com); |
c4b91cbe DK |
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); | |
011188e3 | 132 | std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com); |
c4b91cbe DK |
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); | |
011188e3 | 147 | std::vector<CommandLine::Args> Args = getCommandArgs(APT_CMD::APT_GET, com); |
c4b91cbe DK |
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 | } |