|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using DevProxy.Abstractions.Proxy; |
| 6 | +using DevProxy.Abstractions.Utils; |
| 7 | +using DevProxy.Plugins.Reporters; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.Extensions.Logging.Abstractions; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace DevProxy.Integration.Tests; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Verifies that reporters honour <see cref="IReportsProxyConfiguration.ReportsFolder"/> |
| 17 | +/// and fall back to the current working directory when it isn't set. |
| 18 | +/// </summary> |
| 19 | +public sealed class ReporterReportsFolderTests |
| 20 | +{ |
| 21 | + private static RecordingArgs Recording() => |
| 22 | + new([]) |
| 23 | + { |
| 24 | + GlobalData = new() |
| 25 | + { |
| 26 | + [ProxyUtils.ReportsKey] = new Dictionary<string, object> |
| 27 | + { |
| 28 | + ["TestPlugin"] = new { Value = 1 } |
| 29 | + } |
| 30 | + }, |
| 31 | + }; |
| 32 | + |
| 33 | + private static JsonReporter CreateReporter(string? reportsFolder) => |
| 34 | + new(NullLogger<JsonReporter>.Instance, new HashSet<UrlToWatch>(), new TestProxyConfiguration { ReportsFolder = reportsFolder }); |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task WritesReportToReportsFolder_CreatingItWhenMissing() |
| 38 | + { |
| 39 | + var root = Directory.CreateTempSubdirectory("devproxy-reports-"); |
| 40 | + var cwd = Directory.CreateTempSubdirectory("devproxy-cwd-"); |
| 41 | + var originalCwd = Directory.GetCurrentDirectory(); |
| 42 | + Directory.SetCurrentDirectory(cwd.FullName); |
| 43 | + try |
| 44 | + { |
| 45 | + var reportsFolder = Path.Combine(root.FullName, "nested", "reports"); |
| 46 | + var reporter = CreateReporter(reportsFolder); |
| 47 | + |
| 48 | + await reporter.AfterRecordingStopAsync(Recording(), CancellationToken.None); |
| 49 | + |
| 50 | + Assert.True(File.Exists(Path.Combine(reportsFolder, "TestPlugin_JsonReporter.json"))); |
| 51 | + Assert.Empty(Directory.GetFiles(cwd.FullName)); |
| 52 | + } |
| 53 | + finally |
| 54 | + { |
| 55 | + Directory.SetCurrentDirectory(originalCwd); |
| 56 | + root.Delete(true); |
| 57 | + cwd.Delete(true); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public async Task PluginActivation_UsesConfigurationAwareConstructor() |
| 63 | + { |
| 64 | + var root = Directory.CreateTempSubdirectory("devproxy-reports-"); |
| 65 | + try |
| 66 | + { |
| 67 | + using var serviceProvider = new ServiceCollection() |
| 68 | + .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) |
| 69 | + .AddSingleton(typeof(ILogger<>), typeof(Logger<>)) |
| 70 | + .AddSingleton<IProxyConfiguration>(new TestProxyConfiguration { ReportsFolder = root.FullName }) |
| 71 | + .BuildServiceProvider(); |
| 72 | + var reporter = ActivatorUtilities.CreateInstance<JsonReporter>(serviceProvider, new HashSet<UrlToWatch>()); |
| 73 | + |
| 74 | + await reporter.AfterRecordingStopAsync(Recording(), CancellationToken.None); |
| 75 | + |
| 76 | + Assert.True(File.Exists(Path.Combine(root.FullName, "TestPlugin_JsonReporter.json"))); |
| 77 | + } |
| 78 | + finally |
| 79 | + { |
| 80 | + root.Delete(true); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task AppendsTimestamp_WhenReportExistsInReportsFolder() |
| 86 | + { |
| 87 | + var root = Directory.CreateTempSubdirectory("devproxy-reports-"); |
| 88 | + try |
| 89 | + { |
| 90 | + await File.WriteAllTextAsync(Path.Combine(root.FullName, "TestPlugin_JsonReporter.json"), "{}"); |
| 91 | + var reporter = CreateReporter(root.FullName); |
| 92 | + |
| 93 | + await reporter.AfterRecordingStopAsync(Recording(), CancellationToken.None); |
| 94 | + |
| 95 | + Assert.Equal(2, Directory.GetFiles(root.FullName, "TestPlugin_JsonReporter*.json").Length); |
| 96 | + } |
| 97 | + finally |
| 98 | + { |
| 99 | + root.Delete(true); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Theory] |
| 104 | + [InlineData(null)] |
| 105 | + [InlineData("")] |
| 106 | + public async Task WritesReportToWorkingDirectory_WhenReportsFolderNotSet(string? reportsFolder) |
| 107 | + { |
| 108 | + var cwd = Directory.CreateTempSubdirectory("devproxy-cwd-"); |
| 109 | + var originalCwd = Directory.GetCurrentDirectory(); |
| 110 | + Directory.SetCurrentDirectory(cwd.FullName); |
| 111 | + try |
| 112 | + { |
| 113 | + var reporter = CreateReporter(reportsFolder); |
| 114 | + |
| 115 | + await reporter.AfterRecordingStopAsync(Recording(), CancellationToken.None); |
| 116 | + |
| 117 | + Assert.True(File.Exists(Path.Combine(cwd.FullName, "TestPlugin_JsonReporter.json"))); |
| 118 | + } |
| 119 | + finally |
| 120 | + { |
| 121 | + Directory.SetCurrentDirectory(originalCwd); |
| 122 | + cwd.Delete(true); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments