|
| 1 | +// ProgramRunnerTests.FailOnDiff.cs — --fail-on-diff CLI gating integration tests |
| 2 | +// ProgramRunnerTests.FailOnDiff.cs — --fail-on-diff CLI ゲートの統合テスト |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using FolderDiffIL4DotNet.Services; |
| 9 | +using FolderDiffIL4DotNet.Tests.Helpers; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace FolderDiffIL4DotNet.Tests |
| 13 | +{ |
| 14 | + public sealed partial class ProgramRunnerTests |
| 15 | + { |
| 16 | + [Fact] |
| 17 | + public async Task RunAsync_ReportableDifferencesWithoutFailOnDiff_ReturnsSuccess() |
| 18 | + { |
| 19 | + string tempRoot = CreateFailOnDiffTempRoot(); |
| 20 | + try |
| 21 | + { |
| 22 | + string oldDir = CreateDirectory(tempRoot, "old"); |
| 23 | + string newDir = CreateDirectory(tempRoot, "new"); |
| 24 | + File.WriteAllText(Path.Combine(newDir, "added.txt"), "added"); |
| 25 | + |
| 26 | + await WithConfigFileAsync("""{"SkipIL":true}""", async () => |
| 27 | + { |
| 28 | + var result = await RunFailOnDiffComparisonAsync(tempRoot, oldDir, newDir, includeFailOnDiff: false); |
| 29 | + |
| 30 | + Assert.Equal(0, result.ExitCode); |
| 31 | + Assert.True(File.Exists(Path.Combine(result.ReportDirectory, "diff_report.md"))); |
| 32 | + }); |
| 33 | + } |
| 34 | + finally |
| 35 | + { |
| 36 | + TryDeleteDirectory(tempRoot); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task RunAsync_FailOnDiffWithoutReportableDifferences_ReturnsSuccess() |
| 42 | + { |
| 43 | + string tempRoot = CreateFailOnDiffTempRoot(); |
| 44 | + try |
| 45 | + { |
| 46 | + string oldDir = CreateDirectory(tempRoot, "old"); |
| 47 | + string newDir = CreateDirectory(tempRoot, "new"); |
| 48 | + File.WriteAllText(Path.Combine(oldDir, "same.txt"), "same"); |
| 49 | + File.WriteAllText(Path.Combine(newDir, "same.txt"), "same"); |
| 50 | + |
| 51 | + await WithConfigFileAsync("""{"SkipIL":true}""", async () => |
| 52 | + { |
| 53 | + var result = await RunFailOnDiffComparisonAsync(tempRoot, oldDir, newDir, includeFailOnDiff: true); |
| 54 | + |
| 55 | + Assert.Equal(0, result.ExitCode); |
| 56 | + Assert.True(File.Exists(Path.Combine(result.ReportDirectory, "diff_report.md"))); |
| 57 | + }); |
| 58 | + } |
| 59 | + finally |
| 60 | + { |
| 61 | + TryDeleteDirectory(tempRoot); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task RunAsync_FailOnDiffWithFinalAddedRemovedAndModifiedEntries_ReturnsFiveAfterGeneratingArtifacts() |
| 67 | + { |
| 68 | + string tempRoot = CreateFailOnDiffTempRoot(); |
| 69 | + try |
| 70 | + { |
| 71 | + string oldDir = CreateDirectory(tempRoot, "old"); |
| 72 | + string newDir = CreateDirectory(tempRoot, "new"); |
| 73 | + File.WriteAllText(Path.Combine(oldDir, "removed.txt"), "removed"); |
| 74 | + File.WriteAllText(Path.Combine(newDir, "added.txt"), "added"); |
| 75 | + File.WriteAllText(Path.Combine(oldDir, "modified.txt"), "before"); |
| 76 | + File.WriteAllText(Path.Combine(newDir, "modified.txt"), "after"); |
| 77 | + |
| 78 | + await WithConfigFileAsync("""{"SkipIL":true}""", async () => |
| 79 | + { |
| 80 | + var result = await RunFailOnDiffComparisonAsync(tempRoot, oldDir, newDir, includeFailOnDiff: true); |
| 81 | + |
| 82 | + Assert.Equal(5, result.ExitCode); |
| 83 | + Assert.True(File.Exists(Path.Combine(result.ReportDirectory, "diff_report.md"))); |
| 84 | + Assert.True(File.Exists(Path.Combine(result.ReportDirectory, "diff_report.html"))); |
| 85 | + Assert.True(File.Exists(Path.Combine(result.ReportDirectory, AuditLogGenerateService.AUDIT_LOG_FILE_NAME))); |
| 86 | + }); |
| 87 | + } |
| 88 | + finally |
| 89 | + { |
| 90 | + TryDeleteDirectory(tempRoot); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task RunAsync_FailOnDiffWithOnlyIgnoredExtensionDifference_ReturnsSuccess() |
| 96 | + { |
| 97 | + string tempRoot = CreateFailOnDiffTempRoot(); |
| 98 | + try |
| 99 | + { |
| 100 | + string oldDir = CreateDirectory(tempRoot, "old"); |
| 101 | + string newDir = CreateDirectory(tempRoot, "new"); |
| 102 | + File.WriteAllText(Path.Combine(newDir, "ignored.tmp"), "ignored"); |
| 103 | + |
| 104 | + await WithConfigFileAsync("""{"SkipIL":true,"IgnoredExtensions":[".tmp"]}""", async () => |
| 105 | + { |
| 106 | + var result = await RunFailOnDiffComparisonAsync(tempRoot, oldDir, newDir, includeFailOnDiff: true); |
| 107 | + |
| 108 | + Assert.Equal(0, result.ExitCode); |
| 109 | + Assert.True(File.Exists(Path.Combine(result.ReportDirectory, "diff_report.md"))); |
| 110 | + }); |
| 111 | + } |
| 112 | + finally |
| 113 | + { |
| 114 | + TryDeleteDirectory(tempRoot); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static string CreateFailOnDiffTempRoot() |
| 119 | + => Path.Combine(Path.GetTempPath(), "fd-fail-on-diff-" + Guid.NewGuid().ToString("N")); |
| 120 | + |
| 121 | + private static string CreateDirectory(string root, string name) |
| 122 | + { |
| 123 | + string path = Path.Combine(root, name); |
| 124 | + Directory.CreateDirectory(path); |
| 125 | + return path; |
| 126 | + } |
| 127 | + |
| 128 | + private static async Task<(int ExitCode, string ReportDirectory)> RunFailOnDiffComparisonAsync( |
| 129 | + string tempRoot, |
| 130 | + string oldDir, |
| 131 | + string newDir, |
| 132 | + bool includeFailOnDiff) |
| 133 | + { |
| 134 | + string reportsRoot = CreateDirectory(tempRoot, "reports"); |
| 135 | + string reportLabel = "report_" + Guid.NewGuid().ToString("N"); |
| 136 | + var args = new List<string> |
| 137 | + { |
| 138 | + oldDir, |
| 139 | + newDir, |
| 140 | + reportLabel, |
| 141 | + "--skip-il", |
| 142 | + "--no-pause", |
| 143 | + "--no-banner", |
| 144 | + "--output", |
| 145 | + reportsRoot, |
| 146 | + }; |
| 147 | + if (includeFailOnDiff) |
| 148 | + { |
| 149 | + args.Add("--fail-on-diff"); |
| 150 | + } |
| 151 | + |
| 152 | + var runner = new ProgramRunner(new TestLogger(logFileAbsolutePath: "test.log"), new ConfigService()); |
| 153 | + int exitCode = await runner.RunAsync(args.ToArray()); |
| 154 | + return (exitCode, Path.Combine(reportsRoot, reportLabel)); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments