Skip to content

Commit 9af237e

Browse files
committed
Make testapp use packagereference
1 parent 549fa75 commit 9af237e

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

test/Altinn.App.Analyzers.Tests/Fixtures/AltinnTestAppFixture.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
using System.Diagnostics.CodeAnalysis;
44
using System.Text;
55
using Buildalyzer;
6+
using Buildalyzer.Environment;
67
using Buildalyzer.Workspaces;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.Diagnostics;
910
using Microsoft.CodeAnalysis.Text;
11+
using Microsoft.Extensions.Logging;
1012
using Xunit.Abstractions;
1113

1214
namespace Altinn.App.Analyzers.Tests.Fixtures;
@@ -43,8 +45,18 @@ internal void Initialize()
4345
_projectDir = Path.Combine(Directory.GetCurrentDirectory(), "testapp", "App");
4446
Assert.True(Directory.Exists(_projectDir));
4547
var manager = new AnalyzerManager();
48+
49+
var logger = manager.LoggerFactory?.CreateLogger<AdhocWorkspace>();
50+
_workspace = new AdhocWorkspace();
51+
_workspace.WorkspaceChanged += (sender, args) =>
52+
logger?.LogDebug("Workspace changed: {Kind}{NewLine}", args.Kind, Environment.NewLine);
53+
_workspace.WorkspaceFailed += (sender, args) =>
54+
logger?.LogError("Workspace failed: {Diagnostic}{NewLine}", args.Diagnostic, Environment.NewLine);
55+
4656
var analyzer = manager.GetProject(Path.Combine(_projectDir, "App.csproj"));
47-
_workspace = analyzer.GetWorkspace();
57+
var options = new EnvironmentOptions() { DesignTime = false, TargetsToBuild = { "BeforeTests", "Build" } };
58+
var results = analyzer.Build(options);
59+
results.First().AddToWorkspace(_workspace);
4860
Assert.True(_workspace.CanApplyChange(ApplyChangesKind.AddDocument));
4961
Assert.True(_workspace.CanApplyChange(ApplyChangesKind.RemoveDocument));
5062
Assert.True(_workspace.CanApplyChange(ApplyChangesKind.ChangeDocument));
@@ -102,7 +114,6 @@ public IDisposable WithInvalidHttpContextAccessorUse()
102114

103115
public async Task<(CompilationWithAnalyzers Compilation, IReadOnlyList<Diagnostic>)> GetCompilation(
104116
DiagnosticAnalyzer analyzer,
105-
bool includeAdditionalFiles,
106117
CancellationToken cancellationToken
107118
)
108119
{
@@ -116,12 +127,8 @@ CancellationToken cancellationToken
116127
["build_property.projectdir"] = _projectDir,
117128
}.ToImmutableDictionary();
118129

119-
var additionalFiles = ImmutableArray.CreateRange<AdditionalText>(
120-
[new TestAdditionalText(Content.ApplicationMetadata), new TestAdditionalText(Content.LayoutSets)]
121-
);
122-
123130
var analyzerOptions = new AnalyzerOptions(
124-
includeAdditionalFiles ? additionalFiles : [],
131+
[],
125132
new TestOptionsProvider(
126133
ImmutableDictionary<object, AnalyzerConfigOptions>.Empty,
127134
new TestAnalyzerConfigOptions(globalOptions)

test/Altinn.App.Analyzers.Tests/HttpContextAccessorUsageAnalyzerTests.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ public async Task Builds_OK_By_Default()
2323

2424
var analyzer = new HttpContextAccessorUsageAnalyzer();
2525

26-
var (compilation, diagnostics) = await _fixture.GetCompilation(
27-
analyzer,
28-
includeAdditionalFiles: false,
29-
cancellationToken
30-
);
26+
var (compilation, diagnostics) = await _fixture.GetCompilation(analyzer, cancellationToken);
3127

3228
Assert.Empty(diagnostics);
3329
}
@@ -41,11 +37,7 @@ public async Task Emits_Diagnostic()
4137
using var modification = _fixture.WithInvalidHttpContextAccessorUse();
4238
var analyzer = new HttpContextAccessorUsageAnalyzer();
4339

44-
var (compilation, diagnostics) = await _fixture.GetCompilation(
45-
analyzer,
46-
includeAdditionalFiles: false,
47-
cancellationToken
48-
);
40+
var (compilation, diagnostics) = await _fixture.GetCompilation(analyzer, cancellationToken);
4941

5042
Assert.Contains(diagnostics, d => Diagnostics.CodeSmells.HttpContextAccessorUsage.Id == d.Id);
5143
await Verify(diagnostics);

test/Altinn.App.Analyzers.Tests/testapp/App/App.csproj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,36 @@
55
<RootNamespace>Altinn.App</RootNamespace>
66

77
<!-- This let's us test with local NuGet packages, just put the .nupkg in the ../packages directory -->
8-
<RestoreSources>../packages;https://api.nuget.org/v3/index.json</RestoreSources>
8+
<LocalPackagesPath>../packages</LocalPackagesPath>
99
</PropertyGroup>
1010

1111
<ItemGroup>
1212
<!-- Test files that are added in the Roslyn workspace as needed during tests -->
1313
<Compile Remove="additional/*.cs" />
1414
</ItemGroup>
1515

16+
<Target Name="BeforeTests">
17+
<!-- This target is run through Roslyn/Buildalyzer in the fixture of the analyzer tests -->
18+
<RemoveDir Directories="$(LocalPackagesPath)" />
19+
<MakeDir Directories="$(LocalPackagesPath)" />
20+
<Exec Command="dotnet pack --no-build --no-restore --configuration $(Configuration) --output $(LocalPackagesPath) ../../../../src/Altinn.App.Core/Altinn.App.Core.csproj" />
21+
<Exec Command="dotnet pack --no-build --no-restore --configuration $(Configuration) --output $(LocalPackagesPath) ../../../../src/Altinn.App.Api/Altinn.App.Api.csproj" />
22+
</Target>
23+
1624
<Target Name="Output" AfterTargets="Build">
1725
<Message Importance="High" Text="IsAltinnApp = $(IsAltinnApp)" />
1826
</Target>
1927

2028
<ItemGroup>
21-
<!-- <PackageReference Include="Altinn.App.Api" Version="8.6.0-preview.4.56">
29+
<PackageReference Include="Altinn.App.Api" Version="8.*-*">
2230
<CopyToOutputDirectory>lib\$(TargetFramework)\*.xml</CopyToOutputDirectory>
2331
</PackageReference>
24-
<PackageReference Include="Altinn.App.Core" Version="8.6.0-preview.4.56" /> -->
32+
<PackageReference Include="Altinn.App.Core" Version="8.*-*" />
2533

26-
<ProjectReference Include="../../../../src/Altinn.App.Core/Altinn.App.Core.csproj" />
34+
<!-- <ProjectReference Include="../../../../src/Altinn.App.Core/Altinn.App.Core.csproj" />
2735
<ProjectReference Include="../../../../src/Altinn.App.Api/Altinn.App.Api.csproj">
2836
<CopyToOutputDirectory>lib\$(TargetFramework)\*.xml</CopyToOutputDirectory>
29-
</ProjectReference>
37+
</ProjectReference> -->
3038
<ProjectReference Include="../../../../src/Altinn.App.Analyzers/Altinn.App.Analyzers.csproj" PrivateAssets="all" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
3139
</ItemGroup>
3240
<ItemGroup>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<clear />
5+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
6+
<add key="local" value="./packages" />
7+
</packageSources>
8+
9+
<packageSourceMapping>
10+
<packageSource key="nuget.org">
11+
<package pattern="*" />
12+
</packageSource>
13+
<packageSource key="local">
14+
<package pattern="Altinn.App.Api" />
15+
<package pattern="Altinn.App.Core" />
16+
</packageSource>
17+
</packageSourceMapping>
18+
</configuration>

0 commit comments

Comments
 (0)