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

Create Unit Test extension update for Xunit #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions src/CreateUnitTest.Xunit/CreateUnitTest.Xunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@
<Compile Include="ProjectTypes.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="XunitFrameworkProvider.cs" />
<Compile Include="XunitFrameworkProvider_2_1.cs" />
<Compile Include="XunitFrameworkProvider_2_0.cs" />
<Compile Include="XunitFrameworkProvider_2_2.cs" />
<Compile Include="XunitFrameworkProvider_2_4.cs" />
<Compile Include="XunitSolutionManager.cs" />
<Compile Include="XunitUnitTestClassManager.cs" />
<Compile Include="XunitUnitTestProjectManager.cs" />
Expand Down
15 changes: 0 additions & 15 deletions src/CreateUnitTest.Xunit/XunitFrameworkProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,23 @@ namespace CreateUnitTest.Xunit
public abstract class XunitFrameworkProvider : FrameworkProviderBase
{
readonly string displayName;
readonly string xunitCoreAssemblyVersionPrefix;

public XunitFrameworkProvider(IServiceProvider serviceProvider,
IConfigurationSettings configurationSettings,
INaming naming,
IDirectory directory,
string displayName,
string xunitPackageVersion,
string xunitCoreAssemblyVersionPrefix,
string visualStudioRunnerPackageVersion)
: base(new XunitSolutionManager(serviceProvider, naming, directory, xunitPackageVersion, visualStudioRunnerPackageVersion),
new XunitUnitTestProjectManager(serviceProvider, naming),
new XunitUnitTestClassManager(configurationSettings, naming))
{
this.displayName = displayName;
this.xunitCoreAssemblyVersionPrefix = xunitCoreAssemblyVersionPrefix;
}

public override string AssemblyName => "xunit.core";

public override string Name => displayName;

public override bool IsTestProject(Project project)
{
if (project == null)
throw new ArgumentNullException("project");

var vsProject2 = project.Object as VSProject2;
if (vsProject2 != null && (project.Kind == ProjectTypes.CSharp || project.Kind == ProjectTypes.VisualBasic))
return vsProject2.References.AsEnumerable().Any(r => r.Name == AssemblyName && r.Version.StartsWith(xunitCoreAssemblyVersionPrefix));

return false;
}
}
}
20 changes: 0 additions & 20 deletions src/CreateUnitTest.Xunit/XunitFrameworkProvider_2_1.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/CreateUnitTest.Xunit/XunitFrameworkProvider_2_2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public XunitFrameworkProvider_2_2(IServiceProvider serviceProvider, IConfigurati
: base(serviceProvider, configurationSettings, naming, directory,
displayName: "xUnit.net 2.2",
xunitPackageVersion: "2.2.0",
xunitCoreAssemblyVersionPrefix: "2.2.0.",
visualStudioRunnerPackageVersion: "2.2.0")
{ }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
namespace CreateUnitTest.Xunit
{
[Export(typeof(IFrameworkProvider))]
public class XunitFrameworkProvider_2_0 : XunitFrameworkProvider
public class XunitFrameworkProvider_2_4 : XunitFrameworkProvider
{
[ImportingConstructor]
public XunitFrameworkProvider_2_0(IServiceProvider serviceProvider, IConfigurationSettings configurationSettings, INaming naming, IDirectory directory)
public XunitFrameworkProvider_2_4(IServiceProvider serviceProvider, IConfigurationSettings configurationSettings, INaming naming, IDirectory directory)
: base(serviceProvider, configurationSettings, naming, directory,
displayName: "xUnit.net 2.0",
xunitPackageVersion: "2.0.0",
xunitCoreAssemblyVersionPrefix: "2.0.0.",
visualStudioRunnerPackageVersion: "2.1.0")
displayName: "xUnit.net 2.4",
xunitPackageVersion: "2.4.1",
visualStudioRunnerPackageVersion: "2.4.1")
{ }
}
}
42 changes: 10 additions & 32 deletions src/CreateUnitTest.Xunit/XunitSolutionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,28 @@
using System.Linq;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TestPlatform.TestGeneration;
using Microsoft.VisualStudio.TestPlatform.TestGeneration.Data;
using Microsoft.VisualStudio.TestPlatform.TestGeneration.Logging;
using Microsoft.VisualStudio.TestPlatform.TestGeneration.Model;
using VSLangProj80;

namespace CreateUnitTest.Xunit
{
public class XunitSolutionManager : SolutionManagerBase
{
readonly IServiceProvider serviceProvider;
readonly Solution2 solution;
readonly string visualStudioRunnerPackageVersion;
readonly string xunitPackageVersion;

static readonly Guid GUID_UniversalWindows = new Guid("{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A}");
static readonly Guid GUID_WindowsPhoneApp81 = new Guid("{76F1466A-8B6D-4E39-A767-685A06062A39}");
static readonly Guid GUID_WindowsStore81 = new Guid("{BC8A1FFA-BEE3-4634-8014-F334798102B3}");

public XunitSolutionManager(IServiceProvider serviceProvider,
INaming naming,
IDirectory directory,
string xunitPackageVersion,
string visualStudioRunnerPackageVersion)
: base(serviceProvider, naming, directory)
{
this.serviceProvider = serviceProvider;
this.xunitPackageVersion = xunitPackageVersion;
this.visualStudioRunnerPackageVersion = visualStudioRunnerPackageVersion;

var dte = (DTE)this.serviceProvider.GetService(typeof(SDTE));
solution = (Solution2)dte.Solution;
}

protected override void OnUnitTestProjectCreated(Project unitTestProject, CodeFunction2 sourceMethod)
Expand All @@ -43,10 +34,8 @@ protected override void OnUnitTestProjectCreated(Project unitTestProject, CodeFu
if (sourceMethod == null)
throw new ArgumentNullException("sourceMethod");

// TODO: Remove references to "MSTestFramework" and "MSTestFramework.Universal"

// Add package reference for xUnit.net
TraceLogger.LogInfo("XunitSolutionManager.OnUnitTestProjectCreated: Adding reference to NuGet packages 'xunit' and 'xunit.runner.visualstudion' (version {0})", xunitPackageVersion);
TraceLogger.LogInfo("XunitSolutionManager.OnUnitTestProjectCreated: Adding reference to NuGet packages 'xunit {0}' and 'xunit.runner.visualstudio {1}'", xunitPackageVersion, visualStudioRunnerPackageVersion);
EnsureNuGetReference(unitTestProject, "xunit", xunitPackageVersion);
EnsureNuGetReference(unitTestProject, "xunit.runner.visualstudio", visualStudioRunnerPackageVersion);

Expand All @@ -60,25 +49,14 @@ protected override void OnUnitTestProjectCreated(Project unitTestProject, CodeFu
projectItem.Delete();
}
}
}

protected override string UnitTestProjectTemplatePath(Project sourceProject)
{
string templateName;
var projectLanguage = VisualStudioHelper.GetProjectLanguage(sourceProject);
var isCSharp = projectLanguage == "CSharp";

var projectTypeGuids = sourceProject.ProjectTypeGuids(serviceProvider).ToList();
if (projectTypeGuids.Contains(GUID_WindowsStore81))
templateName = isCSharp ? "Microsoft.CS.WinRT.UnitTestLibrary" : "Microsoft.VisualBasic.WinRT.UnitTestLibrary";
else if (isCSharp && projectTypeGuids.Contains(GUID_WindowsPhoneApp81)) // No VB template for WPA81, have to fall back to a class library
templateName = "Microsoft.CS.WindowsPhoneApp.UnitTestApp";
else if (projectTypeGuids.Contains(GUID_UniversalWindows))
templateName = isCSharp ? "Microsoft.CSharp.UAP.UnitTestProject" : "Microsoft.VisualBasic.UAP.UnitTestProject";
else
templateName = isCSharp ? "Microsoft.CSharp.ClassLibrary" : "Microsoft.VisualBasic.Windows.ClassLibrary";

return solution.GetProjectTemplate(templateName, projectLanguage);
var vsp = unitTestProject.Object as VSProject2;
var reference = vsp?.References.Find(GlobalConstants.MSTestAssemblyName);
if (reference != null)
{
TraceLogger.LogInfo("NUnitSolutionManager.OnUnitTestProjectCreated: Removing reference to {0}", reference.Name);
reference.Remove();
}
}
}
}
5 changes: 4 additions & 1 deletion src/Package/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Icon>logo-512-transparent.png</Icon>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[14.0,)" />
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[14.0,17.0)" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
Expand All @@ -18,4 +18,7 @@
<Asset Type="Microsoft.VisualStudio.TestGenerationExtension" d:Source="Project" d:ProjectName="CreateUnitTest.Xunit" Path="|CreateUnitTest.Xunit|" />
<Asset Type="Microsoft.VisualStudio.IntellitestExtension" d:Source="Project" d:ProjectName="IntelliTest.Xunit" Path="|IntelliTest.Xunit|" />
</Assets>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,)" DisplayName="Visual Studio core editor" />
</Prerequisites>
</PackageManifest>