Beispiel #1
0
        public async Task WriteAllText_FileDoesNotExist_NativeFileWatcherGeneratesChangedCreatedDeletedEvents()
        {
            FilePath rootProject = Util.GetSampleProject("FileWatcherTest", "Root.csproj");
            var      testFile    = rootProject.ParentDirectory.Combine("test1.txt");

            ClearFileEventsCaptured();
            var id = new object();

            try {
                await FileWatcherService.WatchDirectories(id, new [] { rootProject.ParentDirectory });

                File.WriteAllText(testFile, DateTime.Now.ToString());
                File.WriteAllText(testFile, "test1");
                File.WriteAllText(rootProject, "test2");

                await WaitForFilesChanged(new [] { rootProject, testFile });

                AssertFileChanged(rootProject);

                // Check the delete event is not generated for the file being created and written to.
                Assert.IsFalse(filesRemoved.Any(file => file.FileName == testFile));
            } finally {
                await FileWatcherService.WatchDirectories(id, null);
            }
        }
Beispiel #2
0
        public async Task WatchDirectories_SolutionOpen_TwoFilesDeleted()
        {
            FilePath rootSolFile = Util.GetSampleProject("FileWatcherTest", "Root.sln");
            string   solFile     = rootSolFile.ParentDirectory.Combine("FileWatcherTest", "FileWatcherTest.sln");

            sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile);

            var file1       = rootSolFile.ParentDirectory.Combine("FileWatcherTest", "MyClass.cs");
            var file2       = rootSolFile.ParentDirectory.Combine("Library", "Properties", "AssemblyInfo.cs");
            var directories = new [] {
                file1.ParentDirectory,
                file2.ParentDirectory
            };
            var id = new object();

            try {
                await FileWatcherService.WatchDirectories(id, directories);

                ClearFileEventsCaptured();
                await FileWatcherService.Add(sol);

                File.Delete(file1);
                File.Delete(file2);

                await WaitForFilesRemoved(new [] { file1, file2 });

                AssertFileRemoved(file1);
                AssertFileRemoved(file2);
                Assert.AreEqual(1, filesRemoved.Count(fileChange => fileChange.FileName == file1));
                Assert.AreEqual(1, filesRemoved.Count(fileChange => fileChange.FileName == file2));
            } finally {
                await FileWatcherService.WatchDirectories(id, null);
            }
        }
Beispiel #3
0
        public async Task WatchDirectories_EmptyDirectory_NoExceptionThrown()
        {
            var directories = new [] {
                FilePath.Empty
            };

            var id = new object();

            try {
                await FileWatcherService.WatchDirectories(id, directories);
            } finally {
                await FileWatcherService.WatchDirectories(id, null);
            }
        }
Beispiel #4
0
        public async Task WatchDirectories_DirectoryDoesNotExist_NoExceptionThrown()
        {
            FilePath rootProject      = Util.GetSampleProject("FileWatcherTest", "Root.csproj");
            var      invalidDirectory = rootProject.Combine("Invalid");
            var      directories      = new [] {
                invalidDirectory
            };

            var id = new object();

            try {
                await FileWatcherService.WatchDirectories(id, directories);

                Assert.IsFalse(Directory.Exists(invalidDirectory));
            } finally {
                await FileWatcherService.WatchDirectories(id, null);
            }
        }
Beispiel #5
0
        public async Task WatchDirectories_TwoFilesChanged_OneClosed()
        {
            FilePath rootSolFile = Util.GetSampleProject("FileWatcherTest", "Root.sln");
            var      file1       = rootSolFile.ParentDirectory.Combine("FileWatcherTest", "MyClass.cs");
            var      file2       = rootSolFile.ParentDirectory.Combine("Library", "Properties", "AssemblyInfo.cs");
            var      id          = new object();

            try {
                var directories = new [] {
                    file1.ParentDirectory,
                    file2.ParentDirectory
                };
                await FileWatcherService.WatchDirectories(id, directories);

                TextFileUtility.WriteText(file1, string.Empty, Encoding.UTF8);
                TextFileUtility.WriteText(file2, string.Empty, Encoding.UTF8);
                await WaitForFilesChanged(new [] { file1, file2 });

                AssertFileChanged(file1);
                AssertFileChanged(file2);

                // Unwatch one directory.
                directories = new [] {
                    file1.ParentDirectory
                };
                await FileWatcherService.WatchDirectories(id, directories);

                ClearFileEventsCaptured();
                fileChangesTask = new TaskCompletionSource <bool> ();

                TextFileUtility.WriteText(file2, string.Empty, Encoding.UTF8);
                TextFileUtility.WriteText(file1, string.Empty, Encoding.UTF8);
                await WaitForFilesChanged(new [] { file1, file2 });

                AssertFileChanged(file1);
                Assert.IsFalse(fileChanges.Any(f => f.FileName == file2));
            } finally {
                await FileWatcherService.WatchDirectories(id, null);
            }
        }
Beispiel #6
0
        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);
            }
        }