Skip to content

Commit 9c999fb

Browse files
Otielrarkins
andauthored
feat(manager/nuget): add support for "disabledPackageSources" in nuget.config (#32011)
Co-authored-by: Rhys Arkins <[email protected]>
1 parent 27b82f8 commit 9c999fb

File tree

2 files changed

+168
-27
lines changed

2 files changed

+168
-27
lines changed

lib/modules/manager/nuget/util.spec.ts

Lines changed: 142 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ describe('modules/manager/nuget/util', () => {
3434

3535
describe('getConfiguredRegistries', () => {
3636
it('reads nuget config file', async () => {
37-
fs.findUpLocal.mockReturnValue(
38-
Promise.resolve<string | null>('NuGet.config'),
39-
);
37+
fs.findUpLocal.mockResolvedValue('NuGet.config');
4038
fs.readLocalFile.mockResolvedValueOnce(
4139
codeBlock`
4240
<configuration>
@@ -58,23 +56,22 @@ describe('modules/manager/nuget/util', () => {
5856
);
5957

6058
const registries = await getConfiguredRegistries('NuGet.config');
61-
expect(registries?.length).toBe(2);
62-
expect(registries![0].name).toBe('nuget.org');
63-
expect(registries![0].url).toBe('https://api.nuget.org/v3/index.json');
64-
expect(registries![0].sourceMappedPackagePatterns).toEqual(['*']);
65-
66-
expect(registries![1].name).toBe('contoso.com');
67-
expect(registries![1].url).toBe('https://contoso.com/packages/');
68-
expect(registries![1].sourceMappedPackagePatterns).toEqual([
69-
'Contoso.*',
70-
'NuGet.Common',
59+
expect(registries).toEqual([
60+
{
61+
name: 'nuget.org',
62+
url: 'https://api.nuget.org/v3/index.json',
63+
sourceMappedPackagePatterns: ['*'],
64+
},
65+
{
66+
name: 'contoso.com',
67+
url: 'https://contoso.com/packages/',
68+
sourceMappedPackagePatterns: ['Contoso.*', 'NuGet.Common'],
69+
},
7170
]);
7271
});
7372

7473
it('reads nuget config file with default registry', async () => {
75-
fs.findUpLocal.mockReturnValue(
76-
Promise.resolve<string | null>('NuGet.config'),
77-
);
74+
fs.findUpLocal.mockResolvedValue('NuGet.config');
7875
fs.readLocalFile.mockResolvedValueOnce(
7976
codeBlock`
8077
<configuration>
@@ -94,18 +91,137 @@ describe('modules/manager/nuget/util', () => {
9491
);
9592

9693
const registries = await getConfiguredRegistries('NuGet.config');
97-
expect(registries?.length).toBe(2);
98-
expect(registries![0].name).toBe('nuget.org');
99-
expect(registries![0].url).toBe('https://api.nuget.org/v3/index.json');
100-
expect(registries![0].sourceMappedPackagePatterns).toEqual(['*']);
101-
102-
expect(registries![1].name).toBe('contoso.com');
103-
expect(registries![1].url).toBe('https://contoso.com/packages/');
104-
expect(registries![1].sourceMappedPackagePatterns).toEqual([
105-
'Contoso.*',
106-
'NuGet.Common',
94+
expect(registries).toEqual([
95+
{
96+
name: 'nuget.org',
97+
url: 'https://api.nuget.org/v3/index.json',
98+
sourceMappedPackagePatterns: ['*'],
99+
},
100+
{
101+
name: 'contoso.com',
102+
url: 'https://contoso.com/packages/',
103+
sourceMappedPackagePatterns: ['Contoso.*', 'NuGet.Common'],
104+
},
107105
]);
108106
});
107+
108+
it('reads nuget config file with default registry disabled and added sources', async () => {
109+
fs.findUpLocal.mockResolvedValue('NuGet.config');
110+
fs.readLocalFile.mockResolvedValueOnce(
111+
codeBlock`
112+
<configuration>
113+
<packageSources>
114+
<add key="contoso.com" value="https://contoso.com/packages/"/>
115+
</packageSources>
116+
<disabledPackageSources>
117+
<add key="nuget.org" value="true" />
118+
</disabledPackageSources>
119+
</configuration>`,
120+
);
121+
122+
const registries = await getConfiguredRegistries('NuGet.config');
123+
expect(registries).toEqual([
124+
{
125+
name: 'contoso.com',
126+
url: 'https://contoso.com/packages/',
127+
},
128+
]);
129+
});
130+
131+
it('reads nuget config file with default registry disabled given default registry added', async () => {
132+
fs.findUpLocal.mockResolvedValue('NuGet.config');
133+
fs.readLocalFile.mockResolvedValueOnce(
134+
codeBlock`
135+
<configuration>
136+
<packageSources>
137+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
138+
<add key="contoso.com" value="https://contoso.com/packages/"/>
139+
</packageSources>
140+
<disabledPackageSources>
141+
<add key="nuget.org" value="true" />
142+
</disabledPackageSources>
143+
</configuration>`,
144+
);
145+
146+
const registries = await getConfiguredRegistries('NuGet.config');
147+
expect(registries).toEqual([
148+
{
149+
name: 'contoso.com',
150+
url: 'https://contoso.com/packages/',
151+
},
152+
]);
153+
});
154+
155+
it('reads nuget config file with unknown disabled source', async () => {
156+
fs.findUpLocal.mockResolvedValue('NuGet.config');
157+
fs.readLocalFile.mockResolvedValueOnce(
158+
codeBlock`
159+
<configuration>
160+
<packageSources>
161+
<add key="contoso.com" value="https://contoso.com/packages/"/>
162+
</packageSources>
163+
<disabledPackageSources>
164+
<add key="unknown" value="true" />
165+
</disabledPackageSources>
166+
</configuration>`,
167+
);
168+
169+
const registries = await getConfiguredRegistries('NuGet.config');
170+
expect(registries).toEqual([
171+
{
172+
name: 'nuget.org',
173+
url: 'https://api.nuget.org/v3/index.json',
174+
},
175+
{
176+
name: 'contoso.com',
177+
url: 'https://contoso.com/packages/',
178+
},
179+
]);
180+
});
181+
182+
it('reads nuget config file with disabled source with value false', async () => {
183+
fs.findUpLocal.mockResolvedValue('NuGet.config');
184+
fs.readLocalFile.mockResolvedValueOnce(
185+
codeBlock`
186+
<configuration>
187+
<packageSources>
188+
<clear />
189+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
190+
<add key="contoso.com" value="https://contoso.com/packages/"/>
191+
</packageSources>
192+
<disabledPackageSources>
193+
<add key="contoso.com" value="false" />
194+
</disabledPackageSources>
195+
</configuration>`,
196+
);
197+
198+
const registries = await getConfiguredRegistries('NuGet.config');
199+
expect(registries).toEqual([
200+
{
201+
name: 'nuget.org',
202+
url: 'https://api.nuget.org/v3/index.json',
203+
},
204+
{
205+
name: 'contoso.com',
206+
url: 'https://contoso.com/packages/',
207+
},
208+
]);
209+
});
210+
211+
it('reads nuget config file without packageSources and ignores disabledPackageSources', async () => {
212+
fs.findUpLocal.mockResolvedValue('NuGet.config');
213+
fs.readLocalFile.mockResolvedValueOnce(
214+
codeBlock`
215+
<configuration>
216+
<disabledPackageSources>
217+
<add key="contoso.com" value="true" />
218+
</disabledPackageSources>
219+
</configuration>`,
220+
);
221+
222+
const registries = await getConfiguredRegistries('NuGet.config');
223+
expect(registries).toBeUndefined();
224+
});
109225
});
110226

111227
describe('applyRegistries', () => {

lib/modules/manager/nuget/util.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ export async function getConfiguredRegistries(
5252
}
5353

5454
const packageSources = nuGetConfig.childNamed('packageSources');
55+
5556
if (!packageSources) {
57+
// If there are no packageSources, don't even look for any
58+
// disabledPackageSources
59+
// Even if NuGet default source (nuget.org) was among the
60+
// disabledPackageSources, Renovate will default to the default source
61+
// (nuget.org) anyway
5662
return undefined;
5763
}
5864

5965
const packageSourceMapping = nuGetConfig.childNamed('packageSourceMapping');
6066

61-
const registries = getDefaultRegistries();
67+
let registries = getDefaultRegistries();
6268

6369
// Map optional source mapped package patterns to default registries
6470
for (const registry of registries) {
@@ -111,6 +117,25 @@ export async function getConfiguredRegistries(
111117
// child.name === 'remove' not supported
112118
}
113119
}
120+
121+
const disabledPackageSources = nuGetConfig.childNamed(
122+
'disabledPackageSources',
123+
);
124+
125+
if (disabledPackageSources) {
126+
for (const child of disabledPackageSources.children) {
127+
if (
128+
child.type === 'element' &&
129+
child.name === 'add' &&
130+
child.attr.value === 'true'
131+
) {
132+
const disabledRegistryKey = child.attr.key;
133+
registries = registries.filter((o) => o.name !== disabledRegistryKey);
134+
logger.debug(`Disabled registry with key: ${disabledRegistryKey}`);
135+
}
136+
}
137+
}
138+
114139
return registries;
115140
}
116141

0 commit comments

Comments
 (0)