Skip to content

Commit

Permalink
Merge pull request #1463 from nunit/issue-1415
Browse files Browse the repository at this point in the history
Validate addins files and display useful message
  • Loading branch information
CharliePoole authored Aug 21, 2024
2 parents 9e119bd + 9454108 commit 355432d
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 950 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace NUnit.Engine.Internal.Tests
{
/// <summary>
/// Tests the implementation of <see cref="AddinsFileReader"/>.
/// Tests the implementation of <see cref="AddinsFile"/>.
/// </summary>
[TestFixture]
public class AddinsFileReaderTests
public class AddinsFileTests
{
[Test]
public void Read_IFile_Null()
{
var reader = new AddinsFileReader();

Assert.That(() => reader.Read((IFile)null), Throws.ArgumentNullException);
Assert.That(() => AddinsFile.Read((IFile)null), Throws.ArgumentNullException);
}

[Test]
public void Read_Stream()
{
var input = string.Join(Environment.NewLine, new string[]
var content = new[]
{
"# This line is a comment and is ignored. The next (blank) line is ignored as well.",
"",
Expand All @@ -36,59 +35,59 @@ public void Read_Stream()
"some/other/directory/ # process another directory, which may contain its own addins file",
"# note that an absolute path is allowed, but is probably not a good idea in most cases",
"/unix/absolute/directory"
});

IEnumerable<string> result;
};

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(input)))
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(string.Join(Environment.NewLine, content))))
{
// Act
result = AddinsFileReader.Read(stream);
var result = AddinsFile.Read(stream);

Assert.That(result, Has.Count.EqualTo(8));
for (int i = 0; i < 8; i++)
Assert.That(result[i], Is.EqualTo(
new AddinsFileEntry(i + 1, content[i])));
}
}

Assert.That(result, Has.Count.EqualTo(5));
Assert.That(result, Contains.Item("*.dll"));
Assert.That(result, Contains.Item("addins/*.dll"));
Assert.That(result, Contains.Item("special/myassembly.dll"));
Assert.That(result, Contains.Item("some/other/directory/"));
Assert.That(result, Contains.Item("/unix/absolute/directory"));
[Test]
public void Read_InvalidEntry()
{
var content = "// This is not valid";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
Assert.That(() => AddinsFile.Read(stream), Throws.Exception);
}
}

[Test]
[Platform("win")]
public void Read_Stream_TransformBackslash_Windows()
{
var input = string.Join(Environment.NewLine, new string[]
{
"c:\\windows\\absolute\\directory"
});
var content = "c:\\windows\\absolute\\directory";

IEnumerable<string> result;

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(input)))
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
// Act
result = AddinsFileReader.Read(stream);
}
var result = AddinsFile.Read(stream);

Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result, Contains.Item("c:/windows/absolute/directory"));
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0], Is.EqualTo(new AddinsFileEntry(1, content)));
Assert.That(result[0].Text, Is.EqualTo("c:/windows/absolute/directory"));
}
}

[Test]
[Platform("linux,macosx,unix")]
public void Read_Stream_TransformBackslash_NonWindows()
{
IEnumerable<string> result;
var content = "this/is/a\\ path\\ with\\ spaces/";

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("this/is/a\\ path\\ with\\ spaces/")))
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
// Act
result = AddinsFileReader.Read(stream);
}
var result = AddinsFile.Read(stream);

Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result, Contains.Item("this/is/a\\ path\\ with\\ spaces/"));
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0], Is.EqualTo(new AddinsFileEntry(1, content)));
Assert.That(result[0].Text, Is.EqualTo(content));
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,36 @@ public void IsFullyQualifiedWindowsPath_PathIsNull()
{
Assert.That(() => PathUtils.IsFullyQualifiedWindowsPath(null), Throws.ArgumentNullException);
}

[TestCase("X")]
[TestCase("X/")]
[TestCase("/X/")]
[TestCase("\\X\\")]
[TestCase("X/Y/Z")]
[TestCase("X/Y/Z/")]
[TestCase("/X/Y/Z")]
[TestCase("/X/Y/Z/")]
[TestCase("\\X\\Y\\Z\\")]
[TestCase("C:X/Y/Z/")]
[TestCase("C:/X/Y/Z")]
[TestCase("C:/X/Y/Z/")]
[TestCase("C:\\X\\Y\\Z\\")]
public void IsValidPath(string path)
{
Assert.That(PathUtils.IsValidPath(path), Is.True);
}

[TestCase(":")]
[TestCase("?")]
[TestCase("*")]
[TestCase("// Spurious comment")]
public void IsValidPath_Fails(string path)
{
Assert.That(PathUtils.IsValidPath(path), Is.False);
}
}

[TestFixture]
[TestFixture]
public class PathUtilDefaultsTests : PathUtils
{
[Test]
Expand Down
Loading

0 comments on commit 355432d

Please sign in to comment.