Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate addins files and display useful message #1463

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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
Loading