generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13351c0
commit c471bce
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../tests/Bitwarden.Server.Sdk.IntegrationTests/Bitwarden.Server.Sdk.IntegrationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.Build" Version="17.12.6" /> | ||
<PackageReference Include="Microsoft.Build.Framework" Version="17.12.6" /> | ||
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.12.6" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="MSBuild.ProjectCreation" Version="14.0.5" /> | ||
<PackageReference Include="xunit.v3" Version="1.0.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\src\sdk\**\*" | ||
Link="Sdk\%(RecursiveDir)%(Filename)%(Extension)" | ||
CopyToOutputDirectory="PreserveNewest" /> | ||
|
||
<None Include="..\..\src\content\**\*" | ||
Link="Content\%(RecursiveDir)%(Filename)%(Extension)" | ||
CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
|
||
</Project> |
58 changes: 58 additions & 0 deletions
58
...n.Server.Sdk/tests/Bitwarden.Server.Sdk.IntegrationTests/CustomProjectCreatorTemplates.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Microsoft.Build.Utilities.ProjectCreation; | ||
|
||
namespace Bitwarden.Server.Sdk.IntegrationTests; | ||
|
||
public static class CustomProjectCreatorTemplates | ||
{ | ||
private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(typeof(CustomProjectCreatorTemplates).Assembly.Location)!; | ||
|
||
public static ProjectCreator SdkProject(this ProjectCreatorTemplates templates, | ||
string? additional = null, | ||
Action<ProjectCreator>? customAction = null) | ||
{ | ||
var dir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
|
||
Directory.CreateDirectory(dir); | ||
|
||
File.WriteAllText(Path.Combine(dir, "Program.cs"), $""" | ||
var builder = WebApplication.CreateBuilder(args); | ||
builder.UseBitwardenSdk(); | ||
var app = builder.Build(); | ||
{additional} | ||
app.Run(); | ||
""" | ||
); | ||
|
||
return ProjectCreator.Templates.SdkCsproj( | ||
path: Path.Combine(dir, "Test.csproj"), | ||
sdk: "Microsoft.NET.Sdk.Web", | ||
targetFramework: "net9.0") | ||
.Import(Path.Combine(ThisAssemblyDirectory, "sdk", "Sdk.props")) | ||
.CustomAction(customAction) | ||
.Import(Path.Combine(ThisAssemblyDirectory, "sdk", "Sdk.targets")); | ||
} | ||
|
||
public static ProjectCreator TryGetConstant(this ProjectCreator project, string constant, out bool result) | ||
{ | ||
result = false; | ||
project = project.TryGetPropertyValue("DefineConstants", out var constants); | ||
|
||
if (string.IsNullOrEmpty(constants)) | ||
{ | ||
return project; | ||
} | ||
|
||
var allConstants = constants.Split(';'); | ||
|
||
if (!allConstants.Contains(constant)) | ||
{ | ||
return project; | ||
} | ||
|
||
result = true; | ||
return project; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
extensions/Bitwarden.Server.Sdk/tests/Bitwarden.Server.Sdk.IntegrationTests/SdkTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using Microsoft.Build.Utilities.ProjectCreation; | ||
|
||
namespace Bitwarden.Server.Sdk.IntegrationTests; | ||
|
||
public class SdkTests : MSBuildTestBase | ||
{ | ||
[Fact] | ||
public void NoOverridingProperties_CanCompile() | ||
{ | ||
ProjectCreator.Templates.SdkProject() | ||
.TryBuild(restore: true, out var result, out var buildOutput) | ||
.TryGetConstant("BIT_INCLUDE_LOGGING", out var hasLoggingConstant) | ||
.TryGetConstant("BIT_INCLUDE_TELEMETRY", out var hasTelementryConstant) | ||
.TryGetConstant("BIT_INCLUDE_FEATURES", out var hasFeaturesConstant); | ||
|
||
Assert.True(result, buildOutput.GetConsoleLog()); | ||
|
||
Assert.True(hasLoggingConstant); | ||
Assert.True(hasTelementryConstant); | ||
Assert.True(hasFeaturesConstant); | ||
} | ||
|
||
[Fact] | ||
public void LoggingTurnedOff_CanCompile() | ||
{ | ||
ProjectCreator.Templates.SdkProject( | ||
customAction: (project) => | ||
{ | ||
project.Property("BitIncludeLogging", bool.FalseString); | ||
} | ||
) | ||
.TryBuild(restore: true, out var result, out var buildOutput); | ||
|
||
Assert.True(result, buildOutput.GetConsoleLog()); | ||
} | ||
|
||
[Fact] | ||
public void TelemetryTurnedOff_CanCompile() | ||
{ | ||
ProjectCreator.Templates.SdkProject( | ||
customAction: (project) => | ||
{ | ||
project.Property("BitIncludeTelemetry", bool.FalseString); | ||
} | ||
) | ||
.TryBuild(restore: true, out var result, out var buildOutput); | ||
|
||
Assert.True(result, buildOutput.GetConsoleLog()); | ||
} | ||
|
||
[Fact] | ||
public void FeaturesTurnedOff_CanCompile() | ||
{ | ||
ProjectCreator.Templates.SdkProject( | ||
customAction: (project) => | ||
{ | ||
project.Property("BitIncludeFeatures", bool.FalseString); | ||
} | ||
) | ||
.TryBuild(restore: true, out var result, out var buildOutput); | ||
|
||
Assert.True(result, buildOutput.GetConsoleLog()); | ||
} | ||
|
||
[Fact] | ||
public void FeaturesTurnedOff_CanNotUseFeatureService() | ||
{ | ||
ProjectCreator.Templates.SdkProject( | ||
customAction: (project) => | ||
{ | ||
project.Property("BitIncludeFeatures", bool.FalseString); | ||
}, | ||
additional: """ | ||
app.MapGet("/test", (Bitwarden.Server.Sdk.Features.IFeatureService featureService) => featureService.GetAll()); | ||
""" | ||
) | ||
.TryBuild(restore: true, out var result, out var buildOutput); | ||
|
||
Assert.False(result, buildOutput.GetConsoleLog()); | ||
|
||
// error CS0234: The type or namespace name 'Features' does not exist in the namespace 'Bitwarden.Server.Sdk' (are you missing an assembly reference?) | ||
Assert.Contains(buildOutput.ErrorEvents, e => e.Code == "CS0234"); | ||
} | ||
|
||
public static TheoryData<bool, bool, bool> MatrixData | ||
=> new MatrixTheoryData<bool, bool, bool>([true, false], [true, false], [true, false]); | ||
|
||
// There will be some variants that disallow the use of feature Y if feature X is not also enabled. | ||
// Use this set to exclude those known variants from being tested. | ||
public static HashSet<(bool, bool, bool)> ExcludedVariants => []; | ||
|
||
[Theory, MemberData(nameof(MatrixData))] | ||
public void AllVariants_Work(bool includeLogging, bool includeTelemetry, bool includeFeatures) | ||
{ | ||
if (ExcludedVariants.Contains((includeLogging, includeTelemetry, includeFeatures))) | ||
{ | ||
Assert.Skip($""" | ||
Excluded Variant Skipped: | ||
IncludeLogging = {includeLogging} | ||
IncludeTelemetry = {includeTelemetry} | ||
IncludeFeatures = {includeFeatures} | ||
"""); | ||
} | ||
|
||
ProjectCreator.Templates.SdkProject( | ||
customAction: (project) => | ||
{ | ||
project.Property("BitIncludeLogging", includeLogging.ToString()); | ||
project.Property("BitIncludeTelemetry", includeTelemetry.ToString()); | ||
project.Property("BitIncludeFeatures", includeFeatures.ToString()); | ||
} | ||
) | ||
.TryBuild(restore: true, out var result, out var buildOutput); | ||
|
||
Assert.True(result, buildOutput.GetConsoleLog()); | ||
} | ||
} |