-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathMigrationManagerTests.cs
More file actions
156 lines (132 loc) · 5.19 KB
/
MigrationManagerTests.cs
File metadata and controls
156 lines (132 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using TestContext = Xunit.TestContext;
namespace LinkDotNet.Blog.UpgradeAssistant.Tests;
public sealed class MigrationManagerTests : IDisposable
{
private readonly string testDirectory;
public MigrationManagerTests()
{
testDirectory = Path.Combine(Path.GetTempPath(), $"blog-test-{Guid.NewGuid()}");
Directory.CreateDirectory(testDirectory);
}
[Fact]
public async Task Should_Migrate_From_11_To_12()
{
// Arrange
var testFile = Path.Combine(testDirectory, "appsettings.Development.json");
var json = """
{
"BlogName": "Test Blog"
}
""";
await File.WriteAllTextAsync(testFile, json, TestContext.Current.CancellationToken);
var manager = new MigrationManager([new Migration11To12()]);
var backupDir = Path.Combine(testDirectory, "backups");
// Act
var result = await manager.MigrateFileAsync(testFile, false, backupDir);
// Assert
result.ShouldBeTrue();
var content = await File.ReadAllTextAsync(testFile, TestContext.Current.CancellationToken);
content.ShouldContain("\"ConfigVersion\": \"12.0\"");
content.ShouldContain("\"ShowBuildInformation\": true");
// Verify backup was created
var backupFiles = Directory.GetFiles(backupDir);
backupFiles.ShouldNotBeEmpty();
}
[Fact]
public async Task Should_Not_Modify_Already_Migrated_File()
{
// Arrange
var testFile = Path.Combine(testDirectory, "appsettings.Production.json");
var json = """
{
"ConfigVersion": "12.0",
"BlogName": "Test Blog",
"ShowBuildInformation": true
}
""";
await File.WriteAllTextAsync(testFile, json, TestContext.Current.CancellationToken);
var manager = new MigrationManager([new Migration11To12()]);
var backupDir = Path.Combine(testDirectory, "backups");
// Act
var result = await manager.MigrateFileAsync(testFile, false, backupDir);
// Assert
result.ShouldBeTrue();
var content = await File.ReadAllTextAsync(testFile, TestContext.Current.CancellationToken);
content.ShouldBe(json); // Should not change
}
[Fact]
public async Task Should_Skip_Version_Controlled_Appsettings_Json()
{
// Arrange
var testFile = Path.Combine(testDirectory, "appsettings.json");
var json = """
{
"BlogName": "Test Blog"
}
""";
await File.WriteAllTextAsync(testFile, json, TestContext.Current.CancellationToken);
var manager = new MigrationManager([new Migration11To12()]);
var backupDir = Path.Combine(testDirectory, "backups");
// Act
var result = await manager.MigrateFileAsync(testFile, false, backupDir);
// Assert
result.ShouldBeTrue();
var content = await File.ReadAllTextAsync(testFile, TestContext.Current.CancellationToken);
content.ShouldBe(json); // Should not change
Directory.Exists(backupDir).ShouldBeFalse(); // No backup created
}
[Fact]
public async Task Should_Handle_Invalid_Json()
{
// Arrange
var testFile = Path.Combine(testDirectory, "appsettings.Invalid.json");
await File.WriteAllTextAsync(testFile, "{ invalid json }", TestContext.Current.CancellationToken);
var manager = new MigrationManager([new Migration11To12()]);
var backupDir = Path.Combine(testDirectory, "backups");
// Act
var result = await manager.MigrateFileAsync(testFile, false, backupDir);
// Assert
result.ShouldBeFalse();
}
[Fact]
public async Task Should_Handle_Missing_File()
{
// Arrange
var testFile = Path.Combine(testDirectory, "nonexistent.json");
var manager = new MigrationManager([new Migration11To12()]);
var backupDir = Path.Combine(testDirectory, "backups");
// Act
var result = await manager.MigrateFileAsync(testFile, false, backupDir);
// Assert
result.ShouldBeFalse();
}
[Fact]
public async Task DryRun_Should_Not_Modify_File()
{
// Arrange
var testFile = Path.Combine(testDirectory, "appsettings.Development.json");
var json = """
{
"BlogName": "Test Blog"
}
""";
await File.WriteAllTextAsync(testFile, json, TestContext.Current.CancellationToken);
var manager = new MigrationManager([new Migration11To12()]);
var backupDir = Path.Combine(testDirectory, "backups");
// Act
var result = await manager.MigrateFileAsync(testFile, true, backupDir);
// Assert
result.ShouldBeTrue();
var content = await File.ReadAllTextAsync(testFile, TestContext.Current.CancellationToken);
content.ShouldBe(json); // Should not change in dry-run mode
// Verify no backup was created in dry-run mode
Directory.Exists(backupDir).ShouldBeFalse();
}
public void Dispose()
{
if (Directory.Exists(testDirectory))
{
Directory.Delete(testDirectory, true);
}
}
}