Skip to content

Commit

Permalink
Updated tests to make sure that tests related to configuring default …
Browse files Browse the repository at this point in the history
…options doesn't cause side effects on other tests
  • Loading branch information
mikoskinen committed Dec 30, 2020
1 parent 39a45a2 commit 5aef538
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Xunit;

namespace PluginFramework.Catalogs.NuGet.Tests
{
[CollectionDefinition(nameof(NotThreadSafeResourceCollection), DisableParallelization = true)]
public class NotThreadSafeResourceCollection { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,41 @@ public async Task CanConfigureNamingOptions()
// Assert
Assert.EndsWith("Modified", plugin.Name);
}

[Fact]
public async Task CanConfigureDefaultNamingOptions()
[Collection(nameof(NotThreadSafeResourceCollection))]
public class DefaultOptions : IDisposable
{
NugetFeedPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions()
public DefaultOptions()
{
PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified"
};

// Arrange
var feed = new NuGetFeed("nuget.org", "https://api.nuget.org/v3/index.json");
var catalog = new NugetFeedPluginCatalog(feed, searchTerm: "tags:mocking", maxPackages: 1, configureFinder: configure =>
NugetFeedPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions()
{
PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified"
};
}

[Fact]
public async Task CanConfigureDefaultNamingOptions()
{
configure.HasName("Moq.Range");
});

// Act
await catalog.Initialize();
var plugin = catalog.Single();

// Assert
Assert.EndsWith("Modified", plugin.Name);
// Arrange
var feed = new NuGetFeed("nuget.org", "https://api.nuget.org/v3/index.json");
var catalog = new NugetFeedPluginCatalog(feed, searchTerm: "tags:mocking", maxPackages: 1, configureFinder: configure =>
{
configure.HasName("Moq.Range");
});

// Act
await catalog.Initialize();
var plugin = catalog.Single();

// Assert
Assert.EndsWith("Modified", plugin.Name);
}

public void Dispose()
{
NugetFeedPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,6 @@ public async Task CanConfigureNamingOptions()
Assert.EndsWith("Modified", plugin.Name);
}

[Fact]
public async Task CanConfigureDefaultNamingOptions()
{
NugetPluginCatalogOptions.Defaults.PluginNameOptions =
new PluginNameOptions() { PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified" };

// Arrange
var catalog = new NugetPackagePluginCatalog("Serilog", "2.9.0", configureFinder: configure =>
{
configure.HasName("Serilog.Core.Logger");
});

// Act
await catalog.Initialize();
var plugin = catalog.Single();

// Assert
Assert.EndsWith("Modified", plugin.Name);
}

[Fact]
public async Task InstallPackageWithDepencencies()
{
Expand Down Expand Up @@ -207,5 +187,37 @@ public async Task InstallPackageFromFeedUsingCustomNuGetConfig()
// Assert
Assert.NotEmpty(plugins);
}

[Collection(nameof(NotThreadSafeResourceCollection))]
public class DefaultOptions : IDisposable
{
public DefaultOptions()
{
NugetPluginCatalogOptions.Defaults.PluginNameOptions =
new PluginNameOptions() { PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified" };
}

[Fact]
public async Task CanConfigureDefaultNamingOptions()
{
// Arrange
var catalog = new NugetPackagePluginCatalog("Serilog", "2.9.0", configureFinder: configure =>
{
configure.HasName("Serilog.Core.Logger");
});

// Act
await catalog.Initialize();
var plugin = catalog.Single();

// Assert
Assert.EndsWith("Modified", plugin.Name);
}

public void Dispose()
{
NugetPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions();
}
}
}
}
162 changes: 81 additions & 81 deletions tests/unit/Weikio.PluginFramework.Tests/AssemblyPluginCatalogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,32 @@ public async Task CanInitialize()

Assert.NotEmpty(allPlugins);
}

[Fact]
public async Task CanInitializeWithCriteria()
{
var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", configure =>
{
configure.HasName("*Plugin");
});

await catalog.Initialize();

var allPlugins = catalog.GetPlugins();

Assert.Single(allPlugins);
}

[Fact]
public async Task CanConfigureNamingOptions()
{
var options = new AssemblyPluginCatalogOptions()
{
PluginNameOptions = new PluginNameOptions()
{
PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified"
}
PluginNameOptions = new PluginNameOptions() { PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified" }
};

var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", options);

await catalog.Initialize();

var allPlugins = catalog.GetPlugins();
var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", options);

foreach (var plugin in allPlugins)
{
Assert.EndsWith("Modified", plugin.Name);
}
}

[Fact]
public async Task CanConfigureDefaultNamingOptions()
{
AssemblyPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions()
{
PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified"
};

var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll");

await catalog.Initialize();

var allPlugins = catalog.GetPlugins();
Expand All @@ -89,44 +66,7 @@ public async Task CanConfigureDefaultNamingOptions()
Assert.EndsWith("Modified", plugin.Name);
}
}

[Fact]
public async Task CanOverrideDefaultNamingOptions()
{
AssemblyPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions()
{
PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified"
};

var options = new AssemblyPluginCatalogOptions()
{
PluginNameOptions = new PluginNameOptions()
{
PluginNameGenerator = (nameOptions, type) => type.FullName + "Overridden"
}
};

var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll");
var catalog2 = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly2.dll", options);

await catalog.Initialize();
await catalog2.Initialize();

var catalog1Plugins = catalog.GetPlugins();

foreach (var plugin in catalog1Plugins)
{
Assert.EndsWith("Modified", plugin.Name);
}

var catalog2Plugins = catalog2.GetPlugins();

foreach (var plugin in catalog2Plugins)
{
Assert.EndsWith("Overridden", plugin.Name);
}
}

[Fact]
public async Task ByDefaultOnlyContainsPublicNonAbstractClasses()
{
Expand All @@ -139,15 +79,15 @@ public async Task ByDefaultOnlyContainsPublicNonAbstractClasses()
Assert.False(plugin.Type.IsAbstract);
Assert.False(plugin.Type.IsInterface);
}

[Fact]
public async Task CanIncludeAbstractClassesUsingCriteria()
{
var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", builder =>
{
builder.IsAbstract(true);
});

await catalog.Initialize();

var allPlugins = catalog.GetPlugins();
Expand Down Expand Up @@ -224,14 +164,13 @@ public async Task CanUseHostsDependencies()

var newPlugin = folder1Catalog.Single();
var oldPlugin = folder2Catalog.Single();



dynamic newPluginJsonResolver = Activator.CreateInstance(newPlugin);
var newPluginVersion = newPluginJsonResolver.GetVersion();

dynamic oldPluginJsonResolver = Activator.CreateInstance(oldPlugin);
var oldPluginVersion = oldPluginJsonResolver.GetVersion();

Assert.Equal("12.0.0.0", newPluginVersion);
Assert.Equal("12.0.0.0", oldPluginVersion);
}
Expand All @@ -241,30 +180,91 @@ public async Task CanUseSelectedHoststDependencies()
{
// Make sure that the referenced version of JSON.NET is loaded into memory
var json = Newtonsoft.Json.JsonConvert.SerializeObject(1);

// Make sure that the referenced version of Microsoft.Extensions.Logging is loaded into memory
var logging = new Microsoft.Extensions.Logging.LoggerFactory();

var options = new AssemblyPluginCatalogOptions();;

var options = new AssemblyPluginCatalogOptions();
;

options.PluginLoadContextOptions = new PluginLoadContextOptions()
{
UseHostApplicationAssemblies = UseHostApplicationAssembliesEnum.Selected,
HostApplicationAssemblies = new List<AssemblyName>()
{
typeof(Microsoft.Extensions.Logging.LoggerFactory).Assembly.GetName()
}
HostApplicationAssemblies = new List<AssemblyName>() { typeof(Microsoft.Extensions.Logging.LoggerFactory).Assembly.GetName() }
};

var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\JsonOld\netstandard2.0\JsonNetOld.dll", options);
await catalog.Initialize();

var oldPlugin = catalog.Single();

dynamic oldPluginJsonResolver = Activator.CreateInstance(oldPlugin);
var oldPluginVersion = oldPluginJsonResolver.GetVersion();
var loggerVersion = oldPluginJsonResolver.GetLoggingVersion();

Assert.Equal("3.1.2.0", loggerVersion);
Assert.Equal("9.0.0.0", oldPluginVersion);
}

[Collection(nameof(NotThreadSafeResourceCollection))]
public class DefaultOptions : IDisposable
{
public DefaultOptions()
{
AssemblyPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions()
{
PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified"
};
}

[Fact]
public async Task CanConfigureDefaultNamingOptions()
{
var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll");

await catalog.Initialize();

var allPlugins = catalog.GetPlugins();

foreach (var plugin in allPlugins)
{
Assert.EndsWith("Modified", plugin.Name);
}
}

[Fact]
public async Task CanOverrideDefaultNamingOptions()
{
var options = new AssemblyPluginCatalogOptions()
{
PluginNameOptions = new PluginNameOptions() { PluginNameGenerator = (nameOptions, type) => type.FullName + "Overridden" }
};

var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll");
var catalog2 = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly2.dll", options);

await catalog.Initialize();
await catalog2.Initialize();

var catalog1Plugins = catalog.GetPlugins();

foreach (var plugin in catalog1Plugins)
{
Assert.EndsWith("Modified", plugin.Name);
}

var catalog2Plugins = catalog2.GetPlugins();

foreach (var plugin in catalog2Plugins)
{
Assert.EndsWith("Overridden", plugin.Name);
}
}

public void Dispose()
{
AssemblyPluginCatalogOptions.Defaults.PluginNameOptions = new PluginNameOptions();
}
}
}
}
Loading

0 comments on commit 5aef538

Please sign in to comment.