Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
15 changes: 5 additions & 10 deletions Reqnroll.VisualStudio.ProjectTemplate/ProjectTemplate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />$if$ ('$unittestframework$' == 'xUnit')
<PackageReference Include="Reqnroll.xUnit" Version="2.2.1" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1" />$endif$$if$ ('$unittestframework$' == 'NUnit')
<PackageReference Include="Reqnroll.NUnit" Version="2.2.1" />
<PackageReference Include="nunit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />$endif$$if$ ('$unittestframework$' == 'MSTest')
<PackageReference Include="Reqnroll.MsTest" Version="2.2.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />$endif$$if$ ('$fluentassertionsincluded$' == 'True')
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
$Reqnroll_Lib$
$TestFramework_Lib$
$Adapter_Lib$
$if$ ('$fluentassertionsincluded$' == 'True')
<PackageReference Include="FluentAssertions" Version="6.12.0" />$endif$
</ItemGroup>

Expand Down
5 changes: 5 additions & 0 deletions Reqnroll.VisualStudio/Reqnroll.VisualStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
<DefineConstants>TRACE;</DefineConstants>
</PropertyGroup>

<ItemGroup>
<None Remove="Resources\TestFrameworkDescriptors.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Analytics\InstrumentationKey.txt" />
<EmbeddedResource Include="Resources\TestFrameworkDescriptors.json" />
</ItemGroup>

<ItemGroup>
Expand Down
53 changes: 53 additions & 0 deletions Reqnroll.VisualStudio/Resources/TestFrameworkDescriptors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"xUnit": {
"description": "Use xUnit v2 test executor with Reqnroll",
"dependencies": {
"Reqnroll_Lib": {
"name": "Reqnroll.xUnit",
"version": "2.4.1"
},
"TestFramework_Lib": {
"name": "xunit",
"version": "2.8.1"
},
"Adapter_Lib": {
"name": "xunit.runner.visualstudio",
"version": "2.8.1"
}
}
},
"MsTest": {
"description": "Use MsTest v3 test executor with Reqnroll",
"dependencies": {
"Reqnroll_Lib": {
"name": "Reqnroll.MsTest",
"version": "2.4.1"
},
"TestFramework_Lib": {
"name": "MSTest.TestFramework",
"version": "3.4.3"
},
"Adapter_Lib": {
"name": "MSTest.TestAdapter",
"version": "3.4.3"
}
}
},
"NUnit": {
"description": "Use NUnit v3 test executor with Reqnroll",
"dependencies": {
"Reqnroll_Lib": {
"name": "Reqnroll.NUnit",
"version": "2.4.1"
},
"TestFramework_Lib": {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fear that there will be test frameworks that require more than one dependencies (maybe in some combinations we might need even multiple Reqnroll dependencies too), so I think I would prefer having a general list of dependencies that we just add and not have that categorization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your rationale. When allowing for a more flexible set of package references, the wizard code must then generate a <PackageReference> string for each and the number of them can't be predicted ahead of time. That means the template would have a single substitution token into which all of the package reference strings must go.

I've spent all afternoon on this and keep running in to the same problem.
Template substitution works fine when the substitution variable represents a single xml element to be injected into the csproj (or a subsection of an element's textual representation).

When there are multiple items to be injected, each has to have its own substitution variable. Any attempt to include more than one line or more than one xml element causes an error.

I've attempted every way I can think of to join multiple together, but MSBuild keeps complaining that there is invalid xml (specifically 'The element <#text> beneath element <ItemGroup> is unrecognized'). I've joined the package reference strings with Environment.NewLine, with a hard-coded '\n' and with no separation at all. All result in the same error.

Have you seen this problem before? (I'm struggling to find reliable docs and information on what is/is-not allowed)

Copy link
Copy Markdown

@Code-Grump Code-Grump Jun 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like the system is rejecting XML fragments. Could you add a complete <ItemGroup> with all its descendants?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is rejecting multi-line substitution variable values.
I tried the suggestion of substituting the entire <ItemGroup> element, but ended up with the same error.

"name": "nunit",
"version": "3.14.0"
},
"Adapter_Lib": {
"name": "NUnit3TestAdapter",
"version": "4.5.0"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public class AddNewReqnrollProjectViewModel : INotifyPropertyChanged
private const string MsTest = "MsTest";
private const string Net8 = "net8.0";

public AddNewReqnrollProjectViewModel() { }
public AddNewReqnrollProjectViewModel(IEnumerable<string> testFrameworkNames)
{
TestFrameworks = new (testFrameworkNames);
}
#if DEBUG
public static AddNewReqnrollProjectViewModel DesignData = new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace Reqnroll.VisualStudio.Wizards.Infrastructure;

public interface INewProjectMetaDataProvider
{
IEnumerable<string> TestFrameworks { get; }
IDictionary<string, NugetPackageDescriptor> DependenciesOf(string testFramework);
}
public record NugetPackageDescriptor(string name, string version);
public record TestFrameworkInfoModel(string description, Dictionary<string, NugetPackageDescriptor> dependencies);

[Export(typeof(INewProjectMetaDataProvider))]
public class NewProjectMetaDataProvider : INewProjectMetaDataProvider
{
private Dictionary<string, TestFrameworkInfoModel> _testFrameworkDescriptors = new();
private Task<Dictionary<string, TestFrameworkInfoModel>> _httpTask;
private Lazy<Dictionary<string, TestFrameworkInfoModel>> _resolvedTestFrameworkDescriptors;

[ImportingConstructor]
public NewProjectMetaDataProvider()
{
// read static metadata from a resource file, deserialize the resulting json, storing it in the _testFrameworkDescriptors field
var resourceName = "Reqnroll.VisualStudio.Resources.TestFrameworkDescriptors.json";
using (var stream = typeof(NewProjectMetaDataProvider).Assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
var json = reader.ReadToEnd();
var data = JsonSerialization.DeserializeObject<Dictionary<string, TestFrameworkInfoModel>>(json);
if (data != null)
_testFrameworkDescriptors = data;
}

// launch a task to read metadata from http resource and deserialize the resulting json, save the Task to a field
_httpTask = Task.Run(async () =>

Check warning on line 33 in Reqnroll.VisualStudio/Wizards/Infrastructure/NewProjectMetaDataProvider.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in value of type 'Task<Dictionary<string, TestFrameworkInfoModel>?>' doesn't match target type 'Task<Dictionary<string, TestFrameworkInfoModel>>'.
{
try
{
using (var httpClient = new System.Net.Http.HttpClient())
{
var httpJson = await httpClient.GetStringAsync("https://example.com/testframeworks.json").ConfigureAwait(false);
var httpData = JsonSerialization.DeserializeObject<Dictionary<string, TestFrameworkInfoModel>>(httpJson);
return httpData;
}
}
catch
{
return null;
}
});

_resolvedTestFrameworkDescriptors = new Lazy<Dictionary<string, TestFrameworkInfoModel>>(FinishRetrievalOfTestFrameworkDescriptors);
}

public IEnumerable<string> TestFrameworks
{
get
{
var descriptors = _resolvedTestFrameworkDescriptors.Value;
return descriptors.Keys;
}
}

public IDictionary<string, NugetPackageDescriptor> DependenciesOf(string testFramework)
{
var descriptors = _resolvedTestFrameworkDescriptors.Value;
return descriptors[testFramework].dependencies;
}

private Dictionary<string, TestFrameworkInfoModel> FinishRetrievalOfTestFrameworkDescriptors()
{
// Wait for the HTTP task to complete
if (_httpTask == null)
return _testFrameworkDescriptors;

try
{
var result = _httpTask.GetAwaiter().GetResult();
if (result != null)
{
_testFrameworkDescriptors = result;
}
}
catch
{
// Ignore exceptions, keep existing _testFrameworkDescriptors
}
return _testFrameworkDescriptors;
}
}
19 changes: 16 additions & 3 deletions Reqnroll.VisualStudio/Wizards/ReqnrollProjectWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,41 @@ public class ReqnrollProjectWizard : IDeveroomWizard
{
private readonly IDeveroomWindowManager _deveroomWindowManager;
private readonly IMonitoringService _monitoringService;
private readonly INewProjectMetaDataProvider _newProjectMetaDataProvider;

[ImportingConstructor]
public ReqnrollProjectWizard(IDeveroomWindowManager deveroomWindowManager, IMonitoringService monitoringService)
public ReqnrollProjectWizard(IDeveroomWindowManager deveroomWindowManager, IMonitoringService monitoringService, INewProjectMetaDataProvider newProjectMetaDataProvider)
{
_deveroomWindowManager = deveroomWindowManager;
_monitoringService = monitoringService;
_newProjectMetaDataProvider = newProjectMetaDataProvider;
}

public bool RunStarted(WizardRunParameters wizardRunParameters)
{
_monitoringService.MonitorProjectTemplateWizardStarted();

var viewModel = new AddNewReqnrollProjectViewModel();
var frameworkNames = _newProjectMetaDataProvider.TestFrameworks;

var viewModel = new AddNewReqnrollProjectViewModel(frameworkNames);
var dialogResult = _deveroomWindowManager.ShowDialog(viewModel);
if (!dialogResult.HasValue || !dialogResult.Value) return false;

_monitoringService.MonitorProjectTemplateWizardCompleted(viewModel.DotNetFramework, viewModel.UnitTestFramework,
viewModel.FluentAssertionsIncluded);

var dependencies = _newProjectMetaDataProvider.DependenciesOf(viewModel.UnitTestFramework);
var keys = new List<string>() { "Reqnroll_Lib", "TestFramework_Lib", "Adapter_Lib" };
// Add custom parameters.
foreach(string k in keys)
{
var package = dependencies[k];
var name = package.name;
var version = package.version;
wizardRunParameters.ReplacementsDictionary.Add($"${k}$",$"<PackageReference Include=\"{name}\" Version=\"{version}\" />");
}

wizardRunParameters.ReplacementsDictionary.Add("$dotnetframework$", viewModel.DotNetFramework);
wizardRunParameters.ReplacementsDictionary.Add("$unittestframework$", viewModel.UnitTestFramework);
wizardRunParameters.ReplacementsDictionary.Add("$fluentassertionsincluded$",
viewModel.FluentAssertionsIncluded.ToString(CultureInfo.InvariantCulture));

Expand Down
Loading