|
2 | 2 | // Licensed under the MIT license. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections.Immutable; |
5 | 6 | using System.Linq; |
6 | 7 | using System.Threading; |
7 | 8 | using System.Threading.Tasks; |
@@ -1137,13 +1138,77 @@ await _projectManager.UpdateAsync(updater => |
1137 | 1138 | x => x.DocumentRemoved(DocumentFilePath2, miscProject.Key)); |
1138 | 1139 | } |
1139 | 1140 |
|
1140 | | - private static TextLoader CreateEmptyTextLoader() |
| 1141 | + [Fact] |
| 1142 | + public async Task AddProject_MigratesMiscellaneousDocumentsToNewOwnerProject_FixesTargetPath() |
| 1143 | + { |
| 1144 | + // Arrange |
| 1145 | + const string ProjectFilePath = "C:/path/to/project.csproj"; |
| 1146 | + const string IntermediateOutputPath = "C:/path/to/obj"; |
| 1147 | + const string DocumentFilePath1 = "C:/path/to/document1.cshtml"; |
| 1148 | + const string DocumentFilePath2 = "C:/path/to/document2.cshtml"; |
| 1149 | + |
| 1150 | + var miscProject = _snapshotResolver.GetMiscellaneousProject(); |
| 1151 | + |
| 1152 | + await _projectManager.UpdateAsync(updater => |
| 1153 | + { |
| 1154 | + updater.DocumentAdded(miscProject.Key, |
| 1155 | + new HostDocument(DocumentFilePath1, "C:/path/to/document1.cshtml"), CreateEmptyTextLoader()); |
| 1156 | + updater.DocumentAdded(miscProject.Key, |
| 1157 | + new HostDocument(DocumentFilePath2, "C:/path/to/document2.cshtml"), CreateEmptyTextLoader()); |
| 1158 | + }); |
| 1159 | + |
| 1160 | + // Act |
| 1161 | + var newProjectKey = await _projectService.AddProjectAsync( |
| 1162 | + ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, rootNamespace: null, displayName: null, DisposalToken); |
| 1163 | + |
| 1164 | + // Assert |
| 1165 | + var newProject = _projectManager.GetLoadedProject(newProjectKey); |
| 1166 | + Assert.Equal("document1.cshtml", newProject.GetDocument(DocumentFilePath1)!.TargetPath); |
| 1167 | + Assert.Equal("document2.cshtml", newProject.GetDocument(DocumentFilePath2)!.TargetPath); |
| 1168 | + } |
| 1169 | + |
| 1170 | + [Fact] |
| 1171 | + public async Task AddOrUpdateProjectAsync_MigratesMiscellaneousDocumentsToNewOwnerProject_MaintainsTextState() |
| 1172 | + { |
| 1173 | + // Arrange |
| 1174 | + const string ProjectFilePath = "C:/path/to/project.csproj"; |
| 1175 | + const string IntermediateOutputPath = "C:/path/to/obj"; |
| 1176 | + const string DocumentFilePath1 = "C:/path/to/document1.cshtml"; |
| 1177 | + |
| 1178 | + var miscProject = _snapshotResolver.GetMiscellaneousProject(); |
| 1179 | + |
| 1180 | + await _projectManager.UpdateAsync(updater => |
| 1181 | + { |
| 1182 | + updater.DocumentAdded(miscProject.Key, |
| 1183 | + new HostDocument(DocumentFilePath1, "other/document1.cshtml"), CreateTextLoader(SourceText.From("Hello"))); |
| 1184 | + }); |
| 1185 | + |
| 1186 | + var projectKey = new ProjectKey(IntermediateOutputPath); |
| 1187 | + |
| 1188 | + var documentHandles = ImmutableArray.Create(new DocumentSnapshotHandle(DocumentFilePath1, "document1.cshtml", "mvc")); |
| 1189 | + |
| 1190 | + // Act |
| 1191 | + await _projectService.AddOrUpdateProjectAsync( |
| 1192 | + projectKey, ProjectFilePath, RazorConfiguration.Default, rootNamespace: null, displayName: null, ProjectWorkspaceState.Default, documentHandles, DisposalToken); |
| 1193 | + |
| 1194 | + // Assert |
| 1195 | + var newProject = _projectManager.GetLoadedProject(projectKey); |
| 1196 | + var documentText = await newProject.GetDocument(DocumentFilePath1)!.GetTextAsync(); |
| 1197 | + Assert.Equal("Hello", documentText.ToString()); |
| 1198 | + } |
| 1199 | + |
| 1200 | + private static TextLoader CreateTextLoader(SourceText text) |
1141 | 1201 | { |
1142 | 1202 | var textLoaderMock = new StrictMock<TextLoader>(); |
1143 | 1203 | textLoaderMock |
1144 | 1204 | .Setup(x => x.LoadTextAndVersionAsync(It.IsAny<LoadTextOptions>(), It.IsAny<CancellationToken>())) |
1145 | | - .ReturnsAsync(TextAndVersion.Create(s_emptyText, VersionStamp.Create())); |
| 1205 | + .ReturnsAsync(TextAndVersion.Create(text, VersionStamp.Create())); |
1146 | 1206 |
|
1147 | 1207 | return textLoaderMock.Object; |
1148 | 1208 | } |
| 1209 | + |
| 1210 | + private static TextLoader CreateEmptyTextLoader() |
| 1211 | + { |
| 1212 | + return CreateTextLoader(s_emptyText); |
| 1213 | + } |
1149 | 1214 | } |
0 commit comments