forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameNormalization.cpp
More file actions
156 lines (125 loc) · 5.78 KB
/
Copy pathNameNormalization.cpp
File metadata and controls
156 lines (125 loc) · 5.78 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <winget/NameNormalization.h>
using namespace std::string_view_literals;
using namespace AppInstaller::Utility;
// This skipped test case can be used to update the test file.
// It writes back to the output content location, so you must manually
// copy the file(s) back to the git managed location to update.
TEST_CASE("NameNorm_Update_Database_Initial", "[.]")
{
std::ifstream namesStream(TestCommon::TestDataFile("InputNames.txt").GetPath());
REQUIRE(namesStream);
std::ifstream publishersStream(TestCommon::TestDataFile("InputPublishers.txt").GetPath());
REQUIRE(publishersStream);
std::ofstream resultsStream(TestCommon::TestDataFile("NormalizationInitialIdsUpdate.txt").GetPath(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
REQUIRE(resultsStream);
// Far larger than any one value; hopefully
char name[4096]{};
char publisher[4096]{};
NameNormalizer normer(NormalizationVersion::Initial);
for (;;)
{
namesStream.getline(name, ARRAYSIZE(name));
publishersStream.getline(publisher, ARRAYSIZE(publisher));
if (namesStream || publishersStream)
{
REQUIRE(namesStream);
REQUIRE(publishersStream);
INFO("Name[" << name << "], Publisher[" << publisher << "]");
auto normalized = normer.Normalize(name, publisher);
std::string normalizedId = normalized.Publisher();
normalizedId += '.';
normalizedId += normalized.Name();
resultsStream << normalizedId << std::endl;
REQUIRE(resultsStream);
}
else
{
break;
}
}
}
// If this test is failing, either changes to winget code or the ICU binaries have caused it.
// This will impact the functionality of the PreIndexedPackageSource, as it is the primary
// mechanism used to cross reference packages installed outside of winget with those in the
// source.
TEST_CASE("NameNorm_Database_Initial", "[name_norm]")
{
std::ifstream namesStream(TestCommon::TestDataFile("InputNames.txt").GetPath());
REQUIRE(namesStream);
std::ifstream publishersStream(TestCommon::TestDataFile("InputPublishers.txt").GetPath());
REQUIRE(publishersStream);
std::ifstream resultsStream(TestCommon::TestDataFile("NormalizationInitialIds.txt").GetPath());
REQUIRE(resultsStream);
// Far larger than any one value; hopefully
char name[4096]{};
char publisher[4096]{};
char expectedId[4096]{};
NameNormalizer normer(NormalizationVersion::Initial);
for (;;)
{
namesStream.getline(name, ARRAYSIZE(name));
publishersStream.getline(publisher, ARRAYSIZE(publisher));
resultsStream.getline(expectedId, ARRAYSIZE(expectedId));
if (namesStream || publishersStream || resultsStream)
{
REQUIRE(namesStream);
REQUIRE(publishersStream);
REQUIRE(resultsStream);
INFO("Name[" << name << "], Publisher[" << publisher << "]");
auto normalized = normer.Normalize(name, publisher);
std::string normalizedId = normalized.Publisher();
normalizedId += '.';
normalizedId += normalized.Name();
REQUIRE(expectedId == normalizedId);
}
else
{
break;
}
}
}
TEST_CASE("NameNorm_Architecture", "[name_norm]")
{
NameNormalizer normer(NormalizationVersion::Initial);
REQUIRE(normer.Normalize("Name", {}).Architecture() == Architecture::Unknown);
REQUIRE(normer.Normalize("Name x86", {}).Architecture() == Architecture::X86);
REQUIRE(normer.Normalize("Name x86_64", {}).Architecture() == Architecture::X64);
REQUIRE(normer.Normalize("Name (64 bit)", {}).Architecture() == Architecture::X64);
REQUIRE(normer.Normalize("Name 32/64 bit", {}).Architecture() == Architecture::Unknown);
REQUIRE(normer.Normalize("Fox86", {}).Architecture() == Architecture::Unknown);
}
TEST_CASE("NameNorm_Locale", "[name_norm]")
{
NameNormalizer normer(NormalizationVersion::Initial);
REQUIRE(normer.Normalize("Name", {}).Locale() == "");
REQUIRE(normer.Normalize("Name en-US", {}).Locale() == "en-us");
REQUIRE(normer.Normalize("Name (es-mx)", {}).Locale() == "es-mx");
REQUIRE(normer.Normalize("Names-mx", {}).Locale() == "");
}
TEST_CASE("NameNorm_KBNumbers", "[name_norm]")
{
NameNormalizer normer(NormalizationVersion::Initial);
REQUIRE(normer.Normalize("Fix for (KB42)", {}).Name() == "FixforKB42");
}
TEST_CASE("NameNorm_Initial_PreserveWhitespace", "[name_norm]")
{
NameNormalizer normer(NormalizationVersion::InitialPreserveWhiteSpace);
REQUIRE(normer.NormalizeName("Some Name").Name() == "Some Name");
REQUIRE(normer.NormalizePublisher("Some Publisher Corp") == "Some Publisher");
}
TEST_CASE("NameNorm_GetNormalizedName_GetNormalizedFields", "[name_norm]")
{
NameNormalizer normer(NormalizationVersion::Initial);
auto normalizedName = normer.NormalizeName("Name(X64)");
REQUIRE(normalizedName.GetNormalizedName(NormalizationField::None) == "Name");
REQUIRE(normalizedName.GetNormalizedName(NormalizationField::Architecture) == "Name(X64)");
REQUIRE(normalizedName.GetNormalizedFields() == NormalizationField::Architecture);
auto normalizedName2 = normer.NormalizeName("Name");
REQUIRE(normalizedName2.GetNormalizedName(NormalizationField::None) == "Name");
REQUIRE(normalizedName2.GetNormalizedName(NormalizationField::Architecture) == "Name");
REQUIRE(normalizedName2.GetNormalizedFields() == NormalizationField::None);
}