public async Task ApplyProjectBuildAsync_WhenDesignTimeBuildFails_SetsLastDesignTimeBuildSucceededToFalse()
        {
            var applyChangesToWorkspace = CreateInitializedInstance(out var context);

            context.LastDesignTimeBuildSucceeded = true;

            var buildSnapshot = IProjectBuildSnapshotFactory.Create();

            var update = IProjectVersionedValueFactory.FromJson(
                @"{
   ""ProjectChanges"": {
        ""CompilerCommandLineArgs"": {
            ""Difference"": { 
                ""AnyChanges"": true
            },
            ""After"": { 
                ""EvaluationSucceeded"": false
            }
        }
    }
}");

            await applyChangesToWorkspace.ApplyProjectBuildAsync(update, buildSnapshot, new ContextState(), CancellationToken.None);

            Assert.False(context.LastDesignTimeBuildSucceeded);
        }
        public async Task ApplyProjectBuildAsync_IgnoresEvaluationHandlers()
        {
            int callCount = 0;
            var handler   = IEvaluationHandlerFactory.ImplementHandle("RuleName", (version, description, state, logger) => { callCount++; });

            var applyChangesToWorkspace = CreateInitializedInstance(handlers: new[] { handler });

            var buildSnapshot = IProjectBuildSnapshotFactory.Create();
            var update        = IProjectVersionedValueFactory.FromJson(
                @"{
   ""ProjectChanges"": {
        ""CompilerCommandLineArgs"": {
            ""Difference"": { 
                ""AnyChanges"": true
            },
        },
        ""RuleName"": {
            ""Difference"": { 
                ""AnyChanges"": true
            },
        }
    }
}");
            await applyChangesToWorkspace.ApplyProjectBuildAsync(update, buildSnapshot, new ContextState(isActiveEditorContext : true, isActiveConfiguration : false), CancellationToken.None);

            Assert.Equal(0, callCount);
        }
        public async Task ApplyProjectBuildAsync_ParseCommandLineAndCallsHandler()
        {
            (IComparable version, BuildOptions added, BuildOptions removed, ContextState state, IProjectLogger logger)result = default;

            var handler = ICommandLineHandlerFactory.ImplementHandle((version, added, removed, state, logger) =>
            {
                result = (version, added, removed, state, logger);
            });

            var parser = ICommandLineParserServiceFactory.CreateCSharp();

            var applyChangesToWorkspace = CreateInitializedInstance(commandLineParser: parser, handlers: new[] { handler });

            var buildSnapshot = IProjectBuildSnapshotFactory.Create();
            var update        = IProjectVersionedValueFactory.FromJson(version: 2,
                                                                       @"{
   ""ProjectChanges"": {
        ""CompilerCommandLineArgs"": {
            ""Difference"": { 
                ""AnyChanges"": true,
                ""AddedItems"" : [ ""/reference:Added.dll"" ],
                ""RemovedItems"" : [ ""/reference:Removed.dll"" ]
            }
        }
    }
}");
            await applyChangesToWorkspace.ApplyProjectBuildAsync(update, buildSnapshot, new ContextState(isActiveEditorContext : true, isActiveConfiguration : false), CancellationToken.None);

            Assert.Equal(2, result.version);
            Assert.True(result.state.IsActiveEditorContext);
            Assert.NotNull(result.logger);

            Assert.Single(result.added !.MetadataReferences.Select(r => r.Reference), "Added.dll");
            Assert.Single(result.removed !.MetadataReferences.Select(r => r.Reference), "Removed.dll");
        }
        public async Task ApplyProjectBuildAsync_WhenNotInitialized_ThrowsInvalidOperation()
        {
            var applyChangesToWorkspace = CreateInstance();

            var update        = Mock.Of <IProjectVersionedValue <IProjectSubscriptionUpdate> >();
            var buildSnapshot = IProjectBuildSnapshotFactory.Create();

            await Assert.ThrowsAsync <InvalidOperationException>(() =>
            {
                return(applyChangesToWorkspace.ApplyProjectBuildAsync(update, buildSnapshot, new ContextState(), CancellationToken.None));
            });
        }
        public async Task ApplyProjectBuildAsync_WhenNoCompilerCommandLineArgsRuleChanges_DoesNotCallHandler()
        {
            int callCount = 0;
            var handler   = ICommandLineHandlerFactory.ImplementHandle((version, added, removed, state, logger) => { callCount++; });

            var applyChangesToWorkspace = CreateInitializedInstance(handlers: new[] { handler });

            var buildSnapshot = IProjectBuildSnapshotFactory.Create();
            var update        = IProjectVersionedValueFactory.FromJson(
                @"{
   ""ProjectChanges"": {
        ""CompilerCommandLineArgs"": {
            ""Difference"": { 
                ""AnyChanges"": false
            },
        }
    }
}");

            await applyChangesToWorkspace.ApplyProjectBuildAsync(update, buildSnapshot, new ContextState(), CancellationToken.None);

            Assert.Equal(0, callCount);
        }
        public async Task ApplyProjectBuildAsync_WhenCancellationTokenCancelled_StopsProcessingHandlersAndThrowOperationCanceled()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var handler1 = ICommandLineHandlerFactory.ImplementHandle((version, added, removed, state, logger) =>
            {
                cancellationTokenSource.Cancel();
            });

            int callCount = 0;
            var handler2  = ICommandLineHandlerFactory.ImplementHandle((version, added, removed, state, logger) =>
            {
                callCount++;
            });

            var applyChangesToWorkspace = CreateInitializedInstance(handlers: new[] { handler1, handler2 });

            var buildSnapshot = IProjectBuildSnapshotFactory.Create();
            var update        = IProjectVersionedValueFactory.FromJson(
                @"{
   ""ProjectChanges"": {
        ""CompilerCommandLineArgs"": {
            ""Difference"": { 
                ""AnyChanges"": true
            },
        }
    }
}");
            await Assert.ThrowsAsync <OperationCanceledException>(() =>
            {
                return(applyChangesToWorkspace.ApplyProjectBuildAsync(update, buildSnapshot, new ContextState(isActiveEditorContext: true, isActiveConfiguration: false), cancellationTokenSource.Token));
            });

            Assert.True(cancellationTokenSource.IsCancellationRequested);
            Assert.Equal(0, callCount);
        }