]>
git.saurik.com Git - apt.git/blob - test/libapt/commandline_test.cc
7f6c511df2056967b10b53fbc61ec1b23d89ecd2
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
&, aptDispatchWithHelp
const *) {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 { 'z', "zero", "Test::Zero", 0 },
45 CommandLine
CmdL(Args
, &c
);
47 char const * argv
[] = { "test", "--zero", "-t" };
49 EXPECT_TRUE(c
.FindB("Test::Worked", false));
50 EXPECT_TRUE(c
.FindB("Test::Zero", false));
53 EXPECT_FALSE(c
.FindB("Test::Worked", false));
54 EXPECT_FALSE(c
.FindB("Test::Zero", false));
56 c
.Set("Test::Zero", true);
57 EXPECT_TRUE(c
.FindB("Test::Zero", false));
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));
65 TEST(CommandLineTest
, BoolParsing
)
67 CommandLine::Args Args
[] = {
68 { 't', 0, "Test::Worked", 0 },
72 CommandLine
CmdL(Args
, &c
);
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
79 char const * argv
[] = { "show", "-t", "0ad" };
80 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
82 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "0ad");
86 char const * argv
[] = { "show", "-t", "0", "ad" };
87 bool res
= CmdL
.Parse(sizeof(argv
)/sizeof(char*), argv
);
89 ASSERT_EQ(std::string(CmdL
.FileList
[0]), "ad");
94 bool DoVoid(CommandLine
&) { return false; }
96 TEST(CommandLineTest
,GetCommand
)
98 CommandLine::Dispatch Cmds
[] = { {"install",&DoVoid
}, {"remove", &DoVoid
}, {0,0} };
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
);
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");
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
);
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");
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
);
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");
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
);
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");