public async Task TestConsoleProjectWorks()
        {
            string solFile = Util.GetSampleProject("console-with-libs", "console-with-libs.sln");

            using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile))
                using (var ws = await TypeSystemServiceTestExtensions.LoadSolution(sol)) {
                    var results = await Search("");

                    Assert.AreEqual(0, results.Count);

                    results = await Search("M");

                    Assert.AreEqual(6, results.Count);

                    results = await Search("type:M");

                    Assert.AreEqual(3, results.Count);

                    results = await Search("My");

                    // Should be 4: https://github.com/dotnet/roslyn/issues/29031
                    Assert.AreEqual(2, results.Count);

                    results = await Search("MC");

                    Assert.AreEqual(5, results.Count);
                }
        }
Esempio n. 2
0
        public async Task TestWorkspacePersistentStorageLocationService()
        {
            string solFile = Util.GetSampleProject("console-project", "ConsoleProject.sln");

            using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile))
                using (var ws = await TypeSystemServiceTestExtensions.LoadSolution(sol)) {
                    var storageLocationService = (MonoDevelopPersistentStorageLocationService)ws.Services.GetService <IPersistentStorageLocationService> ();
                    Assert.That(storageLocationService.TryGetStorageLocation(ws.CurrentSolution.Id), Is.Not.Null.Or.Empty);
                }
        }
Esempio n. 3
0
        public async Task TestWorkspacePersistentStorage()
        {
            string solFile = Util.GetSampleProject("console-project", "ConsoleProject.sln");

            using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile))
                using (var ws = await TypeSystemServiceTestExtensions.LoadSolution(sol)) {
                    try {
                        var storageLocationService = (MonoDevelopPersistentStorageLocationService)ws.Services.GetService <IPersistentStorageLocationService> ();
                        var storageLocation        = System.IO.Path.Combine(
                            storageLocationService.TryGetStorageLocation(ws.CurrentSolution.Id),
                            "sqlite3",
                            "storage.ide");

                        if (System.IO.File.Exists(storageLocation))
                        {
                            System.IO.File.Delete(storageLocation);
                        }

                        var solutionSizeTracker = (IIncrementalAnalyzerProvider)Composition.CompositionManager.Instance.GetExportedValue <ISolutionSizeTracker> ();

                        // This will return the tracker, since it's a singleton.
                        var analyzer = solutionSizeTracker.CreateIncrementalAnalyzer(ws);

                        // We need this hack because we can't guess when the work coordinator will run the incremental analyzers.
                        await analyzer.NewSolutionSnapshotAsync(ws.CurrentSolution, CancellationToken.None);

                        foreach (var projectFile in sol.GetAllProjects().SelectMany(x => x.Files.Where(file => file.BuildAction == BuildAction.Compile)))
                        {
                            var projectId = ws.GetProjectId(projectFile.Project);
                            var docId     = ws.GetDocumentId(projectId, projectFile.FilePath);
                            var doc       = ws.GetDocument(docId);
                            if (!doc.SupportsSyntaxTree)
                            {
                                continue;
                            }

                            await Microsoft.CodeAnalysis.FindSymbols.SyntaxTreeIndex.PrecalculateAsync(doc, CancellationToken.None);
                        }

                        var fi = new System.IO.FileInfo(storageLocation);
                        Assert.That(fi.Length, Is.GreaterThan(0));
                    } finally {
                        TypeSystemServiceTestExtensions.UnloadSolution(sol);
                    }
                }
        }
        public async Task CancelledParsedProjectionIsIgnored()
        {
            string solFile = Util.GetSampleProject("console-project", "ConsoleProject.sln");

            var parsers = TypeSystemService.Parsers;

            try {
                var projectionParser = new TypeSystemParserNode();
                projectionParser.BuildActions = new [] { "Compile" };
                projectionParser.MimeType     = "text/csharp";

                var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
                var field = typeof(TypeExtensionNode).GetField("type", flags);
                field.SetValue(projectionParser, typeof(ProjectionParser));

                var newParsers = new List <TypeSystemParserNode> ();
                newParsers.Add(projectionParser);
                TypeSystemService.Parsers = newParsers;

                using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile)) {
                    using (var ws = await TypeSystemServiceTestExtensions.LoadSolution(sol)) {
                        var source1 = new CancellationTokenSource();
                        var source2 = new CancellationTokenSource();

                        var options = new ParseOptions {
                            FileName = "first.xaml.cs"
                        };
                        var task1 = TypeSystemService.ParseProjection(options, "text/csharp", source1.Token);
                        source1.Cancel();

                        options = new ParseOptions {
                            FileName = "second.xaml.cs"
                        };
                        var task2 = TypeSystemService.ParseProjection(options, "text/csharp", source2.Token);

                        var result1 = await task1;
                        var result2 = await task2;

                        Assert.IsNotNull(result2);
                        Assert.IsNull(result1);
                    }
                }
            } finally {
                TypeSystemService.Parsers = parsers;
            }
        }
Esempio n. 5
0
        public async Task TestStorageDataIsNotRecomputed()
        {
            string solFile = Util.GetSampleProject("console-project", "ConsoleProject.sln");

            var checkSum1 = await RunTest(usedCache : false);

            var checkSum2 = await RunTest(usedCache : true);

            Assert.AreEqual(checkSum1, checkSum2);

            async Task <Microsoft.CodeAnalysis.Checksum> RunTest(bool usedCache)
            {
                var initial = Logger.GetLogger();

                using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile))
                    using (var ws = await TypeSystemServiceTestExtensions.LoadSolution(sol)) {
                        try {
                            var persistentStorageService = ws.Services.GetService <IPersistentStorageService> ();
                            Assert.That(persistentStorageService, Is.TypeOf(typeof(Microsoft.CodeAnalysis.SQLite.SQLitePersistentStorageService)));

                            if (!(persistentStorageService is Microsoft.CodeAnalysis.SQLite.SQLitePersistentStorageService sqlitePersistentStorageService))
                            {
                                return(null);
                            }

                            var solutionSizeTracker = (IIncrementalAnalyzerProvider)Composition.CompositionManager.Instance.GetExportedValue <ISolutionSizeTracker> ();
                            // This will return the tracker, since it's a singleton.
                            var analyzer = solutionSizeTracker.CreateIncrementalAnalyzer(ws);

                            // We need this hack because we can't guess when the work coordinator will run the incremental analyzers.
                            await analyzer.NewSolutionSnapshotAsync(ws.CurrentSolution, CancellationToken.None);

                            var storageLogger   = new StorageCheckingLogger();
                            var aggregateLogger = AggregateLogger.Create(initial, storageLogger);
                            Logger.SetLogger(aggregateLogger);

                            var provider     = new SymbolTreeInfoIncrementalAnalyzerProvider();
                            var cacheService = (Microsoft.CodeAnalysis.FindSymbols.SymbolTree.ISymbolTreeInfoCacheService)provider.CreateService(ws.Services);

                            var incrementalAnalyzer = provider.CreateIncrementalAnalyzer(ws);

                            var project       = sol.GetAllProjects().Single();
                            var roslynProject = IdeServices.TypeSystemService.GetProject(project);

                            await incrementalAnalyzer.AnalyzeProjectAsync(roslynProject, default, default, CancellationToken.None);
        public async Task ProjectReferencingOutputTrackedReference()
        {
            string solFile = Util.GetSampleProject("csharp-app-fsharp-lib", "csappfslib.sln");

            using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile)) {
                var csharpApp     = sol.Items.FirstOrDefault(pr => pr.Name == "csappfslib") as DotNetProject;
                var fsharpLibrary = sol.Items.FirstOrDefault(pr => pr.Name == "fslib") as DotNetProject;
                Assert.IsTrue(IdeApp.TypeSystemService.IsOutputTrackedProject(fsharpLibrary));

                using (var workspace = await TypeSystemServiceTestExtensions.LoadSolution(sol)) {
                    var projectId = workspace.GetProjectId(csharpApp);

                    var analysisProject = workspace.CurrentSolution.GetProject(projectId);
                    var refs            = analysisProject.MetadataReferences.Select(r => new FilePath(r.Display).FileName);
                    Assert.That(refs, Contains.Item("fslib.dll"));
                }
            }
        }
        public async Task ProjectModifiedWhilstBeingAddedToSolution()
        {
            string solFile = Util.GetSampleProject("console-project", "ConsoleProject.sln");

            using (var sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile)) {
                try {
                    var workspace = await TypeSystemServiceTestExtensions.LoadSolution(sol);

                    var project = new ModifyReferencesDuringGetSourceFilesDotNetProject();
                    project.FileName = Path.Combine(sol.BaseDirectory, "ProjectModifiedWhilstBeingAddedToSolution.csproj");
                    await project.SaveAsync(Util.GetMonitor());

                    var reference = ProjectReference.CreateCustomReference(ReferenceType.Package, "System.ComponentModel.Composition");

                    var assemblyFileName = typeof(System.Xml.Linq.XName).Assembly.Location;
                    project.AddExtraReference(new AssemblyReference(assemblyFileName));

                    var projectLoadedTask = new TaskCompletionSource <bool> ();
                    workspace.WorkspaceChanged += (sender, e) => {
                        if (e.Kind == Microsoft.CodeAnalysis.WorkspaceChangeKind.ProjectReloaded)
                        {
                            projectLoadedTask.TrySetResult(true);
                        }
                    };

                    sol.RootFolder.AddItem(project);

                    if (await Task.WhenAny(projectLoadedTask.Task, Task.Delay(5000)) != projectLoadedTask.Task)
                    {
                        Assert.Fail("Timeout waiting for project to be reloaded by the type system service.");
                    }

                    var projectId         = workspace.GetProjectId(project);
                    var projectInfo       = workspace.CurrentSolution.GetProject(projectId);
                    var metadataReference = projectInfo.MetadataReferences
                                            .OfType <Microsoft.CodeAnalysis.PortableExecutableReference> ()
                                            .FirstOrDefault(r => r.FilePath.EndsWith("System.Xml.Linq.dll", StringComparison.Ordinal));

                    Assert.IsNotNull(metadataReference, "System.Xml.Linq reference missing from type system information");
                } finally {
                    TypeSystemServiceTestExtensions.UnloadSolution(sol);
                }
            }
        }
Esempio n. 8
0
        public async Task TestWorkspacePersistentStorageImplementation()
        {
            string solFile     = Util.GetSampleProject("console-project", "ConsoleProject.sln");
            var    streamName1 = "PersistentService_Solution_WriteReadDifferentInstances1";
            var    streamName2 = "PersistentService_Solution_WriteReadDifferentInstances2";

            using (Solution sol = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solFile))
                using (var ws = await TypeSystemServiceTestExtensions.LoadSolution(sol)) {
                    try {
                        var persistentStorageService = ws.Services.GetService <IPersistentStorageService> ();
                        Assert.That(persistentStorageService, Is.TypeOf(typeof(Microsoft.CodeAnalysis.SQLite.SQLitePersistentStorageService)));

                        if (!(persistentStorageService is Microsoft.CodeAnalysis.SQLite.SQLitePersistentStorageService sqlitePersistentStorageService))
                        {
                            return;
                        }

                        var solutionSizeTracker = (IIncrementalAnalyzerProvider)Composition.CompositionManager.Instance.GetExportedValue <ISolutionSizeTracker> ();
                        // This will return the tracker, since it's a singleton.
                        var analyzer = solutionSizeTracker.CreateIncrementalAnalyzer(ws);

                        // We need this hack because we can't guess when the work coordinator will run the incremental analyzers.
                        await analyzer.NewSolutionSnapshotAsync(ws.CurrentSolution, CancellationToken.None);

                        // Due to the nature of roslyn returning a new wrapper every time we request the storage, do a reflection check.
                        const System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
                        var fieldInfo = sqlitePersistentStorageService.GetType().BaseType.GetField("_currentPersistentStorage", flags);

                        using (var persistentStorage = sqlitePersistentStorageService.GetStorage(ws.CurrentSolution, checkBranchId: false)) {
                            Assert.That(persistentStorage, Is.Not.TypeOf(typeof(NoOpPersistentStorage)));

                            Assert.True(await persistentStorage.WriteStreamAsync(streamName1, EncodeString("MyString")));
                            Assert.True(await persistentStorage.WriteStreamAsync(streamName2, EncodeString("MyString2")));
                        }

                        var initialFieldValue = fieldInfo.GetValue(sqlitePersistentStorageService);

                        using (var persistentStorage = sqlitePersistentStorageService.GetStorage(ws.CurrentSolution, checkBranchId: false)) {
                            Assert.That(persistentStorage, Is.Not.TypeOf(typeof(NoOpPersistentStorage)));
                        }

                        var secondFieldValue = fieldInfo.GetValue(sqlitePersistentStorageService);

                        Assert.AreSame(initialFieldValue, secondFieldValue);

                        using (var persistentStorage = sqlitePersistentStorageService.GetStorage(ws.CurrentSolution, checkBranchId: false)) {
                            Assert.AreEqual("MyString", ReadStringToEnd(await persistentStorage.ReadStreamAsync(streamName1)));
                            Assert.AreEqual("MyString2", ReadStringToEnd(await persistentStorage.ReadStreamAsync(streamName2)));
                        }
                    } finally {
                        TypeSystemServiceTestExtensions.UnloadSolution(sol);
                    }
                }

            Stream EncodeString(string text)
            {
                var bytes  = Encoding.UTF8.GetBytes(text);
                var stream = new MemoryStream(bytes);

                return(stream);
            }

            string ReadStringToEnd(Stream stream)
            {
                using (stream) {
                    var bytes = new byte [stream.Length];
                    int count = 0;
                    while (count < stream.Length)
                    {
                        count = stream.Read(bytes, count, (int)stream.Length - count);
                    }

                    return(Encoding.UTF8.GetString(bytes));
                }
            }
        }