Skip to content

Commit 5a7167f

Browse files
Show a more meaningful message when a project uses build conditions
1 parent 357f8fe commit 5a7167f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/LibYear.Core/FileTypes/XmlProjectFile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ protected XmlProjectFile(string filename, string contents, string elementName, s
3131
_xmlContents = XDocument.Parse(contents);
3232
_whitespace = DetermineWhitespace(contents);
3333

34+
if (_xmlContents.Root is not null && _xmlContents.Root.DescendantsAndSelf().Any(e => e.Attribute("Condition") is not null))
35+
throw new ArgumentException("Project files with conditions are not supported");
36+
3437
Packages = _xmlContents.Descendants(elementName)
3538
.ToDictionary(
3639
d => packageAttributeNames.Select(p => d.Attribute(p)?.Value ?? d.Element(p)?.Value).FirstOrDefault(v => v != null)!,

test/LibYear.Core.Tests/FileTypes/CsProjFileTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public async Task InvalidVersionShowsExceptionDetails()
118118
{
119119
//act
120120
_ = new CsProjFile(filename, newContents);
121-
Assert.False(true);
121+
Assert.Fail();
122122
}
123123
catch (Exception e)
124124
{
@@ -142,4 +142,24 @@ public async Task VersionRangeGetsSkipped()
142142
//assert
143143
Assert.Null(file.Packages["test8"]);
144144
}
145+
146+
[Fact]
147+
public async Task ProjectBuildConditionsThrowException()
148+
{
149+
//arrange
150+
var filename = Path.Combine("FileTypes", "project.csproj");
151+
var contents = @"<Project Sdk=""Microsoft.NET.Sdk""><ItemGroup Condition=""'$(TargetFramework)' == 'net8.0'""></ItemGroup></Project>";
152+
153+
try
154+
{
155+
//act
156+
_ = new CsProjFile(filename, contents);
157+
Assert.Fail();
158+
}
159+
catch (Exception e)
160+
{
161+
//assert
162+
Assert.Contains("not supported", e.Message);
163+
}
164+
}
145165
}

0 commit comments

Comments
 (0)