Esempio n. 1
0
        public void CloseNonExistentCompilation_ShouldClearDiagnostics()
        {
            PublishDiagnosticsParams?receivedParams = null;

            var document = CreateMockDocument(p => receivedParams = p);

            var server = CreateMockServer(document);

            var manager = new BicepCompilationManager(server.Object, new BicepCompilationProvider(TestResourceTypeProvider.Create(), CreateEmptyFileResolver()), new Workspace());

            var uri = DocumentUri.File(this.TestContext.TestName);

            manager.CloseCompilation(uri);

            // close should have cleared diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            // expect zero diagnostics and 0 version
            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(0);
            receivedParams.Diagnostics.Should().BeEmpty();

            // reset call counts
            document.Invocations.Clear();
        }
        private void RefreshCompilationOfSourceFilesInWorkspace(string bicepFileContents, string bicepConfigFileContents, bool saveBicepConfigFile, out Mock <ITextDocumentLanguageServer> document, out Container <Diagnostic>?diagnostics)
        {
            PublishDiagnosticsParams?receivedParams = null;

            document = BicepCompilationManagerHelper.CreateMockDocument(p => receivedParams = p);
            ILanguageServerFacade server = BicepCompilationManagerHelper.CreateMockServer(document).Object;

            string testOutputPath = Path.Combine(TestContext.ResultsDirectory, Guid.NewGuid().ToString());

            var bicepFilePath = FileHelper.SaveResultFile(TestContext, "input.bicep", bicepFileContents, testOutputPath);
            var workspace     = new Workspace();

            var bicepCompilationManager = new BicepCompilationManager(server, BicepCompilationManagerHelper.CreateEmptyCompilationProvider(), workspace, new FileResolver(), BicepCompilationManagerHelper.CreateMockScheduler().Object, new ConfigurationManager(new IOFileSystem()));

            bicepCompilationManager.UpsertCompilation(DocumentUri.From(bicepFilePath), null, bicepFileContents, LanguageConstants.LanguageId);

            var bicepConfigDocumentUri = DocumentUri.FromFileSystemPath(bicepFilePath);

            if (saveBicepConfigFile)
            {
                string bicepConfigFilePath = FileHelper.SaveResultFile(TestContext, "bicepconfig.json", bicepConfigFileContents, testOutputPath);
                bicepConfigDocumentUri = DocumentUri.FromFileSystemPath(bicepConfigFilePath);
            }

            BicepConfigChangeHandler.RefreshCompilationOfSourceFilesInWorkspace(bicepCompilationManager, workspace);

            diagnostics = receivedParams?.Diagnostics;
        }
Esempio n. 3
0
        public void GetNonExistentCompilation_ShouldNotThrow()
        {
            var server = Repository.Create <ILanguageServerFacade>();

            var manager = new BicepCompilationManager(server.Object, new BicepCompilationProvider(TestResourceTypeProvider.Create(), CreateEmptyFileResolver()), new Workspace());

            var uri = DocumentUri.File(this.TestContext.TestName);

            manager.GetCompilation(uri).Should().BeNull();
        }
Esempio n. 4
0
        public void GetNonExistentCompilation_ShouldNotThrow()
        {
            var server = Repository.Create <ILanguageServer>();

            var manager = new BicepCompilationManager(server.Object, new BicepCompilationProvider());

            var uri = DocumentUri.File(this.TestContext !.TestName);

            manager.GetCompilation(uri).Should().BeNull();
        }
Esempio n. 5
0
        public void FatalException_ShouldProduceCorrectDiagnosticsAndClearThemWhenFileIsClosed()
        {
            PublishDiagnosticsParams?receivedParams = null;

            var document = CreateMockDocument(p => receivedParams = p);

            var server = CreateMockServer(document);

            var          provider        = Repository.Create <ICompilationProvider>();
            const string expectedMessage = "Internal bicep exception.";

            provider.Setup(m => m.Create(It.IsAny <IReadOnlyWorkspace>(), It.IsAny <DocumentUri>())).Throws(new InvalidOperationException(expectedMessage));

            var manager = new BicepCompilationManager(server.Object, provider.Object, new Workspace());

            const int version = 74;
            var       uri     = DocumentUri.File(this.TestContext.TestName);

            // upsert should fail because of the mock fatal exception
            manager.UpsertCompilation(uri, version, "fake");
            manager.GetCompilation(uri).Should().BeNull();

            // diagnostics should have been published once
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(version);
            receivedParams.Diagnostics.Should().HaveCount(1);

            var fatalDiagnostic = receivedParams.Diagnostics.Single();

            fatalDiagnostic.Message.Should().Be(expectedMessage);
            fatalDiagnostic.Severity.Should().Be(DiagnosticSeverity.Error);

            // reset counts
            document.Invocations.Clear();

            // close the compilation (even if it wasn't opened successfully)
            manager.CloseCompilation(uri);

            // diagnostics should have been published once
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            // 0 diagnostics expected
            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(0);
            receivedParams.Diagnostics.Should().BeEmpty();
        }
Esempio n. 6
0
        public void UpsertCompilation_ShouldUpsertSuccessfully()
        {
            PublishDiagnosticsParams?receivedParams = null;

            var document = CreateMockDocument(p => receivedParams = p);

            var server = CreateMockServer(document);

            var manager = new BicepCompilationManager(server.Object, new BicepCompilationProvider(TestResourceTypeProvider.Create(), CreateEmptyFileResolver()), new Workspace());

            const int version = 42;
            var       uri     = DocumentUri.File(this.TestContext.TestName);

            // first get should not return anything
            manager.GetCompilation(uri).Should().BeNull();

            // upsert the compilation
            manager.UpsertCompilation(uri, version, "hello");
            var upserted = manager.GetCompilation(uri);

            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            // there should have been 1 diagnostic
            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(version);
            receivedParams.Diagnostics.Should().NotBeNullOrEmpty();
            receivedParams.Diagnostics.Count().Should().Be(1);

            // reset tracked calls
            document.Invocations.Clear();

            // get again
            var actual = manager.GetCompilation(uri);

            actual.Should().NotBeNull();

            // should be the same object
            actual.Should().BeSameAs(upserted);

            // get should not have pushed diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Never);
        }
Esempio n. 7
0
        public void NormalUpsertAfterFatalException_ShouldReplaceDiagnostics()
        {
            PublishDiagnosticsParams?receivedParams = null;

            var document = CreateMockDocument(p => receivedParams = p);

            var server = CreateMockServer(document);

            var          provider        = Repository.Create <ICompilationProvider>();
            const string expectedMessage = "Internal bicep exception.";

            const int version = 74;
            var       uri     = DocumentUri.File(this.TestContext.TestName);

            // start by failing
            bool failUpsert = true;

            provider
            .Setup(m => m.Create(It.IsAny <IReadOnlyWorkspace>(), It.IsAny <DocumentUri>()))
            .Returns <IReadOnlyWorkspace, DocumentUri>((workspace, documentUri) => failUpsert ? throw new InvalidOperationException(expectedMessage) : new BicepCompilationProvider(TestResourceTypeProvider.Create(), CreateEmptyFileResolver()).Create(workspace, documentUri));

            var manager = new BicepCompilationManager(server.Object, provider.Object, new Workspace());


            // upsert should fail because of the mock fatal exception
            manager.UpsertCompilation(uri, version, "fake");
            manager.GetCompilation(uri).Should().BeNull();

            // diagnostics should have been published once
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(version);
            receivedParams.Diagnostics.Should().HaveCount(1);

            var fatalDiagnostic = receivedParams.Diagnostics.Single();

            fatalDiagnostic.Message.Should().Be(expectedMessage);
            fatalDiagnostic.Severity.Should().Be(DiagnosticSeverity.Error);

            // reset counts
            document.Invocations.Clear();

            // allow success
            failUpsert = false;

            // upsert should succeed because we allowed it
            manager.UpsertCompilation(uri, version, "fake\nfake\nfake\n");
            var upserted = manager.GetCompilation(uri);

            upserted.Should().NotBeNull();

            // new diagnostics should have been published once
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(version);
            receivedParams.Diagnostics.Should().HaveCount(3);

            // none of the messages should be our fatal message
            receivedParams.Diagnostics
            .Select(diag => diag.Message)
            .All(message => string.Equals(message, expectedMessage) == false)
            .Should().BeTrue();
        }
        public void UpsertCompilation_ShouldUpdateDiagnostics()
        {
            PublishDiagnosticsParams?receivedParams = null;

            var document = CreateMockDocument(p => receivedParams = p);

            var server = CreateMockServer(document);

            var manager = new BicepCompilationManager(server.Object, new BicepCompilationProvider(TestTypeHelper.CreateEmptyProvider(), CreateEmptyFileResolver()), new Workspace());

            const int version = 42;
            var       uri     = DocumentUri.File(this.TestContext.TestName);

            // first get should not return anything
            manager.GetCompilation(uri).Should().BeNull();

            // upsert the compilation
            manager.UpsertCompilation(uri, version, "hello");
            var firstUpserted = manager.GetCompilation(uri);

            // should have pushed out diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            // there should have been 1 diagnostic
            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(version);
            receivedParams.Diagnostics.Should().NotBeNullOrEmpty();
            receivedParams.Diagnostics.Count().Should().Be(1);

            // reset tracked calls
            document.Invocations.Clear();

            // get again
            var firstActual = manager.GetCompilation(uri);

            firstActual.Should().NotBeNull();

            // should be same as first upserted
            firstActual.Should().BeSameAs(firstUpserted);

            // upsert second one
            const int newVersion = version + 1;

            manager.UpsertCompilation(uri, newVersion, "hello\r\nthere\r\n");
            var secondUpserted = manager.GetCompilation(uri);

            secondUpserted.Should().NotBeNull();
            secondUpserted.Should().NotBeSameAs(firstUpserted);

            // should have pushed out new diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            // reset invocations
            document.Invocations.Clear();

            // there should have been 2 diagnostics
            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(newVersion);
            receivedParams.Diagnostics.Should().NotBeNullOrEmpty();
            receivedParams.Diagnostics.Count().Should().Be(2);

            // get latest
            var secondActual = manager.GetCompilation(uri);

            secondActual.Should().BeSameAs(secondUpserted);
        }
Esempio n. 9
0
        public void CloseAfterUpsert_ShouldClearDiagnostics()
        {
            PublishDiagnosticsParams?receivedParams = null;

            var document = CreateMockDocument(p => receivedParams = p);

            var server = CreateMockServer(document);

            var manager = new BicepCompilationManager(server.Object, new BicepCompilationProvider());

            const long version = 42;
            var        uri     = DocumentUri.File(this.TestContext !.TestName);

            // first get should not return anything
            manager.GetCompilation(uri).Should().BeNull();

            // upsert the compilation
            manager.UpsertCompilation(uri, version, "hello");

            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            // there should have been 1 diagnostic
            receivedParams.Should().NotBeNull();
            receivedParams !.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(version);
            receivedParams.Diagnostics.Should().NotBeNullOrEmpty();
            receivedParams.Diagnostics.Count().Should().Be(1);

            // reset tracked calls
            document.Invocations.Clear();

            // get again
            var actual = manager.GetCompilation(uri);

            actual.Should().NotBeNull();

            // get should not have pushed diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Never);

            // 2nd get should be the same
            manager.GetCompilation(uri).Should().BeSameAs(actual);

            // get should not have pushed diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Never);

            // close compilation
            manager.CloseCompilation(uri);

            // close should have cleared diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Once);

            // expect zero diagnostics and 0 version
            receivedParams.Should().NotBeNull();
            receivedParams.Uri.Should().Be(uri);
            receivedParams.Version.Should().Be(0);
            receivedParams.Diagnostics.Should().BeEmpty();

            // reset call counts
            document.Invocations.Clear();

            // get again
            manager.GetCompilation(uri).Should().BeNull();

            // get should not have pushed diagnostics
            document.Verify(m => m.SendNotification(It.IsAny <PublishDiagnosticsParams>()), Times.Never);
        }