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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
//
// GetSourceFilesAsyncTests.cs
//
// Author:
// Greg Munn <greg.munn@xamarin.com>
//
// Copyright (c) 2015 Xamarin Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MonoDevelop.Core;
using MonoDevelop.Projects.MSBuild;
using NUnit.Framework;
using UnitTests;
namespace MonoDevelop.Projects
{
[TestFixture]
public class GetSourceFilesAsyncTests : TestBase
{
TaskCompletionSource<object> fileChangeNotification;
string waitingForFileNameChange;
[SetUp]
public void TestSetUp ()
{
fileChangeNotification = new TaskCompletionSource<object> ();
FileService.FileChanged -= FileService_FileChanged;
FileService.FileChanged += FileService_FileChanged;
}
[TearDown]
public void TestTearDown ()
{
FileService.FileChanged -= FileService_FileChanged;
fileChangeNotification = null;
}
[Test()]
public async Task GetSourceFilesFromProjectWithImportedCompileItems()
{
string projectFile = Util.GetSampleProject ("project-with-corecompiledepends", "project-with-imported-files.csproj");
var project = (Project) await Services.ProjectService.ReadSolutionItem (Util.GetMonitor (), projectFile);
var projectFiles = project.Files.Where (f => f.Subtype != Subtype.Directory).ToList ();
var sourceFiles = await project.GetSourceFilesAsync (project.Configurations[0].Selector);
var msg = "GetSourceFilesAsync should include imported items";
Assert.AreEqual (projectFiles.Count + 1, sourceFiles.Length, msg);
msg = "When a project does specify CoreCompileDependsOn that adds Compile items, then GetSourceFilesAsync should include all of Files as well";
foreach (var file in projectFiles) {
var sourceFile = sourceFiles.FirstOrDefault (sf => sf.FilePath == file.FilePath);
Assert.IsNotNull (sourceFile, msg);
}
Assert.IsTrue (sourceFiles.Any (f => f.FilePath.FileName == "Foo.cs"));
project.Dispose ();
}
[Test()]
public async Task GetSourceFilesFromProjectWithAddedCompileItems()
{
string projectFile = Util.GetSampleProject ("project-with-corecompiledepends", "project.csproj");
var project = (Project) await Services.ProjectService.ReadSolutionItem (Util.GetMonitor (), projectFile);
var projectFiles = project.Files.Where (f => f.Subtype != Subtype.Directory).ToList ();
var sourceFiles = await project.GetSourceFilesAsync (project.Configurations[0].Selector);
var msg = "When a project does specify CoreCompileDependsOn that adds Compile items, then GetSourceFilesAsync should include the added item(s)";
Assert.AreEqual (projectFiles.Count + 2, sourceFiles.Length, msg);
msg = "When a project does specify CoreCompileDependsOn that adds Compile items, then GetSourceFilesAsync should include all of Files as well";
foreach (var file in projectFiles) {
var sourceFile = sourceFiles.FirstOrDefault (sf => sf.FilePath == file.FilePath);
Assert.IsNotNull (sourceFile, msg + ": " + file.FilePath.FileName + " not found");
}
project.Dispose ();
}
[Test()]
public async Task FileChangeCalledWhenPerformGeneratorAsyncInvoked()
{
string projectFile = Util.GetSampleProject ("project-with-corecompiledepends", "project.csproj");
var project = (Project) await Services.ProjectService.ReadSolutionItem (Util.GetMonitor (), projectFile);
// FIXME: we need to mimic the project load step because MonoDevelopWorkspace calls this on project load
// this has the effect of initialising the cache for GetSourceFilesAsync, which makes change detection work once the project has loaded
// for when PerformGeneratorAsync is called. this also causes the file to be written on load if it is not present, which doesn't
// help us simulate the target changing the file
await project.GetSourceFilesAsync (project.Configurations[0].Selector);
// clean it out so that the call to `PerformGeneratorAsync` can create it instead, that's what we want to test
var generatedFileName = Path.Combine (project.ItemDirectory, "GeneratedFile.g.cs");
if (File.Exists (generatedFileName)) {
File.Delete (generatedFileName);
}
var id = new object ();
try {
waitingForFileNameChange = generatedFileName;
await FileWatcherService.WatchDirectories (id, new [] { project.BaseDirectory });
await project.PerformGeneratorAsync (project.Configurations[0].Selector, "UpdateGeneratedFiles");
// we need to wait for the file notification to be posted
await Task.Run (() => {
fileChangeNotification.Task.Wait (TimeSpan.FromMilliseconds (10000));
});
Assert.IsTrue (fileChangeNotification.Task.IsCompleted, "Performing the generator should have fired a file change event");
project.Dispose ();
} finally {
await FileWatcherService.WatchDirectories (id, null);
}
}
[Test()]
public async Task GetSourceFilesFromProjectWithoutCoreCompileDependsOn ()
{
string projectFile = Util.GetSampleProject ("project-without-corecompiledepends", "project.csproj");
var project = (Project) await Services.ProjectService.ReadSolutionItem (Util.GetMonitor (), projectFile);
var projectFiles = project.Files.Where (f => f.Subtype != Subtype.Directory).ToList ();
var sourceFiles = await project.GetSourceFilesAsync (project.Configurations[0].Selector);
const string msg = "When a project does not specify CoreCompileDependsOn, then GetSourceFilesAsync should be identical to Files";
Assert.AreEqual (projectFiles.Count, sourceFiles.Length, msg);
foreach (var file in projectFiles) {
var sourceFile = sourceFiles.FirstOrDefault (sf => sf.FilePath == file.FilePath);
Assert.IsNotNull (sourceFile, msg);
}
project.Dispose ();
}
void FileService_FileChanged (object sender, FileEventArgs e)
{
var tcs = fileChangeNotification;
if (tcs != null) {
if (e.Any (info => info.FileName == waitingForFileNameChange))
tcs.TrySetResult (null);
}
}
[Test ()]
public async Task FilesWithConfigurationCondition ()
{
string projectFile = Util.GetSampleProject ("project-with-corecompiledepends", "project-with-conditioned-file.csproj");
var project = (Project)await Services.ProjectService.ReadSolutionItem (Util.GetMonitor (), projectFile);
var projectFiles = project.Files.Where (f => f.Subtype != Subtype.Directory).ToList ();
var sourceFiles = await project.GetSourceFilesAsync (project.Configurations ["Debug|x86"].Selector);
Assert.IsTrue (sourceFiles.Any (f => f.FilePath.FileName == "Conditioned.cs"));
sourceFiles = await project.GetSourceFilesAsync (project.Configurations ["Release|x86"].Selector);
Assert.IsFalse (sourceFiles.Any (f => f.FilePath.FileName == "Conditioned.cs"));
project.Dispose ();
}
[Test]
public async Task ImportWithCoreCompileDependsOnAddedAfterSourceFilesCached ()
{
string projectFile = Util.GetSampleProject ("project-with-corecompiledepends", "consoleproject.csproj");
var project = (Project)await Services.ProjectService.ReadSolutionItem (Util.GetMonitor (), projectFile);
var projectFiles = project.Files.Where (f => f.Subtype != Subtype.Directory).ToList ();
var sourceFiles = await project.GetSourceFilesAsync (project.Configurations[0].Selector);
Assert.AreEqual (projectFiles.Count, sourceFiles.Count ());
Assert.AreEqual (0, sourceFiles.Count ());
string modifiedHint = null;
project.Modified += (sender, args) => modifiedHint = args.First ().Hint;
var before = new MSBuildItem (); // Ensures import added at end of project.
project.MSBuildProject.AddNewImport ("consoleproject-import.targets", null, before);
Assert.AreEqual ("Files", modifiedHint);
sourceFiles = await project.GetSourceFilesAsync (project.Configurations[0].Selector);
Assert.IsTrue (sourceFiles.Any (f => f.FilePath.FileName == "GeneratedFile.g.cs"));
modifiedHint = null;
project.MSBuildProject.RemoveImport ("consoleproject-import.targets");
Assert.AreEqual ("Files", modifiedHint);
sourceFiles = await project.GetSourceFilesAsync (project.Configurations[0].Selector);
Assert.IsFalse (sourceFiles.Any (f => f.FilePath.FileName == "GeneratedFile.g.cs"));
Assert.AreEqual (0, sourceFiles.Count ());
project.Dispose ();
}
/// <summary>
/// Bug 705785: [Feedback] Search result tab has temporary files
/// </summary>
[Test ()]
public async Task GetSourceFilesFromProjectWithDesignerfiles_VSTS705785 ()
{
string projectFile = Util.GetSampleProject ("project-with-corecompiledepends", "project-with-design-files.csproj");
using (var project = (Project)await Services.ProjectService.ReadSolutionItem (Util.GetMonitor (), projectFile)) {
var sourceFiles = await project.GetSourceFilesAsync (project.Configurations [0].Selector);
var activityFile = sourceFiles.FirstOrDefault (f => f.Name.EndsWith("MainActivity.cs", StringComparison.Ordinal));
Assert.AreEqual (Subtype.Code, activityFile.Subtype);
var file2 = sourceFiles.FirstOrDefault (f => f.Name.EndsWith("GeneratedFile.g.cs", StringComparison.Ordinal));
Assert.AreEqual (Subtype.Designer, file2.Subtype);
}
}
/// <summary>
/// Ensures GetSourceFilesAsync does not include the old filename.
/// </summary>
[Test]
public async Task FileRenamedInProject ()
{
var solutionFile = Util.GetSampleProject ("csharp-console", "csharp-console.sln");
using (var solution = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solutionFile)) {
var project = solution.FindProjectByName ("csharp-console");
var config = ConfigurationSelector.Default;
var sourceFiles = await project.GetSourceFilesAsync (config);
Assert.IsTrue (sourceFiles.Any (f => f.FilePath.FileName == "Program.cs"));
Assert.IsFalse (sourceFiles.Any (f => f.FilePath.FileName == "RenamedProgram.cs"));
// Rename Program.cs
var projectFile = project.Files.FirstOrDefault (f => f.FilePath.FileName == "Program.cs");
var newFileName = projectFile.FilePath.ParentDirectory.Combine ("RenamedProgram.cs");
FileService.RenameFile (projectFile.FilePath, newFileName);
projectFile.Name = newFileName;
await project.SaveAsync (Util.GetMonitor ());
// Check that source files is correct.
sourceFiles = await project.GetSourceFilesAsync (config);
Assert.IsTrue (sourceFiles.Any (f => f.FilePath.FileName == "RenamedProgram.cs"));
Assert.IsFalse (sourceFiles.Any (f => f.FilePath.FileName == "Program.cs"));
}
}
}
}
|