GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 61.7% 79 / 0 / 128
Functions: 54.2% 13 / 0 / 24
Branches: 61.8% 84 / 0 / 136

src/utilities/bcl/BCLFileReference.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "BCLFileReference.hpp"
7 #include "../core/Checksum.hpp"
8 #include "../core/StringHelpers.hpp"
9
10 #include <pugixml.hpp>
11 #include <json/json.h>
12
13 namespace openstudio {
14
15 880 BCLFileReference::BCLFileReference(const openstudio::path& measureRootDir, const openstudio::path& relativePath, const bool setMembers)
16 880 : m_measureRootDir(openstudio::filesystem::system_complete(measureRootDir)),
17
2/2
✓ Branch 2 taken 880 times.
✓ Branch 5 taken 880 times.
1760 m_path(openstudio::filesystem::system_complete(measureRootDir / relativePath)) {
18 // DLM: why would you not want to set the members?
19
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 772 times.
880 if (setMembers) {
20
1/1
✓ Branch 1 taken 108 times.
108 m_checksum = openstudio::checksum(m_path);
21
22
1/1
✓ Branch 1 taken 108 times.
108 std::string fileType = this->fileType();
23
3/3
✓ Branch 1 taken 108 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 97 times.
108 if (fileType == "osm") {
24
1/1
✓ Branch 1 taken 11 times.
11 m_softwareProgram = "OpenStudio";
25 //m_softwareProgramVersion = "";
26
2/3
✓ Branch 1 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
97 } else if (fileType == "osc") {
27 ✗ m_softwareProgram = "OpenStudio";
28 //m_softwareProgramVersion = "";
29
2/3
✓ Branch 1 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
97 } else if (fileType == "idf") {
30 ✗ m_softwareProgram = "EnergyPlus";
31 //m_softwareProgramVersion = "";
32
8/8
✓ Branch 1 taken 97 times.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 26 times.
✓ Branch 6 taken 71 times.
✓ Branch 8 taken 13 times.
✓ Branch 9 taken 58 times.
✓ Branch 10 taken 39 times.
✓ Branch 11 taken 58 times.
97 } else if ((fileType == "rb") || (fileType == "py")) {
33
1/1
✓ Branch 1 taken 39 times.
39 m_softwareProgram = "OpenStudio";
34 //m_softwareProgramVersion = "";
35 }
36 108 }
37 880 }
38
39 2349 openstudio::path BCLFileReference::path() const {
40 2349 return m_path;
41 }
42
43 16 openstudio::path BCLFileReference::relativePath() const {
44 16 return openstudio::filesystem::relative(m_path, m_measureRootDir);
45 }
46
47 ✗ std::string BCLFileReference::checksum() const {
48 ✗ return m_checksum;
49 }
50
51 ✗ std::string BCLFileReference::softwareProgram() const {
52 ✗ return m_softwareProgram;
53 }
54
55 ✗ std::string BCLFileReference::softwareProgramVersion() const {
56 ✗ return m_softwareProgramVersion;
57 }
58
59 ✗ boost::optional<VersionString> BCLFileReference::minCompatibleVersion() const {
60 ✗ return m_minCompatibleVersion;
61 }
62
63 ✗ boost::optional<VersionString> BCLFileReference::maxCompatibleVersion() const {
64 ✗ return m_maxCompatibleVersion;
65 }
66
67 216 std::string BCLFileReference::fileName() const {
68
69
1/1
✓ Branch 1 taken 216 times.
216 std::string usageType = this->usageType();
70
1/1
✓ Branch 1 taken 216 times.
216 openstudio::path baseDir = m_measureRootDir;
71
3/3
✓ Branch 1 taken 216 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 189 times.
216 if (usageType == "doc") {
72 baseDir /= "docs";
73
3/3
✓ Branch 1 taken 189 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 165 times.
189 } else if (usageType == "resource") {
74 baseDir /= "resources";
75
3/3
✓ Branch 1 taken 165 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 84 times.
165 } else if (usageType == "test") {
76 baseDir /= "tests";
77 }
78
79
2/2
✓ Branch 1 taken 216 times.
✓ Branch 4 taken 216 times.
432 return toString(openstudio::filesystem::relative(m_path, baseDir));
80 216 }
81
82 252 std::string BCLFileReference::fileType() const {
83
5/5
✓ Branch 1 taken 252 times.
✓ Branch 4 taken 252 times.
✓ Branch 7 taken 252 times.
✓ Branch 10 taken 252 times.
✓ Branch 13 taken 252 times.
1260 return openstudio::replace(openstudio::toString(m_path.extension()), ".", "");
84 }
85
86 232 std::string BCLFileReference::usageType() const {
87 232 return m_usageType;
88 }
89
90 772 void BCLFileReference::setChecksum(const std::string& checksum) {
91 772 m_checksum = checksum;
92 772 }
93
94 772 void BCLFileReference::setSoftwareProgram(const std::string& softwareProgram) {
95 772 m_softwareProgram = softwareProgram;
96 772 }
97
98 786 void BCLFileReference::setSoftwareProgramVersion(const std::string& softwareProgramVersion) {
99 786 m_softwareProgramVersion = softwareProgramVersion;
100 786 }
101
102 460 void BCLFileReference::setMinCompatibleVersion(const VersionString& minCompatibleVersion) {
103 460 m_minCompatibleVersion = minCompatibleVersion;
104 460 }
105
106 ✗ void BCLFileReference::resetMinCompatibleVersion() {
107 ✗ m_minCompatibleVersion.reset();
108 ✗ }
109
110 ✗ void BCLFileReference::setMaxCompatibleVersion(const VersionString& maxCompatibleVersion) {
111 ✗ m_maxCompatibleVersion = maxCompatibleVersion;
112 ✗ }
113
114 ✗ void BCLFileReference::resetMaxCompatibleVersion() {
115 ✗ m_maxCompatibleVersion.reset();
116 ✗ }
117
118 875 void BCLFileReference::setUsageType(const std::string& usageType) {
119 875 m_usageType = usageType;
120 875 }
121
122 143 void BCLFileReference::writeValues(pugi::xml_node& element) const {
123
9/9
✓ Branch 1 taken 143 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 111 times.
✓ Branch 6 taken 31 times.
✓ Branch 7 taken 1 time.
✓ Branch 9 taken 19 times.
✓ Branch 10 taken 12 times.
✓ Branch 11 taken 19 times.
✓ Branch 12 taken 124 times.
143 if (m_usageType == "script" && !m_softwareProgram.empty() && !m_softwareProgramVersion.empty()) {
124
1/1
✓ Branch 1 taken 19 times.
19 auto versionElement = element.append_child("version");
125
126
1/1
✓ Branch 1 taken 19 times.
19 auto subelement = versionElement.append_child("software_program");
127
1/1
✓ Branch 1 taken 19 times.
19 auto text = subelement.text();
128
1/1
✓ Branch 2 taken 19 times.
19 text.set(m_softwareProgram.c_str());
129
130
1/1
✓ Branch 1 taken 19 times.
19 subelement = versionElement.append_child("identifier");
131
1/1
✓ Branch 1 taken 19 times.
19 text = subelement.text();
132
1/1
✓ Branch 2 taken 19 times.
19 text.set(m_softwareProgramVersion.c_str());
133
134
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 14 times.
38 if (m_minCompatibleVersion) {
135
1/1
✓ Branch 1 taken 5 times.
5 subelement = versionElement.append_child("min_compatible");
136
1/1
✓ Branch 1 taken 5 times.
5 text = subelement.text();
137
3/3
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 8 taken 5 times.
5 text.set(m_minCompatibleVersion->str().c_str());
138 }
139
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
38 if (m_maxCompatibleVersion) {
141 ✗ subelement = versionElement.append_child("max_compatible");
142 ✗ text = subelement.text();
143 ✗ text.set(m_maxCompatibleVersion->str().c_str());
144 }
145 }
146
147
1/1
✓ Branch 1 taken 143 times.
143 auto subelement = element.append_child("filename");
148
1/1
✓ Branch 1 taken 143 times.
143 auto text = subelement.text();
149
2/2
✓ Branch 1 taken 143 times.
✓ Branch 5 taken 143 times.
143 text.set(fileName().c_str()); // careful to write out function result instead of member
150
151
1/1
✓ Branch 1 taken 143 times.
143 subelement = element.append_child("filetype");
152
1/1
✓ Branch 1 taken 143 times.
143 text = subelement.text();
153
2/2
✓ Branch 1 taken 143 times.
✓ Branch 5 taken 143 times.
143 text.set(fileType().c_str()); // careful to write out function result instead of member
154
155
1/1
✓ Branch 1 taken 143 times.
143 subelement = element.append_child("usage_type");
156
1/1
✓ Branch 1 taken 143 times.
143 text = subelement.text();
157
1/1
✓ Branch 2 taken 143 times.
143 text.set(m_usageType.c_str());
158
159
1/1
✓ Branch 1 taken 143 times.
143 subelement = element.append_child("checksum");
160
1/1
✓ Branch 1 taken 143 times.
143 text = subelement.text();
161
1/1
✓ Branch 2 taken 143 times.
143 text.set(m_checksum.c_str());
162 143 }
163
164 ✗ Json::Value BCLFileReference::toJSON() const {
165 ✗ Json::Value root;
166 ✗ if (m_usageType == "script" && !m_softwareProgram.empty() && !m_softwareProgramVersion.empty()) {
167 ✗ auto& versionElement = root["version"];
168
169 ✗ versionElement["software_program"] = m_softwareProgram;
170 ✗ versionElement["identifier"] = m_softwareProgramVersion;
171
172 ✗ if (m_minCompatibleVersion) {
173 ✗ versionElement["min_compatible"] = m_minCompatibleVersion->str();
174 }
175
176 ✗ if (m_maxCompatibleVersion) {
177 ✗ versionElement["max_compatible"] = m_maxCompatibleVersion->str();
178 }
179 }
180
181 ✗ root["filename"] = fileName();
182 ✗ root["filetype"] = fileType();
183 ✗ root["usage_type"] = m_usageType;
184 ✗ root["checksum"] = m_checksum;
185
186 ✗ return root;
187 ✗ }
188
189 ✗ std::string BCLFileReference::toJSONString() const {
190 ✗ return toJSON().toStyledString();
191 }
192
193 53 bool BCLFileReference::checkForUpdate() {
194
2/2
✓ Branch 1 taken 53 times.
✓ Branch 4 taken 53 times.
53 std::string newChecksum = openstudio::checksum(this->path());
195
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 50 times.
53 if (m_checksum != newChecksum) {
196
1/1
✓ Branch 1 taken 3 times.
3 m_checksum = newChecksum;
197 3 return true;
198 }
199 50 return false;
200 53 }
201
202 // bool operator==(const BCLFileReference& lhs, const BCLFileReference& rhs) {
203 // return lhs.m_path == rhs.m_path;
204 // }
205 //
206 // std::strong_ordering operator<=>(const BCLFileReference& lhs, const BCLFileReference& rhs) {
207 // if (lhs.m_path < rhs.m_path) {
208 // return std::strong_ordering::less;
209 // } else if (lhs.m_path == rhs.m_path) {
210 // return std::strong_ordering::equal;
211 // }
212 // return std::strong_ordering::greater;
213 // }
214
215 ✗ std::ostream& operator<<(std::ostream& os, const BCLFileReference& file) {
216 ✗ pugi::xml_document doc;
217 ✗ auto element = doc.append_child("File");
218 ✗ file.writeValues(element);
219
220 ✗ doc.save(os, " ");
221 ✗ return os;
222 ✗ }
223
224 } // namespace openstudio
225