Esempio n. 1
0
        public void SarifFolder_DoesNot_Exist()
        {
            string sarifDirectory    = ".sarif";
            string solutionDirectory = @"C:\some-solution-folder";
            string combinedPath      = Path.Combine(solutionDirectory, sarifDirectory);

            // folder doesnt exist
            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(fs => fs.DirectoryExists(It.IsAny <string>())).Returns(false);

            var mockFileWatcher = new Mock <IFileWatcher>();

            var mockLoadService  = new Mock <ILoadSarifLogService>();
            var mockCloseService = new Mock <ICloseSarifLogService>();

            var monitor = new SarifFolderMonitor(mockFileSystem.Object, mockFileWatcher.Object, mockLoadService.Object, mockCloseService.Object);

            monitor.StartWatch(solutionDirectory);

            // methods should not be called
            mockLoadService.Verify(m => m.LoadSarifLogs(It.IsAny <IEnumerable <string> >(), It.IsAny <bool>()), Times.Never);
            mockLoadService.Verify(m => m.LoadSarifLog(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never);

            monitor.StopWatch();

            mockCloseService.Verify(m => m.CloseSarifLogs(It.IsAny <IEnumerable <string> >()), Times.Never);
        }
Esempio n. 2
0
        public void SarifFolder_Does_Exist_WithoutSarifFile()
        {
            string sarifDirectory    = ".sarif";
            string solutionDirectory = @"C:\some-solution-folder";
            string combinedPath      = Path.Combine(solutionDirectory, sarifDirectory);
            string searchPattern     = Constants.SarifFileSearchPattern;

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(fs => fs.DirectoryExists(It.IsAny <string>())).Returns(false);
            mockFileSystem.Setup(fs => fs.DirectoryExists(solutionDirectory)).Returns(true);
            mockFileSystem.Setup(fs => fs.DirectoryExists(combinedPath)).Returns(true);
            // no sarif files
            mockFileSystem.Setup(fs => fs.DirectoryGetFiles(combinedPath, searchPattern)).Returns(new string[] { });

            var mockFileWatcher = new Mock <IFileWatcher>();

            var mockLoadService  = new Mock <ILoadSarifLogService>();
            var mockCloseService = new Mock <ICloseSarifLogService>();

            var monitor = new SarifFolderMonitor(mockFileSystem.Object, mockFileWatcher.Object, mockLoadService.Object, mockCloseService.Object);

            monitor.StartWatch(solutionDirectory);

            mockLoadService.Verify(m => m.LoadSarifLogs(It.IsAny <IEnumerable <string> >(), It.IsAny <bool>()), Times.Once);
            mockLoadService.Verify(m => m.LoadSarifLog(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never);
            mockFileWatcher.Verify(m => m.Start(), Times.Once);

            monitor.StopWatch();

            mockCloseService.Verify(m => m.CloseSarifLogs(It.IsAny <IEnumerable <string> >()), Times.Once);
            mockFileWatcher.Verify(m => m.Stop(), Times.Once);
        }
Esempio n. 3
0
        public void SarifFolder_Delete_ExistingSarifFiles()
        {
            string sarifDirectory    = ".sarif";
            string solutionDirectory = @"C:\some-solution-folder";
            string combinedPath      = Path.Combine(solutionDirectory, sarifDirectory);
            string searchPattern     = Constants.SarifFileSearchPattern;
            var    sarifFiles        = new List <string>
            {
                Path.Combine(combinedPath, "Test1.sarif"),
                Path.Combine(combinedPath, "Test2.sarif"),
                Path.Combine(combinedPath, "Test3.sarif"),
            };

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(fs => fs.DirectoryExists(It.IsAny <string>())).Returns(false);
            mockFileSystem.Setup(fs => fs.DirectoryExists(solutionDirectory)).Returns(true);
            mockFileSystem.Setup(fs => fs.DirectoryExists(combinedPath)).Returns(true);
            mockFileSystem.Setup(fs => fs.DirectoryGetFiles(combinedPath, searchPattern)).Returns(sarifFiles);

            var mockFileWatcher = new Mock <IFileWatcher>();

            var mockLoadService  = new Mock <ILoadSarifLogService>();
            var mockCloseService = new Mock <ICloseSarifLogService>();

            var monitor = new SarifFolderMonitor(mockFileSystem.Object, mockFileWatcher.Object, mockLoadService.Object, mockCloseService.Object);

            monitor.StartWatch(solutionDirectory);

            mockLoadService.Verify(m => m.LoadSarifLogs(It.IsAny <IEnumerable <string> >(), It.IsAny <bool>()), Times.Once);
            mockLoadService.Verify(m => m.LoadSarifLog(It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never);
            mockFileWatcher.Verify(m => m.Start(), Times.Once);

            // simulate deleting one sarif file
            string newFile  = Path.Combine(combinedPath, "Test1.sarif");
            var    eventArg = new FileSystemEventArgs(WatcherChangeTypes.Deleted, newFile, string.Empty);

            // trigger file deleted event 1st time
            mockFileWatcher.Raise(fw => fw.SarifLogFileDeleted += null, eventArg);
            mockCloseService.Verify(m => m.CloseSarifLogs(It.IsAny <IEnumerable <string> >()), Times.Once);


            // trigger file created event 2nd time
            newFile  = Path.Combine(combinedPath, "Test2.sarif");
            eventArg = new FileSystemEventArgs(WatcherChangeTypes.Deleted, newFile, string.Empty);
            mockFileWatcher.Raise(fw => fw.SarifLogFileDeleted += null, eventArg);

            newFile  = Path.Combine(combinedPath, "Test3.sarif");
            eventArg = new FileSystemEventArgs(WatcherChangeTypes.Deleted, newFile, string.Empty);
            mockFileWatcher.Raise(fw => fw.SarifLogFileDeleted += null, eventArg);

            // 2 files deleted, method expected to be called 1 + 2 times
            mockCloseService.Verify(m => m.CloseSarifLogs(It.IsAny <IEnumerable <string> >()), Times.Exactly(3));

            monitor.StopWatch();

            mockCloseService.Verify(m => m.CloseSarifLogs(It.IsAny <IEnumerable <string> >()), Times.Exactly(4));
            mockFileWatcher.Verify(m => m.Stop(), Times.Once);
        }