diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/CombinedListTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/CombinedListTests.cs index 4897b5af2f8a..904ddb9ba705 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/CombinedListTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/CombinedListTests.cs @@ -1,24 +1,22 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Xunit; - namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class CombinedListTests { - [Theory(DisplayName = nameof(VerifyCombinedListCombinesCorrectly))] - [InlineData(new int[] { }, new int[] { })] - [InlineData(new int[] { 5 }, new int[] { })] - [InlineData(new int[] { }, new int[] { 3 })] - [InlineData(new int[] { 1, 2 }, new int[] { })] - [InlineData(new int[] { }, new int[] { 1, 2 })] - [InlineData(new int[] { 1 }, new int[] { 1 })] - [InlineData(new int[] { 1 }, new int[] { 1, 2, 3 })] - [InlineData(new int[] { 1, 2, 3 }, new int[] { 1 })] - [InlineData(new int[] { 1, 2 }, new int[] { 1, 2, 3 })] - [InlineData(new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3 })] - + [TestMethod] + [DataRow(new int[] { }, new int[] { })] + [DataRow(new int[] { 5 }, new int[] { })] + [DataRow(new int[] { }, new int[] { 3 })] + [DataRow(new int[] { 1, 2 }, new int[] { })] + [DataRow(new int[] { }, new int[] { 1, 2 })] + [DataRow(new int[] { 1 }, new int[] { 1 })] + [DataRow(new int[] { 1 }, new int[] { 1, 2, 3 })] + [DataRow(new int[] { 1, 2, 3 }, new int[] { 1 })] + [DataRow(new int[] { 1, 2 }, new int[] { 1, 2, 3 })] + [DataRow(new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3 })] public void VerifyCombinedListCombinesCorrectly(IReadOnlyList listOne, IReadOnlyList listTwo) { CombinedList combined = new CombinedList(listOne, listTwo); @@ -27,7 +25,7 @@ public void VerifyCombinedListCombinesCorrectly(IReadOnlyList listOne, IRea manuallyAppended.AddRange(listOne); manuallyAppended.AddRange(listTwo); - Assert.Equal(combined.Count, manuallyAppended.Count); + Assert.AreEqual(combined.Count, manuallyAppended.Count); int enumerationCount = 0; foreach (int value in combined) @@ -35,7 +33,7 @@ public void VerifyCombinedListCombinesCorrectly(IReadOnlyList listOne, IRea enumerationCount++; } - Assert.Equal(enumerationCount, combined.Count); + Assert.AreEqual(enumerationCount, combined.Count); } } } diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DefaultTemplatePackageProviderTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DefaultTemplatePackageProviderTests.cs index 52b5ea99c741..f2f25a7c8c7d 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DefaultTemplatePackageProviderTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DefaultTemplatePackageProviderTests.cs @@ -1,23 +1,42 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.NET.TestFramework; using Microsoft.TemplateEngine.Abstractions; using Microsoft.TemplateEngine.TestHelper; -using Xunit; namespace Microsoft.TemplateEngine.Utils.UnitTests { - public class DefaultTemplatePackageProviderTests : IClassFixture + [TestClass] + public class DefaultTemplatePackageProviderTests { - private readonly IEngineEnvironmentSettings _engineEnvironmentSettings; + // MSTest has no IClassFixture equivalent; a lazily-initialized static helper + // mirrors the per-class lifetime that xUnit's IClassFixture provides. + private static readonly Lazy s_environmentSettingsHelper = + new(() => new EnvironmentSettingsHelper(NullMessageSink.Instance)); - public DefaultTemplatePackageProviderTests(EnvironmentSettingsHelper environmentSettingsHelper) + private IEngineEnvironmentSettings _engineEnvironmentSettings = null!; + + public TestContext TestContext { get; set; } = null!; + + [TestInitialize] + public void TestInitialize() + { + _engineEnvironmentSettings = s_environmentSettingsHelper.Value.CreateEnvironment( + hostIdentifier: GetType().Name, + virtualize: true); + } + + [ClassCleanup] + public static void ClassCleanup() { - _engineEnvironmentSettings = environmentSettingsHelper.CreateEnvironment(hostIdentifier: this.GetType().Name, virtualize: true); + if (s_environmentSettingsHelper.IsValueCreated) + { + s_environmentSettingsHelper.Value.Dispose(); + } } - [Fact] + [TestMethod] public async Task ReturnsFoldersAndNuPkgs() { string testAssetsDir = SdkTestContext.Current.TestAssetsDirectory; @@ -29,14 +48,14 @@ public async Task ReturnsFoldersAndNuPkgs() var nupkgs = new[] { Path.Combine(templateEngineTestAssets, "nupkg_templates", "*.nupkg") }; var provider = new DefaultTemplatePackageProvider(null!, _engineEnvironmentSettings, nupkgs, folders); - var sources = await provider.GetAllTemplatePackagesAsync(TestContext.Current.CancellationToken); + var sources = await provider.GetAllTemplatePackagesAsync(TestContext.CancellationToken); //Total should be 7 - Assert.Equal(7, sources.Count); + Assert.AreEqual(7, sources.Count); - Assert.True(sources[0].LastChangeTime > new DateTime(2000, 1, 1)); - Assert.False(string.IsNullOrWhiteSpace(sources[0].MountPointUri)); - Assert.Equal(provider, sources[0].Provider); + Assert.IsTrue(sources[0].LastChangeTime > new DateTime(2000, 1, 1)); + Assert.IsFalse(string.IsNullOrWhiteSpace(sources[0].MountPointUri)); + Assert.AreEqual(provider, sources[0].Provider); } } } diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DirectedGraphTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DirectedGraphTests.cs index ad88af7039c1..e6bc3e210d5f 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DirectedGraphTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/DirectedGraphTests.cs @@ -1,11 +1,11 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using FluentAssertions; -using Xunit; namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class DirectedGraphTests { public static IEnumerable DirectedGraphHasCycleData() @@ -83,23 +83,20 @@ public class DirectedGraphTests }; } - [Theory] - [MemberData(nameof(DirectedGraphHasCycleData))] -#pragma warning disable xUnit1026 // Theory methods should use all of their parameters + [TestMethod] + [DynamicData(nameof(DirectedGraphHasCycleData))] public void HasCycleTests(Dictionary> dependencies, bool shouldHaveCycle, IReadOnlyList expectedCycle, IReadOnlyList unused) -#pragma warning restore xUnit1026 // Theory methods should use all of their parameters { + _ = unused; new DirectedGraph(dependencies).HasCycle(out IReadOnlyList cycle).Should().Be(shouldHaveCycle); cycle.Should().BeEquivalentTo(expectedCycle, options => options.WithStrictOrdering()); - //cycle.SequenceEqual(expectedCycle).Should().BeTrue(); } - [Theory] - [MemberData(nameof(DirectedGraphHasCycleData))] -#pragma warning disable xUnit1026 // Theory methods should use all of their parameters + [TestMethod] + [DynamicData(nameof(DirectedGraphHasCycleData))] public void EnumerateTopologicalSortTests(Dictionary> dependencies, bool shouldHaveCycle, IReadOnlyList unused, IReadOnlyList expectedOrder) -#pragma warning restore xUnit1026 // Theory methods should use all of their parameters { + _ = unused; new DirectedGraph(dependencies).TryGetTopologicalSort(out IReadOnlyList order).Should().Be(!shouldHaveCycle); if (!shouldHaveCycle) { @@ -211,8 +208,8 @@ public void EnumerateTopologicalSortTests(Dictionary> dependen yield return new object?[] { graphB, new List() { 20 }, false, new Dictionary>() { { 1, empty } } }; } - [Theory] - [MemberData(nameof(DirectedGraphSubgraphData))] + [TestMethod] + [DynamicData(nameof(DirectedGraphSubgraphData))] public void GetSubGraphDependandOnVerticesTests(Dictionary> dependencies, IReadOnlyList vertices, bool includeSeedVertices, Dictionary> expectedResult) { var result = new DirectedGraph(dependencies).GetSubGraphDependentOnVertices(vertices, includeSeedVertices); diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/EqualityExtensionsTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/EqualityExtensionsTests.cs index f1834ad648d4..508ce48dd1ae 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/EqualityExtensionsTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/EqualityExtensionsTests.cs @@ -1,13 +1,12 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Xunit; - namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class EqualityExtensionsTests { - [Fact(DisplayName = "AllAreTheSameDefaultComparerTrueTest")] + [TestMethod] public void AllAreTheSameDefaultComparerTrueTest() { IDictionary items = new Dictionary() @@ -19,10 +18,10 @@ public void AllAreTheSameDefaultComparerTrueTest() }; static string Selector(KeyValuePair x) => x.Value; - Assert.True(items.AllAreTheSame(Selector)); + Assert.IsTrue(items.AllAreTheSame(Selector)); } - [Fact(DisplayName = "AllAreTheSameDefaultComparerFailsTest")] + [TestMethod] public void AllAreTheSameDefaultComparerFailsTest() { IDictionary items = new Dictionary() @@ -34,10 +33,10 @@ public void AllAreTheSameDefaultComparerFailsTest() }; static string Selector(KeyValuePair x) => x.Value; - Assert.False(items.AllAreTheSame(Selector)); + Assert.IsFalse(items.AllAreTheSame(Selector)); } - [Fact(DisplayName = "AllAreTheSameCustomComparerTest")] + [TestMethod] public void AllAreTheSameCustomComparerTest() { IDictionary items = new Dictionary() @@ -52,10 +51,10 @@ public void AllAreTheSameCustomComparerTest() static bool LengthComparer(string? x, string? y) => x!.Length == y!.Length; // they're all the same length - Assert.True(items.AllAreTheSame(Selector, LengthComparer)); + Assert.IsTrue(items.AllAreTheSame(Selector, LengthComparer)); static bool UpperComparer(string? x, string? y) => x!.ToUpper() == y!.ToUpper(); - Assert.False(items.AllAreTheSame(Selector, UpperComparer)); + Assert.IsFalse(items.AllAreTheSame(Selector, UpperComparer)); } } } diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobTests.cs index cb5c07557ebf..4c633e58cf37 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobTests.cs @@ -1,122 +1,121 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Xunit; - namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class GlobTests { - [Fact(DisplayName = nameof(VerifyLeadGlobPathSpanning))] + [TestMethod] public void VerifyLeadGlobPathSpanning() { Glob g = Glob.Parse("**/file"); - Assert.True(g.IsMatch("file")); - Assert.True(g.IsMatch("a/file")); - Assert.True(g.IsMatch("a/b/file")); - Assert.True(g.IsMatch("a/b/c/file")); - Assert.False(g.IsMatch("other")); - Assert.False(g.IsMatch("a/other")); - Assert.False(g.IsMatch("a/b/other")); - Assert.False(g.IsMatch("a/b/c/other")); - Assert.False(g.IsMatch("file/stuff")); - Assert.False(g.IsMatch("file.txt")); - Assert.False(g.IsMatch("thefile")); + Assert.IsTrue(g.IsMatch("file")); + Assert.IsTrue(g.IsMatch("a/file")); + Assert.IsTrue(g.IsMatch("a/b/file")); + Assert.IsTrue(g.IsMatch("a/b/c/file")); + Assert.IsFalse(g.IsMatch("other")); + Assert.IsFalse(g.IsMatch("a/other")); + Assert.IsFalse(g.IsMatch("a/b/other")); + Assert.IsFalse(g.IsMatch("a/b/c/other")); + Assert.IsFalse(g.IsMatch("file/stuff")); + Assert.IsFalse(g.IsMatch("file.txt")); + Assert.IsFalse(g.IsMatch("thefile")); } - [Fact(DisplayName = nameof(VerifyGlobExactPathSpanning))] + [TestMethod] public void VerifyGlobExactPathSpanning() { Glob g = Glob.Parse("a/**/b"); - Assert.True(g.IsMatch("a/b")); - Assert.True(g.IsMatch("a/x/b")); - Assert.True(g.IsMatch("a/x/y/b")); - Assert.False(g.IsMatch("z/a/x/y/b")); - Assert.False(g.IsMatch("z/a/b")); + Assert.IsTrue(g.IsMatch("a/b")); + Assert.IsTrue(g.IsMatch("a/x/b")); + Assert.IsTrue(g.IsMatch("a/x/y/b")); + Assert.IsFalse(g.IsMatch("z/a/x/y/b")); + Assert.IsFalse(g.IsMatch("z/a/b")); } - [Fact(DisplayName = nameof(VerifyGlobPathSpanning))] + [TestMethod] public void VerifyGlobPathSpanning() { Glob g = Glob.Parse("a/**"); - Assert.True(g.IsMatch("a/b")); - Assert.True(g.IsMatch("a/x/b")); - Assert.True(g.IsMatch("a/x/y/b")); - Assert.False(g.IsMatch("z/a/x/y/b")); - Assert.False(g.IsMatch("z/a/b")); + Assert.IsTrue(g.IsMatch("a/b")); + Assert.IsTrue(g.IsMatch("a/x/b")); + Assert.IsTrue(g.IsMatch("a/x/y/b")); + Assert.IsFalse(g.IsMatch("z/a/x/y/b")); + Assert.IsFalse(g.IsMatch("z/a/b")); } - [Fact(DisplayName = nameof(VerifyGlobCharacterGroups))] + [TestMethod] public void VerifyGlobCharacterGroups() { Glob g = Glob.Parse("f[Oo]o"); - Assert.True(g.IsMatch("foo")); - Assert.True(g.IsMatch("fOo")); - Assert.True(g.IsMatch("a/foo")); - Assert.True(g.IsMatch("z/a/x/y/fOo")); - Assert.False(g.IsMatch("z/a/x/y/fOO")); + Assert.IsTrue(g.IsMatch("foo")); + Assert.IsTrue(g.IsMatch("fOo")); + Assert.IsTrue(g.IsMatch("a/foo")); + Assert.IsTrue(g.IsMatch("z/a/x/y/fOo")); + Assert.IsFalse(g.IsMatch("z/a/x/y/fOO")); } - [Fact(DisplayName = nameof(VerifyGlobWildcard))] + [TestMethod] public void VerifyGlobWildcard() { Glob g = Glob.Parse("f*o"); - Assert.True(g.IsMatch("foo")); - Assert.True(g.IsMatch("foooooooo")); - Assert.False(g.IsMatch("foot")); + Assert.IsTrue(g.IsMatch("foo")); + Assert.IsTrue(g.IsMatch("foooooooo")); + Assert.IsFalse(g.IsMatch("foot")); } - [Fact(DisplayName = nameof(VerifyGlobNegate))] + [TestMethod] public void VerifyGlobNegate() { Glob g = Glob.Parse("!f*o"); - Assert.False(g.IsMatch("foo")); - Assert.False(g.IsMatch("foooooooo")); - Assert.True(g.IsMatch("foot")); + Assert.IsFalse(g.IsMatch("foo")); + Assert.IsFalse(g.IsMatch("foooooooo")); + Assert.IsTrue(g.IsMatch("foot")); } - [Fact(DisplayName = nameof(VerifyGlobEscape))] + [TestMethod] public void VerifyGlobEscape() { Glob g = Glob.Parse(@"\[[\[\ \]]"); - Assert.True(g.IsMatch("[ ")); - Assert.True(g.IsMatch("[]")); - Assert.True(g.IsMatch("[[")); - Assert.False(g.IsMatch("]")); + Assert.IsTrue(g.IsMatch("[ ")); + Assert.IsTrue(g.IsMatch("[]")); + Assert.IsTrue(g.IsMatch("[[")); + Assert.IsFalse(g.IsMatch("]")); } - [Fact(DisplayName = nameof(VerifyGlobKitchenSink))] + [TestMethod] public void VerifyGlobKitchenSink() { Glob g = Glob.Parse("**/[Dd]ocuments/**/*.htm*"); - Assert.True(g.IsMatch("Documents/git.html")); - Assert.True(g.IsMatch("Documents/ppc/ppc.html")); - Assert.True(g.IsMatch("tools/perf/Documents/perf.html")); + Assert.IsTrue(g.IsMatch("Documents/git.html")); + Assert.IsTrue(g.IsMatch("Documents/ppc/ppc.html")); + Assert.IsTrue(g.IsMatch("tools/perf/Documents/perf.html")); g = Glob.Parse("**/[Dd]ocuments/**/*p*.htm*"); - Assert.False(g.IsMatch("Documents/git.html")); - Assert.True(g.IsMatch("Documents/ppc/ppc.html")); - Assert.True(g.IsMatch("tools/perf/Documents/perf.html")); + Assert.IsFalse(g.IsMatch("Documents/git.html")); + Assert.IsTrue(g.IsMatch("Documents/ppc/ppc.html")); + Assert.IsTrue(g.IsMatch("tools/perf/Documents/perf.html")); g = Glob.Parse("[Dd]ocuments/**/*.htm*"); - Assert.True(g.IsMatch("Documents/git.html")); - Assert.True(g.IsMatch("Documents/ppc/ppc.html")); - Assert.False(g.IsMatch("tools/perf/Documents/perf.html")); + Assert.IsTrue(g.IsMatch("Documents/git.html")); + Assert.IsTrue(g.IsMatch("Documents/ppc/ppc.html")); + Assert.IsFalse(g.IsMatch("tools/perf/Documents/perf.html")); g = Glob.Parse("[Dd]ocuments/**/*.h*"); - Assert.True(g.IsMatch("Documents/git.html")); - Assert.True(g.IsMatch("Documents/ppc/ppc.html")); - Assert.False(g.IsMatch("tools/perf/Documents/perf.html")); + Assert.IsTrue(g.IsMatch("Documents/git.html")); + Assert.IsTrue(g.IsMatch("Documents/ppc/ppc.html")); + Assert.IsFalse(g.IsMatch("tools/perf/Documents/perf.html")); g = Glob.Parse("[Dd]ocuments/**/*.html"); - Assert.True(g.IsMatch("Documents/git.html")); - Assert.True(g.IsMatch("Documents/ppc/ppc.html")); - Assert.False(g.IsMatch("tools/perf/Documents/perf.html")); + Assert.IsTrue(g.IsMatch("Documents/git.html")); + Assert.IsTrue(g.IsMatch("Documents/ppc/ppc.html")); + Assert.IsFalse(g.IsMatch("tools/perf/Documents/perf.html")); g = Glob.Parse("[Dd]ocuments/*.html"); - Assert.True(g.IsMatch("Documents/git.html")); - Assert.False(g.IsMatch("Documents/ppc/ppc.html")); - Assert.False(g.IsMatch("tools/perf/Documents/perf.html")); + Assert.IsTrue(g.IsMatch("Documents/git.html")); + Assert.IsFalse(g.IsMatch("Documents/ppc/ppc.html")); + Assert.IsFalse(g.IsMatch("tools/perf/Documents/perf.html")); } } } diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobalUsings.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobalUsings.cs new file mode 100644 index 000000000000..6203351f775a --- /dev/null +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/GlobalUsings.cs @@ -0,0 +1,10 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Microsoft.TemplateEngine.TestHelper still references xunit.v3.extensibility.core +// (it is consumed by other in-tree xUnit projects and is not part of this migration). +// EnvironmentSettingsHelper's constructor takes an Xunit.Sdk.IMessageSink. We surface +// it here with a short alias so the tests below can construct a no-op sink without +// pulling in xUnit's global usings (which would conflict with MSTest's Assert). +global using IMessageSink = Xunit.Sdk.IMessageSink; +global using IMessageSinkMessage = Xunit.Sdk.IMessageSinkMessage; diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs index 7da80beb1b12..0db9db34e3e3 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InMemoryFileSystemTests.cs @@ -1,15 +1,15 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.TemplateEngine.Abstractions.PhysicalFileSystem; using Microsoft.TemplateEngine.Mocks; -using Xunit; namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class InMemoryFileSystemTests { - [Fact(DisplayName = nameof(VerifyMultipleVirtualizationsAreHandled))] + [TestMethod] public void VerifyMultipleVirtualizationsAreHandled() { IPhysicalFileSystem mockFileSystem = new MockFileSystem(); @@ -18,9 +18,9 @@ public void VerifyMultipleVirtualizationsAreHandled() string testFilePath = Directory.GetCurrentDirectory().CombinePaths("test1", "test.txt"); virtualized2.CreateFile(testFilePath).Dispose(); - Assert.False(mockFileSystem.FileExists(testFilePath)); - Assert.True(virtualized1.FileExists(testFilePath)); - Assert.True(virtualized2.FileExists(testFilePath)); + Assert.IsFalse(mockFileSystem.FileExists(testFilePath)); + Assert.IsTrue(virtualized1.FileExists(testFilePath)); + Assert.IsTrue(virtualized2.FileExists(testFilePath)); } } } diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InstallRequestPathResolutionTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InstallRequestPathResolutionTests.cs index 00043a9c0304..ee030f4cfb37 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InstallRequestPathResolutionTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/InstallRequestPathResolutionTests.cs @@ -1,51 +1,68 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Runtime.InteropServices; using Microsoft.TemplateEngine.Abstractions; using Microsoft.TemplateEngine.TestHelper; -using Xunit; namespace Microsoft.TemplateEngine.Utils.UnitTests { - public class InstallRequestPathResolutionTests : IClassFixture + [TestClass] + public class InstallRequestPathResolutionTests { - private readonly IEngineEnvironmentSettings _engineEnvironmentSettings; + // MSTest has no IClassFixture equivalent; a lazily-initialized static helper + // mirrors the per-class lifetime that xUnit's IClassFixture provides. + private static readonly Lazy s_environmentSettingsHelper = + new(() => new EnvironmentSettingsHelper(NullMessageSink.Instance)); - public InstallRequestPathResolutionTests(EnvironmentSettingsHelper environmentSettingsHelper) + private IEngineEnvironmentSettings _engineEnvironmentSettings = null!; + + [TestInitialize] + public void TestInitialize() + { + _engineEnvironmentSettings = s_environmentSettingsHelper.Value.CreateEnvironment( + hostIdentifier: GetType().Name, + virtualize: true); + } + + [ClassCleanup] + public static void ClassCleanup() { - _engineEnvironmentSettings = environmentSettingsHelper.CreateEnvironment(hostIdentifier: this.GetType().Name, virtualize: true); + if (s_environmentSettingsHelper.IsValueCreated) + { + s_environmentSettingsHelper.Value.Dispose(); + } } - [Fact] + [TestMethod] public void CanResolvePath() { IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(Directory.GetCurrentDirectory(), _engineEnvironmentSettings); - Assert.Equal(Directory.GetCurrentDirectory(), installPath.Single()); + Assert.AreEqual(Directory.GetCurrentDirectory(), installPath.Single()); } - [Fact] + [TestMethod] public void CanTrimTrailingSeparator() { IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, _engineEnvironmentSettings); - Assert.Equal(Directory.GetCurrentDirectory(), installPath.Single()); + Assert.AreEqual(Directory.GetCurrentDirectory(), installPath.Single()); } - [Fact] + [TestMethod] public void CanResolveCurrentPath() { IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(".", _engineEnvironmentSettings); - Assert.Equal(Directory.GetCurrentDirectory(), installPath.Single()); + Assert.AreEqual(Directory.GetCurrentDirectory(), installPath.Single()); } - [Fact] + [TestMethod] public void CanResolveParentPath() { IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath("..", _engineEnvironmentSettings); - Assert.Equal(Path.GetDirectoryName(Directory.GetCurrentDirectory()), installPath.Single()); + Assert.AreEqual(Path.GetDirectoryName(Directory.GetCurrentDirectory()), installPath.Single()); } - [Fact] + [TestMethod] public void CanResolveSubdirectories() { var testRootDir = TestUtils.CreateTemporaryFolder(); @@ -55,11 +72,11 @@ public void CanResolveSubdirectories() IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(Path.Combine(testRootDir, "*"), _engineEnvironmentSettings); - Assert.Equal(3, installPath.Count()); + Assert.AreEqual(3, installPath.Count()); Assert.Contains(Path.Combine(testRootDir, "dir1"), installPath); } - [Fact] + [TestMethod] public void CanResolveMaskedSubdirectories() { var testRootDir = TestUtils.CreateTemporaryFolder(); @@ -69,12 +86,12 @@ public void CanResolveMaskedSubdirectories() IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(Path.Combine(testRootDir, "dir*"), _engineEnvironmentSettings); - Assert.Equal(2, installPath.Count()); + Assert.AreEqual(2, installPath.Count()); Assert.Contains(Path.Combine(testRootDir, "dir1"), installPath); Assert.Contains(Path.Combine(testRootDir, "dir33"), installPath); } - [Fact] + [TestMethod] public void CanResolveMaskedFiles() { var testRootDir = TestUtils.CreateTemporaryFolder(); @@ -84,30 +101,30 @@ public void CanResolveMaskedFiles() IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(Path.Combine(testRootDir, "*.nupkg"), _engineEnvironmentSettings); - Assert.Equal(2, installPath.Count()); + Assert.AreEqual(2, installPath.Count()); Assert.Contains(Path.Combine(testRootDir, "1.nupkg"), installPath); Assert.Contains(Path.Combine(testRootDir, "2.nupkg"), installPath); } - [Fact] + [TestMethod] public void CannotResolveInvalidPath() { IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath("|path|", _engineEnvironmentSettings); - Assert.Equal("|path|", installPath.Single()); + Assert.AreEqual("|path|", installPath.Single()); } - [Fact] + [TestMethod] public void CannotResolveNonExistingPath() { - Assert.False(File.Exists("path")); + Assert.IsFalse(File.Exists("path")); IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath("path", _engineEnvironmentSettings); - Assert.Equal("path", installPath.Single()); + Assert.AreEqual("path", installPath.Single()); installPath = InstallRequestPathResolution.ExpandMaskedPath("path\\", _engineEnvironmentSettings); - Assert.Equal("path\\", installPath.Single()); + Assert.AreEqual("path\\", installPath.Single()); } - [Fact] + [TestMethod] public void CannotResolveMaskedPathInFolder() { var testRootDir = TestUtils.CreateTemporaryFolder(); @@ -116,16 +133,16 @@ public void CannotResolveMaskedPathInFolder() File.Create(Path.Combine(testRootDir, "dir", "2.nupkg")); IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(Path.Combine(testRootDir, "*", "*.nupkg"), _engineEnvironmentSettings); - Assert.Equal(Path.Combine(testRootDir, "*", "*.nupkg"), installPath.Single()); + Assert.AreEqual(Path.Combine(testRootDir, "*", "*.nupkg"), installPath.Single()); } - [Fact] + [TestMethod] public void CanResolveParentOfRootFolder() { string dir = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\" : "/"; IEnumerable installPath = InstallRequestPathResolution.ExpandMaskedPath(dir + "..", _engineEnvironmentSettings); - Assert.Equal(dir, installPath.Single()); + Assert.AreEqual(dir, installPath.Single()); } } } diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/ListExtensionsTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/ListExtensionsTests.cs index c6eea96dd72d..e13316ff54c2 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/ListExtensionsTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/ListExtensionsTests.cs @@ -1,13 +1,12 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Xunit; - namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class ListExtensionsTests { - [Fact(DisplayName = nameof(GroupByExtensionTest))] + [TestMethod] public void GroupByExtensionTest() { List templatesToGroup = new List @@ -60,12 +59,12 @@ public void GroupByExtensionTest() }; var templateGroups = templatesToGroup.GroupBy(x => x._groupIdentity, x => !string.IsNullOrEmpty(x._groupIdentity), StringComparer.OrdinalIgnoreCase); - Assert.Equal(7, templateGroups.Count()); + Assert.AreEqual(7, templateGroups.Count()); var groupWithExpectedMultipleElements = templateGroups.Single(g => g.Key?.Equals("TemplateGroup", StringComparison.OrdinalIgnoreCase) ?? false); - Assert.Equal(3, groupWithExpectedMultipleElements.Count()); - Assert.Single(groupWithExpectedMultipleElements, s => s._identity == "5"); - Assert.Single(groupWithExpectedMultipleElements, s => s._identity == "6"); - Assert.Single(groupWithExpectedMultipleElements, s => s._identity == "9"); + Assert.AreEqual(3, groupWithExpectedMultipleElements.Count()); + Assert.AreEqual(1, groupWithExpectedMultipleElements.Count(s => s._identity == "5")); + Assert.AreEqual(1, groupWithExpectedMultipleElements.Count(s => s._identity == "6")); + Assert.AreEqual(1, groupWithExpectedMultipleElements.Count(s => s._identity == "9")); } internal struct GroupByTestStruct diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/Microsoft.TemplateEngine.Utils.UnitTests.csproj b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/Microsoft.TemplateEngine.Utils.UnitTests.csproj index f98adba4e2f7..5ee2997deb66 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/Microsoft.TemplateEngine.Utils.UnitTests.csproj +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/Microsoft.TemplateEngine.Utils.UnitTests.csproj @@ -1,7 +1,9 @@ - + $(NetCurrent);$(NetFrameworkCurrent) + true + enable @@ -10,9 +12,4 @@ - - - - - diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/NullMessageSink.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/NullMessageSink.cs new file mode 100644 index 000000000000..35022b22fe52 --- /dev/null +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/NullMessageSink.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace Microsoft.TemplateEngine.Utils.UnitTests; + +/// +/// A no-op used to satisfy +/// 's constructor +/// in an MSTest context (no xUnit runner is present to provide one). +/// +internal sealed class NullMessageSink : IMessageSink +{ + public static readonly IMessageSink Instance = new NullMessageSink(); + + private NullMessageSink() + { + } + + public bool OnMessage(IMessageSinkMessage message) => true; +} diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/VersionStringTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/VersionStringTests.cs index deb189ee15eb..eee39be40f33 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/VersionStringTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/VersionStringTests.cs @@ -1,74 +1,73 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Xunit; - namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class VersionStringTests { - [Theory(DisplayName = nameof(VerifyVersionIsWellFormedCheckerTest))] - [InlineData("1.0.0.0", true)] - [InlineData("1.0.0", true)] - [InlineData("1.0", true)] - [InlineData("1", false)] - [InlineData("1.0.0.0.", false)] - [InlineData("1.0.0.", false)] - [InlineData("1.0.", false)] - [InlineData("1.", false)] - [InlineData("MyVersion", false)] - [InlineData("1.0.0.A", false)] - [InlineData("A.0.0.0", false)] - [InlineData("", false)] - [InlineData(null, false)] + [TestMethod] + [DataRow("1.0.0.0", true)] + [DataRow("1.0.0", true)] + [DataRow("1.0", true)] + [DataRow("1", false)] + [DataRow("1.0.0.0.", false)] + [DataRow("1.0.0.", false)] + [DataRow("1.0.", false)] + [DataRow("1.", false)] + [DataRow("MyVersion", false)] + [DataRow("1.0.0.A", false)] + [DataRow("A.0.0.0", false)] + [DataRow("", false)] + [DataRow(null, false)] public void VerifyVersionIsWellFormedCheckerTest(string? versionString, bool expectedParseResult) { - Assert.Equal(expectedParseResult, VersionStringHelpers.IsVersionWellFormed(versionString)); + Assert.AreEqual(expectedParseResult, VersionStringHelpers.IsVersionWellFormed(versionString)); } - [Theory(DisplayName = nameof(VerifyVersionComparisonTest))] - [InlineData("", "", null)] - [InlineData(null, null, null)] - [InlineData("1.0.0.0", "1.0.0.0", 0)] - [InlineData("1.0.0.0", null, null)] - [InlineData(null, "1.0.0.0", null)] - [InlineData("1.0.0.0", "1.1.0.0", -1)] - [InlineData("1.1.0.0", "1.0.0.0", 1)] - [InlineData("1.0.0", "1.1", -1)] - [InlineData("1.1", "1.0.0", 1)] + [TestMethod] + [DataRow("", "", null)] + [DataRow(null, null, null)] + [DataRow("1.0.0.0", "1.0.0.0", 0)] + [DataRow("1.0.0.0", null, null)] + [DataRow(null, "1.0.0.0", null)] + [DataRow("1.0.0.0", "1.1.0.0", -1)] + [DataRow("1.1.0.0", "1.0.0.0", 1)] + [DataRow("1.0.0", "1.1", -1)] + [DataRow("1.1", "1.0.0", 1)] public void VerifyVersionComparisonTest(string? version1, string? version2, int? expectedComparison) { - Assert.Equal(expectedComparison, VersionStringHelpers.CompareVersions(version1, version2)); + Assert.AreEqual(expectedComparison, VersionStringHelpers.CompareVersions(version1, version2)); } - [Theory(DisplayName = nameof(VersionParseCompare))] - [InlineData("1.0.0.0", "1.0.0.0", true)] - [InlineData("1.0.0.0", "1.0.0", true)] - [InlineData("1.0.0.0", "1.0", true)] - [InlineData("1.0.0.0", "1.1", false)] - [InlineData("2.0.0.0", "1.1", false)] - [InlineData("[1.0.0.0-*)", "1.0.0.0", true)] - [InlineData("[1.0.0.0-*)", "1.1.0.0", true)] - [InlineData("[1.0.0.0-*)", "1.0.1.0", true)] - [InlineData("[1.0.0.0-*)", "1.0.0.1", true)] - [InlineData("(1.0.0.0-*)", "1.0.0.0", false)] - [InlineData("(*-2.0.0.0)", "1.0.0.0", true)] - [InlineData("(*-2.0.0.0)", "1.5.0.0", true)] - [InlineData("(*-2.0.0.0)", "2.0.0.0", false)] - [InlineData("(*-2.0.0.0]", "2.0.0.0", true)] - [InlineData("[1.1.0.0-1.2.0.0]", "1.0.0.0", false)] - [InlineData("[1.1.0.0-1.2.0.0]", "1.1.0.0", true)] - [InlineData("[1.1.0.0-1.2.0.0]", "1.2.0.0", true)] - [InlineData("[1.1.0.0-1.2.0.0]", "1.2.0.1", false)] - [InlineData("(1.1.0.0-1.2.0.0)", "1.1.0.0", false)] - [InlineData("(1.1.0.0-1.2.0.0)", "1.2.0.0", false)] - [InlineData("(1.1.0.0-1.2.0.0)", "1.1.1.0", true)] - [InlineData("(1.1.0.0-1.2.0.0)", "1.1.0.1", true)] + [TestMethod] + [DataRow("1.0.0.0", "1.0.0.0", true)] + [DataRow("1.0.0.0", "1.0.0", true)] + [DataRow("1.0.0.0", "1.0", true)] + [DataRow("1.0.0.0", "1.1", false)] + [DataRow("2.0.0.0", "1.1", false)] + [DataRow("[1.0.0.0-*)", "1.0.0.0", true)] + [DataRow("[1.0.0.0-*)", "1.1.0.0", true)] + [DataRow("[1.0.0.0-*)", "1.0.1.0", true)] + [DataRow("[1.0.0.0-*)", "1.0.0.1", true)] + [DataRow("(1.0.0.0-*)", "1.0.0.0", false)] + [DataRow("(*-2.0.0.0)", "1.0.0.0", true)] + [DataRow("(*-2.0.0.0)", "1.5.0.0", true)] + [DataRow("(*-2.0.0.0)", "2.0.0.0", false)] + [DataRow("(*-2.0.0.0]", "2.0.0.0", true)] + [DataRow("[1.1.0.0-1.2.0.0]", "1.0.0.0", false)] + [DataRow("[1.1.0.0-1.2.0.0]", "1.1.0.0", true)] + [DataRow("[1.1.0.0-1.2.0.0]", "1.2.0.0", true)] + [DataRow("[1.1.0.0-1.2.0.0]", "1.2.0.1", false)] + [DataRow("(1.1.0.0-1.2.0.0)", "1.1.0.0", false)] + [DataRow("(1.1.0.0-1.2.0.0)", "1.2.0.0", false)] + [DataRow("(1.1.0.0-1.2.0.0)", "1.1.1.0", true)] + [DataRow("(1.1.0.0-1.2.0.0)", "1.1.0.1", true)] public void VersionParseCompare(string allowed, string proposed, bool expected) { - Assert.True(VersionStringHelpers.TryParseVersionSpecification(allowed, out IVersionSpecification? checker)); - Assert.NotNull(checker); - Assert.Equal(expected, checker.CheckIfVersionIsValid(proposed)); + Assert.IsTrue(VersionStringHelpers.TryParseVersionSpecification(allowed, out IVersionSpecification? checker)); + Assert.IsNotNull(checker); + Assert.AreEqual(expected, checker.CheckIfVersionIsValid(proposed)); } } } diff --git a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/WellKnownSearchFiltersTests.cs b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/WellKnownSearchFiltersTests.cs index 81333ab45a2f..9b04716418dc 100644 --- a/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/WellKnownSearchFiltersTests.cs +++ b/test/TemplateEngine/Microsoft.TemplateEngine.Utils.UnitTests/WellKnownSearchFiltersTests.cs @@ -1,19 +1,19 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.TemplateEngine.Abstractions.TemplateFiltering; using Microsoft.TemplateEngine.Mocks; -using Xunit; namespace Microsoft.TemplateEngine.Utils.UnitTests { + [TestClass] public class WellKnownSearchFiltersTests { - [Theory] - [InlineData("test", "test", MatchKind.Exact)] - [InlineData("test1||test2", "test1", MatchKind.Exact)] - [InlineData("test1||test2", "test", MatchKind.Mismatch)] - [InlineData("test1||test2", null, null)] + [TestMethod] + [DataRow("test", "test", MatchKind.Exact)] + [DataRow("test1||test2", "test1", MatchKind.Exact)] + [DataRow("test1||test2", "test", MatchKind.Mismatch)] + [DataRow("test1||test2", null, null)] public void TagFilterTests_TemplateWithTags(string templateTags, string? testTag, MatchKind? kind) { const string separator = "||"; @@ -27,12 +27,12 @@ public void TagFilterTests_TemplateWithTags(string templateTags, string? testTag var filter = WellKnownSearchFilters.ClassificationFilter(testTag); MatchInfo? result = filter(template); - Assert.Equal(kind, result?.Kind); + Assert.AreEqual(kind, result?.Kind); } - [Theory] - [InlineData("test", MatchKind.Mismatch)] - [InlineData(null, null)] + [TestMethod] + [DataRow("test", MatchKind.Mismatch)] + [DataRow(null, null)] public void TagFilterTests_TemplateWithoutTags(string? testTag, MatchKind? kind) { MockTemplateInfo template = new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.T1", groupIdentity: "Console.App.Test") @@ -42,7 +42,7 @@ public void TagFilterTests_TemplateWithoutTags(string? testTag, MatchKind? kind) var filter = WellKnownSearchFilters.ClassificationFilter(testTag); MatchInfo? result = filter(template); - Assert.Equal(kind, result?.Kind); + Assert.AreEqual(kind, result?.Kind); } } }