Skip to content

Commit

Permalink
feat(manager/nuget): extract msbuild sdk from Project and Sdk (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Jun 3, 2024
1 parent f60b3e2 commit c89ae5c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/modules/manager/nuget/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,84 @@ describe('modules/manager/nuget/extract', () => {
expect(res?.deps).toHaveLength(17);
});

it('extracts msbuild sdk from the Sdk attr of Project element', async () => {
const packageFile = 'sample.csproj';
const sample = `
<Project Sdk="Microsoft.Build.NoTargets/3.4.0">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <!-- this is a dummy value -->
<NuspecFile>$(MSBuildThisFileDirectory)\tdlib.native.nuspec</NuspecFile>
<NuspecProperties>version=$(PackageVersion)</NuspecProperties>
</PropertyGroup>
</Project>
`;
const res = await extractPackageFile(sample, packageFile, config);
expect(res?.deps).toEqual([
{
depName: 'Microsoft.Build.NoTargets',
depType: 'msbuild-sdk',
currentValue: '3.4.0',
datasource: 'nuget',
},
]);
expect(res?.deps).toHaveLength(1);
});

it('does not extract msbuild sdk from the Sdk attr of Project element if version is missing', async () => {
const packageFile = 'sample.csproj';
const sample = `
<Project Sdk="Microsoft.Build.NoTargets">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <!-- this is a dummy value -->
<NuspecFile>$(MSBuildThisFileDirectory)\tdlib.native.nuspec</NuspecFile>
<NuspecProperties>version=$(PackageVersion)</NuspecProperties>
</PropertyGroup>
</Project>
`;
const res = await extractPackageFile(sample, packageFile, config);
expect(res).toBeNull();
});

it('extracts msbuild sdk from the Sdk element', async () => {
const packageFile = 'sample.csproj';
const sample = `
<Project>
<Sdk Name="Microsoft.Build.NoTargets" Version="3.4.0" />
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <!-- this is a dummy value -->
<NuspecFile>$(MSBuildThisFileDirectory)\tdlib.native.nuspec</NuspecFile>
<NuspecProperties>version=$(PackageVersion)</NuspecProperties>
</PropertyGroup>
</Project>
`;
const res = await extractPackageFile(sample, packageFile, config);
expect(res?.deps).toEqual([
{
depName: 'Microsoft.Build.NoTargets',
depType: 'msbuild-sdk',
currentValue: '3.4.0',
datasource: 'nuget',
},
]);
expect(res?.deps).toHaveLength(1);
});

it('does not extract msbuild sdk from the Sdk element if version is missing', async () => {
const packageFile = 'sample.csproj';
const sample = `
<Project>
<Sdk Name="Microsoft.Build.NoTargets" />
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <!-- this is a dummy value -->
<NuspecFile>$(MSBuildThisFileDirectory)\tdlib.native.nuspec</NuspecFile>
<NuspecProperties>version=$(PackageVersion)</NuspecProperties>
</PropertyGroup>
</Project>
`;
const res = await extractPackageFile(sample, packageFile, config);
expect(res).toBeNull();
});

it('extracts dependency with lower-case Version attribute', async () => {
const contents = codeBlock`
<Project Sdk="Microsoft.NET.Sdk">
Expand Down
26 changes: 26 additions & 0 deletions lib/modules/manager/nuget/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,33 @@ function extractDepsFromXml(xmlNode: XmlDocument): NugetPackageDependency[] {
currentValue,
});
}
} else if (name === 'Sdk') {
const depName = attr?.Name;
const version = attr?.Version;
// if sdk element is present it will always have the Name field but the Version is an optional field
if (depName && version) {
results.push({
depName,
currentValue: version,
depType: 'msbuild-sdk',
datasource: NugetDatasource.id,
});
}
} else {
if (name === 'Project') {
if (attr?.Sdk) {
const str = attr?.Sdk;
const [name, version] = str.split('/');
if (name && version) {
results.push({
depName: name,
depType: 'msbuild-sdk',
currentValue: version,
datasource: NugetDatasource.id,
});
}
}
}
todo.push(...(child.children.filter(isXmlElem) as XmlElement[]));
}
}
Expand Down

0 comments on commit c89ae5c

Please sign in to comment.