File: test_repoquery.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (205 lines) | stat: -rw-r--r-- 6,931 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.


#include "test_repoquery.hpp"

#include <libdnf5-cli/output/repoquery.hpp>
#include <stdio.h>
#include <stdlib.h>

#include <string_view>
#include <system_error>

CPPUNIT_TEST_SUITE_REGISTRATION(RepoqueryTest);


void RepoqueryTest::setUp() {
    BaseTestCase::setUp();
    BaseTestCase::add_repo_repomd("repomd-repo1");
    pkgs = std::make_unique<libdnf5::rpm::PackageQuery>(base);
}


namespace {

class MemStream {
public:
    MemStream() {
        stream = open_memstream(&buf, &len);
        if (!stream) {
            throw std::system_error(errno, std::system_category(), "open_memstream");
        }
    }

    ~MemStream() {
        CPPUNIT_ASSERT_EQUAL(fclose(stream), 0);
        free(buf);
    }

    FILE * get_file() noexcept { return stream; }

    std::string_view get_string_view() noexcept {
        CPPUNIT_ASSERT_EQUAL(fflush(stream), 0);
        return std::string_view(buf, len);
    }

private:
    FILE * stream;
    char * buf;
    size_t len;
};

}  // namespace


void RepoqueryTest::test_format_set_with_simple_str() {
    MemStream stream;
    libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "test\n");
    CPPUNIT_ASSERT_EQUAL(std::string_view("test\n"), stream.get_string_view());
}


void RepoqueryTest::test_format_set_with_tags() {
    // One tag
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%{name}\n");
        CPPUNIT_ASSERT_EQUAL(std::string_view("pkg\npkg-libs\nunresolvable\n"), stream.get_string_view());
    }

    // Duplicate tag
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%{name}-%{name}-%{name}\n");
        CPPUNIT_ASSERT_EQUAL(
            std::string_view("pkg-libs-pkg-libs-pkg-libs\npkg-pkg-pkg\nunresolvable-unresolvable-unresolvable\n"),
            stream.get_string_view());
    }

    // Different tags
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%{name}-%{arch}-%{installsize}\n");
        CPPUNIT_ASSERT_EQUAL(
            std::string_view("pkg-libs-x86_64-222\npkg-x86_64-222\nunresolvable-noarch-222\n"),
            stream.get_string_view());
    }
}


void RepoqueryTest::test_format_set_with_invalid_tags() {
    // Incomplete tag
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%{name\n");
        CPPUNIT_ASSERT_EQUAL(std::string_view("%{name\n"), stream.get_string_view());
    }

    // Not matching tag
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%{asd}\n");
        CPPUNIT_ASSERT_EQUAL(std::string_view("%{asd}\n"), stream.get_string_view());
    }

    // Incomplate tag with not matching tag and additional braces
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%%{ %% {{{%{asd}");
        CPPUNIT_ASSERT_EQUAL(std::string_view("%%{ %% {{{%{asd}"), stream.get_string_view());
    }

    // Incomplate tag with matching tag and additional braces
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%%{}{}{{%{name}}");
        CPPUNIT_ASSERT_EQUAL(
            std::string_view("%%{}{}{{pkg-libs}%%{}{}{{pkg}%%{}{}{{unresolvable}"), stream.get_string_view());
    }

    // fmt formatting strings are disabled
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%{name:^30}");
        CPPUNIT_ASSERT_EQUAL(std::string_view("%{name:^30}"), stream.get_string_view());
    }
}


void RepoqueryTest::test_format_set_with_tags_with_spacing() {
    // Tag with invalid format spec
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%aa20{name}%{evr}\n");
        CPPUNIT_ASSERT_EQUAL(
            std::string_view("%aa20{name}1.2-3\n%aa20{name}1:1.3-4\n%aa20{name}1:2-3\n"), stream.get_string_view());
    }

    // One tag with align spec
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_set_with_format(stream.get_file(), *pkgs, "%-20{name}%{evr}\n");
        CPPUNIT_ASSERT_EQUAL(
            std::string_view("pkg                 1.2-3\npkg-libs            1:1.3-4\nunresolvable        1:2-3\n"),
            stream.get_string_view());
    }
}


void RepoqueryTest::test_pkg_attr_uniq_sorted() {
    // requires
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_attr_uniq_sorted(stream.get_file(), *pkgs, "requires");
        CPPUNIT_ASSERT_EQUAL(std::string_view("prereq\nreq = 1:2-3\n"), stream.get_string_view());
    }

    // provides
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_attr_uniq_sorted(stream.get_file(), *pkgs, "provides");
        CPPUNIT_ASSERT_EQUAL(
            std::string_view("pkg = 1.2-3\npkg-libs = 1:1.3-4\nprv = 1:2-3\nunresolvable = 1:2-3\n"),
            stream.get_string_view());
    }

    // name
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_attr_uniq_sorted(stream.get_file(), *pkgs, "name");
        CPPUNIT_ASSERT_EQUAL(std::string_view("pkg\npkg-libs\nunresolvable\n"), stream.get_string_view());
    }

    // deduplicated buildtimes
    {
        MemStream stream;
        libdnf5::cli::output::print_pkg_attr_uniq_sorted(stream.get_file(), *pkgs, "buildtime");
        CPPUNIT_ASSERT_EQUAL(std::string_view("456\n"), stream.get_string_view());
    }
}


void RepoqueryTest::test_requires_filelists() {
    CPPUNIT_ASSERT_EQUAL(libdnf5::cli::output::requires_filelists("asd"), false);
    CPPUNIT_ASSERT_EQUAL(libdnf5::cli::output::requires_filelists("file"), false);
    CPPUNIT_ASSERT_EQUAL(libdnf5::cli::output::requires_filelists("%{file}"), false);
    CPPUNIT_ASSERT_EQUAL(libdnf5::cli::output::requires_filelists("%{name}"), false);
    CPPUNIT_ASSERT_EQUAL(libdnf5::cli::output::requires_filelists("%{files}"), true);
}