V10: fix build warnings in test projects (#12509)

* Run code cleanup

* Dotnet format benchmarks project

* Fix up Test.Common

* Run dotnet format + manual cleanup

* Run code cleanup for unit tests

* Run dotnet format

* Fix up errors

* Manual cleanup of Unit test project

* Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix according to review

* Fix after merge

* Fix errors

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
Nikolaj Geisle
2022-06-21 08:09:38 +02:00
committed by GitHub
parent 29961d40a3
commit 7aeb400fce
599 changed files with 87303 additions and 86123 deletions

View File

@@ -2,189 +2,186 @@
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using NUnit.Framework;
using Umbraco.Cms.Core.IO;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.IO
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.IO;
[TestFixture]
public abstract class AbstractFileSystemTests
{
[TestFixture]
public abstract class AbstractFileSystemTests
protected IFileSystem _fileSystem;
protected AbstractFileSystemTests(IFileSystem fileSystem) => _fileSystem = fileSystem;
[Test]
public void Can_Create_And_Delete_Files()
{
protected IFileSystem _fileSystem;
_fileSystem.AddFile("test.txt", CreateStream());
protected AbstractFileSystemTests(IFileSystem fileSystem) => _fileSystem = fileSystem;
Assert.IsTrue(_fileSystem.FileExists("test.txt"));
[Test]
public void Can_Create_And_Delete_Files()
{
_fileSystem.AddFile("test.txt", CreateStream());
_fileSystem.DeleteFile("test.txt");
Assert.IsTrue(_fileSystem.FileExists("test.txt"));
_fileSystem.DeleteFile("test.txt");
Assert.IsFalse(_fileSystem.FileExists("test.txt"));
}
[Test]
public void Can_Overwrite_File()
{
_fileSystem.AddFile("test/test.txt", CreateStream());
_fileSystem.AddFile("test/test.txt", CreateStream());
IEnumerable<string> files = _fileSystem.GetFiles("test");
Assert.AreEqual(1, files.Count());
foreach (var file in files)
{
_fileSystem.DeleteFile(file);
}
_fileSystem.DeleteDirectory("test", true);
}
[Test]
public void Cant_Overwrite_File() =>
Assert.Throws<InvalidOperationException>(() =>
{
_fileSystem.AddFile("test.txt", CreateStream());
_fileSystem.AddFile("test.txt", CreateStream(), false);
_fileSystem.DeleteFile("test.txt");
});
[Test]
public void Can_Get_Files()
{
_fileSystem.AddFile("test/test1.txt", CreateStream());
_fileSystem.AddFile("test/test2.txt", CreateStream());
_fileSystem.AddFile("test/test3.txt", CreateStream());
_fileSystem.AddFile("test/test4.bak", CreateStream());
IEnumerable<string> files = _fileSystem.GetFiles("test");
Assert.AreEqual(4, files.Count());
files = _fileSystem.GetFiles("test", "*.txt");
Assert.AreEqual(3, files.Count());
_fileSystem.DeleteDirectory("test", true);
}
[Test]
public void Can_Read_File()
{
_fileSystem.AddFile("test.txt", CreateStream("hello world"));
Stream stream = _fileSystem.OpenFile("test.txt");
var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
reader.Close();
Assert.AreEqual("hello world", contents);
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Get_Directories()
{
_fileSystem.AddFile("test/sub1/test.txt", CreateStream());
_fileSystem.AddFile("test/sub2/test.txt", CreateStream());
_fileSystem.AddFile("test/sub3/test.txt", CreateStream());
IEnumerable<string> dirs = _fileSystem.GetDirectories("test");
Assert.AreEqual(3, dirs.Count());
Assert.IsTrue(_fileSystem.DirectoryExists("test/sub1"));
Assert.IsTrue(_fileSystem.DirectoryExists("test/sub2"));
Assert.IsTrue(_fileSystem.DirectoryExists("test/sub3"));
_fileSystem.DeleteDirectory("test", true);
}
[Test]
public void Can_Get_File_Dates()
{
_fileSystem.DeleteFile("test.txt");
_fileSystem.AddFile("test.txt", CreateStream());
DateTimeOffset created = _fileSystem.GetCreated("test.txt");
DateTimeOffset modified = _fileSystem.GetLastModified("test.txt");
Assert.AreEqual(DateTime.UtcNow.Year, created.Year);
Assert.AreEqual(DateTime.UtcNow.Month, created.Month);
Assert.AreEqual(DateTime.UtcNow.Date, created.Date);
Assert.AreEqual(DateTime.UtcNow.Year, modified.Year);
Assert.AreEqual(DateTime.UtcNow.Month, modified.Month);
Assert.AreEqual(DateTime.UtcNow.Date, modified.Date);
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Get_File_Url()
{
_fileSystem.AddFile("test.txt", CreateStream());
var url = _fileSystem.GetUrl("test.txt");
Assert.AreEqual(ConstructUrl("test.txt"), url);
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Convert_Full_Path_And_Url_To_Relative_Path()
{
_fileSystem.AddFile("test.txt", CreateStream());
var url = _fileSystem.GetUrl("test.txt");
var fullPath = _fileSystem.GetFullPath("test.txt");
Assert.AreNotEqual("test.txt", url);
Assert.AreNotEqual("test.txt", fullPath);
Assert.AreEqual("test.txt", _fileSystem.GetRelativePath(url));
Assert.AreEqual("test.txt", _fileSystem.GetRelativePath(fullPath));
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Get_Size()
{
Stream stream = CreateStream();
var streamLength = stream.Length;
_fileSystem.AddFile("test.txt", stream);
Assert.AreEqual(streamLength, _fileSystem.GetSize("test.txt"));
_fileSystem.DeleteFile("test.txt");
}
protected Stream CreateStream(string contents = null)
{
if (string.IsNullOrEmpty(contents))
{
contents = "test";
}
var bytes = Encoding.UTF8.GetBytes(contents);
var stream = new MemoryStream(bytes);
return stream;
}
protected abstract string ConstructUrl(string path);
Assert.IsFalse(_fileSystem.FileExists("test.txt"));
}
[Test]
public void Can_Overwrite_File()
{
_fileSystem.AddFile("test/test.txt", CreateStream());
_fileSystem.AddFile("test/test.txt", CreateStream());
var files = _fileSystem.GetFiles("test").ToArray();
Assert.AreEqual(1, files.Count());
foreach (var file in files)
{
_fileSystem.DeleteFile(file);
}
_fileSystem.DeleteDirectory("test", true);
}
[Test]
public void Cant_Overwrite_File() =>
Assert.Throws<InvalidOperationException>(() =>
{
_fileSystem.AddFile("test.txt", CreateStream());
_fileSystem.AddFile("test.txt", CreateStream(), false);
_fileSystem.DeleteFile("test.txt");
});
[Test]
public void Can_Get_Files()
{
_fileSystem.AddFile("test/test1.txt", CreateStream());
_fileSystem.AddFile("test/test2.txt", CreateStream());
_fileSystem.AddFile("test/test3.txt", CreateStream());
_fileSystem.AddFile("test/test4.bak", CreateStream());
var files = _fileSystem.GetFiles("test");
Assert.AreEqual(4, files.Count());
files = _fileSystem.GetFiles("test", "*.txt");
Assert.AreEqual(3, files.Count());
_fileSystem.DeleteDirectory("test", true);
}
[Test]
public void Can_Read_File()
{
_fileSystem.AddFile("test.txt", CreateStream("hello world"));
var stream = _fileSystem.OpenFile("test.txt");
var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
reader.Close();
Assert.AreEqual("hello world", contents);
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Get_Directories()
{
_fileSystem.AddFile("test/sub1/test.txt", CreateStream());
_fileSystem.AddFile("test/sub2/test.txt", CreateStream());
_fileSystem.AddFile("test/sub3/test.txt", CreateStream());
var dirs = _fileSystem.GetDirectories("test");
Assert.AreEqual(3, dirs.Count());
Assert.IsTrue(_fileSystem.DirectoryExists("test/sub1"));
Assert.IsTrue(_fileSystem.DirectoryExists("test/sub2"));
Assert.IsTrue(_fileSystem.DirectoryExists("test/sub3"));
_fileSystem.DeleteDirectory("test", true);
}
[Test]
public void Can_Get_File_Dates()
{
_fileSystem.DeleteFile("test.txt");
_fileSystem.AddFile("test.txt", CreateStream());
var created = _fileSystem.GetCreated("test.txt");
var modified = _fileSystem.GetLastModified("test.txt");
Assert.AreEqual(DateTime.UtcNow.Year, created.Year);
Assert.AreEqual(DateTime.UtcNow.Month, created.Month);
Assert.AreEqual(DateTime.UtcNow.Date, created.Date);
Assert.AreEqual(DateTime.UtcNow.Year, modified.Year);
Assert.AreEqual(DateTime.UtcNow.Month, modified.Month);
Assert.AreEqual(DateTime.UtcNow.Date, modified.Date);
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Get_File_Url()
{
_fileSystem.AddFile("test.txt", CreateStream());
var url = _fileSystem.GetUrl("test.txt");
Assert.AreEqual(ConstructUrl("test.txt"), url);
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Convert_Full_Path_And_Url_To_Relative_Path()
{
_fileSystem.AddFile("test.txt", CreateStream());
var url = _fileSystem.GetUrl("test.txt");
var fullPath = _fileSystem.GetFullPath("test.txt");
Assert.AreNotEqual("test.txt", url);
Assert.AreNotEqual("test.txt", fullPath);
Assert.AreEqual("test.txt", _fileSystem.GetRelativePath(url));
Assert.AreEqual("test.txt", _fileSystem.GetRelativePath(fullPath));
_fileSystem.DeleteFile("test.txt");
}
[Test]
public void Can_Get_Size()
{
var stream = CreateStream();
var streamLength = stream.Length;
_fileSystem.AddFile("test.txt", stream);
Assert.AreEqual(streamLength, _fileSystem.GetSize("test.txt"));
_fileSystem.DeleteFile("test.txt");
}
protected Stream CreateStream(string contents = null)
{
if (string.IsNullOrEmpty(contents))
{
contents = "test";
}
var bytes = Encoding.UTF8.GetBytes(contents);
var stream = new MemoryStream(bytes);
return stream;
}
protected abstract string ConstructUrl(string path);
}

View File

@@ -4,120 +4,123 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Tests.UnitTests.TestHelpers;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.IO
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.IO;
[TestFixture]
public class PhysicalFileSystemTests : AbstractFileSystemTests
{
[TestFixture]
public class PhysicalFileSystemTests : AbstractFileSystemTests
[SetUp]
public void Setup()
{
public PhysicalFileSystemTests()
: base(new PhysicalFileSystem(TestHelper.IOHelper, TestHelper.GetHostingEnvironment(), Mock.Of<ILogger<PhysicalFileSystem>>(), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests"), "/Media/"))
}
[TearDown]
public void TearDown()
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests");
if (Directory.Exists(path) == false)
{
return;
}
[SetUp]
public void Setup()
var files = Directory.GetFiles(path);
foreach (var f in files)
{
File.Delete(f);
}
[TearDown]
public void TearDown()
Directory.Delete(path, true);
}
public PhysicalFileSystemTests()
: base(new PhysicalFileSystem(
TestHelper.IOHelper,
TestHelper.GetHostingEnvironment(),
Mock.Of<ILogger<PhysicalFileSystem>>(),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests"),
"/Media/"))
{
}
protected override string ConstructUrl(string path) => "/Media/" + path;
private string Repeat(string pattern, int count)
{
var text = new StringBuilder();
for (var i = 0; i < count; i++)
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests");
if (Directory.Exists(path) == false)
{
return;
}
var files = Directory.GetFiles(path);
foreach (var f in files)
{
File.Delete(f);
}
Directory.Delete(path, true);
text.Append(pattern);
}
protected override string ConstructUrl(string path) => "/Media/" + path;
return text.ToString();
}
private string Repeat(string pattern, int count)
[Test]
public void SaveFileTest()
{
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests");
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
{
var text = new StringBuilder();
for (var i = 0; i < count; i++)
{
text.Append(pattern);
}
return text.ToString();
_fileSystem.AddFile("sub/f3.txt", ms);
}
[Test]
public void SaveFileTest()
Assert.IsTrue(File.Exists(Path.Combine(basePath, "sub/f3.txt")));
var path = Repeat("bah/bah/", 50);
Assert.Less(260, path.Length);
Assert.Throws<PathTooLongException>(() =>
{
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests");
using var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo"));
_fileSystem.AddFile(path + "f3.txt", ms);
});
}
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo")))
{
_fileSystem.AddFile("sub/f3.txt", ms);
}
[Test]
public void GetFullPathTest()
{
// outside of tests, one initializes the PhysicalFileSystem with eg ~/Dir
// and then, rootPath = /path/to/Dir and rootUrl = /Dir/
// here we initialize the PhysicalFileSystem with
// rootPath = /path/to/FileSysTests
// rootUrl = /Media/
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests");
Assert.IsTrue(File.Exists(Path.Combine(basePath, "sub/f3.txt")));
// ensure that GetFullPath
// - does return the proper full path
// - does properly normalize separators
// - does throw on invalid paths
// works
var path = _fileSystem.GetFullPath("foo.tmp");
Assert.AreEqual(Path.Combine(basePath, @"foo.tmp"), path);
var path = Repeat("bah/bah/", 50);
Assert.Less(260, path.Length);
// a very long relative path, which ends up being a short path, works
path = Repeat("bah/../", 50);
Assert.Less(260, path.Length);
path = _fileSystem.GetFullPath(path + "foo.tmp");
Assert.AreEqual(Path.Combine(basePath, @"foo.tmp"), path);
Assert.Throws<PathTooLongException>(() =>
{
using var ms = new MemoryStream(Encoding.UTF8.GetBytes("foo"));
_fileSystem.AddFile(path + "f3.txt", ms);
});
}
// works too
path = _fileSystem.GetFullPath("foo/bar.tmp");
Assert.AreEqual(Path.Combine(basePath, @$"foo{Path.DirectorySeparatorChar}bar.tmp"), path);
[Test]
public void GetFullPathTest()
// that path is invalid as it would be outside the root directory
Assert.Throws<UnauthorizedAccessException>(() => _fileSystem.GetFullPath("../../foo.tmp"));
// a very long path, which ends up being very long, works
path = Repeat("bah/bah/", 50);
Assert.Less(260, path.Length);
Assert.Throws<PathTooLongException>(() =>
{
// outside of tests, one initializes the PhysicalFileSystem with eg ~/Dir
// and then, rootPath = /path/to/Dir and rootUrl = /Dir/
// here we initialize the PhysicalFileSystem with
// rootPath = /path/to/FileSysTests
// rootUrl = /Media/
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileSysTests");
// ensure that GetFullPath
// - does return the proper full path
// - does properly normalize separators
// - does throw on invalid paths
// works
var path = _fileSystem.GetFullPath("foo.tmp");
Assert.AreEqual(Path.Combine(basePath, @"foo.tmp"), path);
// a very long relative path, which ends up being a short path, works
path = Repeat("bah/../", 50);
Assert.Less(260, path.Length);
path = _fileSystem.GetFullPath(path + "foo.tmp");
Assert.AreEqual(Path.Combine(basePath, @"foo.tmp"), path);
// works too
path = _fileSystem.GetFullPath("foo/bar.tmp");
Assert.AreEqual(Path.Combine(basePath, @$"foo{Path.DirectorySeparatorChar}bar.tmp"), path);
// that path is invalid as it would be outside the root directory
Assert.Throws<UnauthorizedAccessException>(() => _fileSystem.GetFullPath("../../foo.tmp"));
// a very long path, which ends up being very long, works
path = Repeat("bah/bah/", 50);
Assert.Less(260, path.Length);
Assert.Throws<PathTooLongException>(() =>
{
path = _fileSystem.GetFullPath(path + "foo.tmp");
Assert.Less(260, path.Length); // gets a >260 path and it's fine (but Windows will not like it)
});
}
Assert.Less(260, path.Length); // gets a >260 path and it's fine (but Windows will not like it)
});
}
}