Skip to content

Commit 05cc42e

Browse files
committed
Ignore .addins files in XML format
1 parent a653f61 commit 05cc42e

4 files changed

Lines changed: 60 additions & 19 deletions

File tree

GitVersion.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
next-version: 3.21.0
12
mode: ContinuousDelivery
23
branches:
34
main:

src/NUnitEngine/nunit.engine.core.tests/Internal/AddinsFileTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ public void Read_Stream()
4848
}
4949
}
5050

51+
[Test]
52+
public void Read_AddinsFileIsNotOurs()
53+
{
54+
var content = new[]
55+
{
56+
"Anything",
57+
"<SomeXml>",
58+
" We don't understand this",
59+
"</SomeXml>",
60+
"More stuff"
61+
};
62+
63+
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(string.Join(Environment.NewLine, content))))
64+
{
65+
var result = AddinsFile.Read(stream);
66+
Assert.That(result.Count, Is.Zero);
67+
}
68+
}
69+
5170
[Test]
5271
public void Read_InvalidEntry()
5372
{

src/NUnitEngine/nunit.engine.core/DotNetHelper.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ public static class DotNet
3434
private static Lazy<List<RuntimeInfo>> _x64Runtimes = new Lazy<List<RuntimeInfo>>(() => [.. GetAllRuntimes(x86: false)]);
3535
private static Lazy<List<RuntimeInfo>> _x86Runtimes = new Lazy<List<RuntimeInfo>>(() => [.. GetAllRuntimes(x86: true)]);
3636

37-
public enum Architecture
38-
{
39-
Unspecified,
40-
X64,
41-
X86
42-
}
43-
4437
public class RuntimeInfo
4538
{
4639
public string Name;

src/NUnitEngine/nunit.engine.core/Internal/AddinsFile.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace NUnit.Engine.Internal
1313
{
1414
internal class AddinsFile : List<AddinsFileEntry>
1515
{
16+
static readonly Logger log = InternalTrace.GetLogger(typeof(AddinsFile));
17+
1618
public static AddinsFile Read(IFile file)
1719
{
1820
if (file == null)
@@ -33,29 +35,55 @@ public static AddinsFile Read(IFile file)
3335
/// <remarks>If the executing system uses backslashes ('\') to separate directories, these will be substituted with slashes ('/').</remarks>
3436
internal static AddinsFile Read(Stream stream, string fullName = null)
3537
{
38+
// Read the whole file first
39+
var content = new List<string>();
3640
using (var reader = new StreamReader(stream))
41+
{
42+
while (!reader.EndOfStream)
43+
content.Add(reader.ReadLine().Trim());
44+
}
45+
46+
// Create an empty AddinsFile, with no entries
47+
var addinsFile = new AddinsFile();
48+
49+
// Ensure that this is actually an NUnit .addins file, since
50+
// the extension is used by others. See, for example,
51+
// https://github.com/nunit/nunit-console/issues/1761
52+
// TODO: Consider using an extension specific to NUnit for V4
53+
if (!IsNUnitAddinsFile(content))
3754
{
38-
var addinsFile = new AddinsFile();
55+
log.Warning($"Ignoring file {fullName} because it's not an NUnit .addins file");
56+
return addinsFile;
57+
}
3958

40-
int lineNumber = 0;
41-
while (!reader.EndOfStream)
59+
// It's our file, so process it
60+
int lineNumber = 0;
61+
foreach (var line in content)
62+
{
63+
var entry = new AddinsFileEntry(++lineNumber, line);
64+
if (entry.Text != "" && !entry.IsValid)
4265
{
43-
var entry = new AddinsFileEntry(++lineNumber, reader.ReadLine());
44-
if (entry.Text != "" && !entry.IsValid)
45-
{
46-
string msg = $"Invalid Entry in {fullName ?? "addins file"}:\r\n {entry}";
47-
throw new InvalidOperationException(msg);
48-
}
49-
50-
addinsFile.Add(entry);
66+
string msg = $"Invalid Entry in {fullName ?? "addins file"}:\r\n {entry}";
67+
throw new InvalidOperationException(msg);
5168
}
5269

53-
return addinsFile;
70+
addinsFile.Add(entry);
5471
}
72+
73+
return addinsFile;
5574
}
5675

5776
private AddinsFile() { }
5877

78+
private static bool IsNUnitAddinsFile(List<string> content)
79+
{
80+
foreach (var line in content)
81+
if (line.Length > 0 && line[0] == '<')
82+
return false;
83+
84+
return true;
85+
}
86+
5987
public override string ToString()
6088
{
6189
var sb = new StringBuilder("AddinsFile:");

0 commit comments

Comments
 (0)