• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1071875915

14 Nov 2023 11:46AM UTC coverage: 80.182% (+0.08%) from 80.107%
1071875915

push

gitlab-ci

kacf
chore: Get rid of direct comparisons with `Error::code`.

Such comparisons are dangerous, because if you don't also compare the
category, you can get false positives.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

12 of 12 new or added lines in 3 files covered. (100.0%)

79 existing lines in 9 files now uncovered.

6967 of 8689 relevant lines covered (80.18%)

9263.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.47
/src/artifact/v3/header/type_info.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <artifact/v3/header/header.hpp>
16

17
#include <vector>
18

19
#include <common/expected.hpp>
20
#include <common/error.hpp>
21
#include <common/io.hpp>
22
#include <common/log.hpp>
23
#include <common/json.hpp>
24
#include <common/common.hpp>
25

26
#include <artifact/error.hpp>
27
#include <artifact/lexer.hpp>
28
#include <artifact/tar/tar.hpp>
29

30

31
namespace mender {
32
namespace artifact {
33
namespace v3 {
34
namespace header {
35

36
namespace type_info {
37

38
namespace expected = mender::common::expected;
39
namespace io = mender::common::io;
40
namespace error = mender::common::error;
41
namespace log = mender::common::log;
42
namespace json = mender::common::json;
43

44
ExpectedTypeInfo Parse(io::Reader &reader) {
140✔
45
        TypeInfo type_info;
280✔
46

47
        log::Trace("Parse(type-info)...");
280✔
48

49
        auto expected_json = json::Load(reader);
140✔
50

51
        if (!expected_json) {
140✔
52
                return expected::unexpected(parser_error::MakeError(
×
53
                        parser_error::Code::ParseError,
54
                        "Failed to parse the  sub-header JSON: " + expected_json.error().message));
×
55
        }
56

57
        const json::Json type_info_json = expected_json.value();
140✔
58
        type_info.verbatim = type_info_json;
140✔
59

60

61
        //
62
        // Parse the single payload_type key:value (required)
63
        //
64

65
        log::Trace("type-info: Parsing the payload type");
280✔
66

67
        auto expected_payload = type_info_json.Get("type");
140✔
68
        if (!expected_payload) {
140✔
69
                return expected::unexpected(parser_error::MakeError(
×
70
                        parser_error::Code::ParseError,
71
                        "Failed to get the type-info payload type JSON: " + expected_payload.error().message));
×
72
        }
73
        auto payload_type = expected_payload.value();
140✔
74
        if (payload_type.IsNull()) {
140✔
75
                type_info.type = "null";
4✔
76
        } else if (payload_type.IsString()) {
136✔
77
                type_info.type = payload_type.GetString().value();
272✔
78
        } else {
79
                return expected::unexpected(parser_error::MakeError(
×
80
                        parser_error::Code::ParseError,
81
                        "Failed to parse the type-info payload type JSON: "
82
                                + expected_payload.error().message));
×
83
        }
84

85
        log::Trace("type-info: Parsing the artifact_provides");
280✔
86

87
        //
88
        // artifact_provides (Optional)
89
        //
90

91
        auto expected_artifact_provides =
92
                type_info_json.Get("artifact_provides").and_then(json::ToKeyValueMap);
280✔
93
        if (!expected_artifact_provides
140✔
94
                && expected_artifact_provides.error().code != json::MakeError(json::KeyError, "").code) {
155✔
95
                return expected::unexpected(parser_error::MakeError(
×
96
                        parser_error::Code::ParseError,
97
                        "Failed to parse the type-info artifact_provides JSON: "
98
                                + expected_artifact_provides.error().message));
×
99
        }
100
        if (expected_artifact_provides) {
140✔
101
                type_info.artifact_provides = expected_artifact_provides.value();
135✔
102
        } else {
103
                log::Trace("No artifact_provides found in type-info");
10✔
104
        }
105

106

107
        //
108
        // artifact_depends (Optional)
109
        //
110
        log::Trace("type-info: Parsing the artifact_depends");
280✔
111

112
        auto expected_artifact_depends =
113
                type_info_json.Get("artifact_depends").and_then(json::ToKeyValueMap);
280✔
114
        if (!expected_artifact_depends
140✔
115
                && expected_artifact_depends.error().code != json::MakeError(json::KeyError, "").code) {
554✔
116
                return expected::unexpected(parser_error::MakeError(
×
117
                        parser_error::Code::ParseError,
118
                        "Failed to parse the type-info artifact_depends JSON: "
119
                                + expected_artifact_provides.error().message));
×
120
        }
121
        if (expected_artifact_depends) {
140✔
122
                type_info.artifact_depends = expected_artifact_depends.value();
2✔
123
        } else {
124
                log::Trace("No artifact_depends found in type-info");
276✔
125
        }
126

127
        //
128
        // clears_artifact_provides (Optional)
129
        //
130

131
        log::Trace("type-info: Parsing the clears_artifact_provides");
280✔
132
        auto expected_clears_artifact_provides =
133
                type_info_json.Get("clears_artifact_provides").and_then(json::ToStringVector);
280✔
134
        if (!expected_clears_artifact_provides
140✔
135
                && expected_clears_artifact_provides.error().code
140✔
136
                           != json::MakeError(json::KeyError, "").code) {
155✔
UNCOV
137
                return expected::unexpected(parser_error::MakeError(
×
138
                        parser_error::Code::ParseError,
139
                        "Failed to parse the type-info clears_artifact_depends JSON: "
140
                                + expected_artifact_provides.error().message));
×
141
        }
142
        if (expected_clears_artifact_provides) {
140✔
143
                type_info.clears_artifact_provides = expected_clears_artifact_provides.value();
135✔
144
        } else {
145
                log::Trace("No artifact_clears_provides found in type-info");
10✔
146
        }
147

148
        log::Trace("Finished parsing the type-info..");
280✔
149

150
        return type_info;
140✔
151
}
152

153
} // namespace type_info
154
} // namespace header
155
} // namespace v3
156
} // namespace artifact
157
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc