@@ -651,6 +651,166 @@ void Execute(Mocker Mocks)
651651 Assert . Equal ( DiagnosticIds . PreferTypedServiceProviderHelpers , diagnostic . Id ) ;
652652 }
653653
654+ [ Fact ]
655+ public async Task ServiceProviderShimAnalyzer_ShouldReportAndFix_FunctionContextInstanceServicesReturnsUsage ( )
656+ {
657+ const string SOURCE = @"
658+ using System;
659+ using FastMoq;
660+ using FastMoq.Providers.MoqProvider;
661+
662+ namespace Microsoft.Azure.Functions.Worker
663+ {
664+ abstract class FunctionContext
665+ {
666+ public virtual IServiceProvider InstanceServices { get; set; }
667+ }
668+ }
669+
670+ class Sample
671+ {
672+ void Execute(Mocker Mocks, IServiceProvider provider)
673+ {
674+ var context = Mocks.GetOrCreateMock<Microsoft.Azure.Functions.Worker.FunctionContext>();
675+ context.Setup(x => x.InstanceServices).Returns(provider);
676+ }
677+ }" ;
678+
679+ var diagnostics = await AnalyzerTestHelpers . GetDiagnosticsAsync ( SOURCE , new ServiceProviderShimAnalyzer ( ) ) ;
680+ var diagnostic = Assert . Single ( diagnostics . Where ( item => item . Id == DiagnosticIds . PreferTypedServiceProviderHelpers ) ) ;
681+ Assert . Equal ( DiagnosticIds . PreferTypedServiceProviderHelpers , diagnostic . Id ) ;
682+
683+ var fixedSource = await AnalyzerTestHelpers . ApplyCodeFixAsync (
684+ SOURCE ,
685+ new ServiceProviderShimAnalyzer ( ) ,
686+ codeFixProvider ,
687+ DiagnosticIds . PreferTypedServiceProviderHelpers ,
688+ includeAzureFunctionsHelpers : true ) ;
689+ var expected = AnalyzerTestHelpers . NormalizeCode ( @"
690+ using System;
691+ using FastMoq;
692+ using FastMoq.Providers.MoqProvider;
693+ using FastMoq.AzureFunctions.Extensions;
694+
695+ namespace Microsoft.Azure.Functions.Worker
696+ {
697+ abstract class FunctionContext
698+ {
699+ public virtual IServiceProvider InstanceServices { get; set; }
700+ }
701+ }
702+
703+ class Sample
704+ {
705+ void Execute(Mocker Mocks, IServiceProvider provider)
706+ {
707+ var context = Mocks.GetOrCreateMock<Microsoft.Azure.Functions.Worker.FunctionContext>();
708+ context.AddFunctionContextInstanceServices(provider);
709+ }
710+ }" ) ;
711+
712+ Assert . Equal ( expected , fixedSource ) ;
713+ }
714+
715+ [ Fact ]
716+ public async Task ServiceProviderShimAnalyzer_ShouldReportButNotOfferFix_WhenFunctionContextHelperPackageIsUnavailable ( )
717+ {
718+ const string SOURCE = @"
719+ using System;
720+ using FastMoq;
721+ using FastMoq.Providers.MoqProvider;
722+
723+ namespace Microsoft.Azure.Functions.Worker
724+ {
725+ abstract class FunctionContext
726+ {
727+ public virtual IServiceProvider InstanceServices { get; set; }
728+ }
729+ }
730+
731+ class Sample
732+ {
733+ void Execute(Mocker Mocks, IServiceProvider provider)
734+ {
735+ var context = Mocks.GetOrCreateMock<Microsoft.Azure.Functions.Worker.FunctionContext>();
736+ context.SetupGet(x => x.InstanceServices).Returns(provider);
737+ }
738+ }" ;
739+
740+ var diagnostics = await AnalyzerTestHelpers . GetDiagnosticsAsync ( SOURCE , new ServiceProviderShimAnalyzer ( ) ) ;
741+ var diagnostic = Assert . Single ( diagnostics . Where ( item => item . Id == DiagnosticIds . PreferTypedServiceProviderHelpers ) ) ;
742+ Assert . Equal ( DiagnosticIds . PreferTypedServiceProviderHelpers , diagnostic . Id ) ;
743+
744+ var codeFixTitles = await AnalyzerTestHelpers . GetCodeFixTitlesAsync (
745+ SOURCE ,
746+ new ServiceProviderShimAnalyzer ( ) ,
747+ codeFixProvider ,
748+ DiagnosticIds . PreferTypedServiceProviderHelpers ) ;
749+
750+ Assert . Empty ( codeFixTitles ) ;
751+ }
752+
753+ [ Fact ]
754+ public async Task ServiceProviderShimAnalyzer_ShouldReportAndFix_FunctionContextInstanceServicesSetupPropertyUsage ( )
755+ {
756+ const string SOURCE = @"
757+ using System;
758+ using FastMoq;
759+ using FastMoq.Providers.MoqProvider;
760+
761+ namespace Microsoft.Azure.Functions.Worker
762+ {
763+ abstract class FunctionContext
764+ {
765+ public virtual IServiceProvider InstanceServices { get; set; }
766+ }
767+ }
768+
769+ class Sample
770+ {
771+ void Execute(Mocker Mocks, IServiceProvider provider)
772+ {
773+ var context = Mocks.GetOrCreateMock<Microsoft.Azure.Functions.Worker.FunctionContext>();
774+ context.AsMoq().SetupProperty(x => x.InstanceServices, provider);
775+ }
776+ }" ;
777+
778+ var diagnostics = await AnalyzerTestHelpers . GetDiagnosticsAsync ( SOURCE , new ServiceProviderShimAnalyzer ( ) ) ;
779+ var diagnostic = Assert . Single ( diagnostics . Where ( item => item . Id == DiagnosticIds . PreferTypedServiceProviderHelpers ) ) ;
780+ Assert . Equal ( DiagnosticIds . PreferTypedServiceProviderHelpers , diagnostic . Id ) ;
781+
782+ var fixedSource = await AnalyzerTestHelpers . ApplyCodeFixAsync (
783+ SOURCE ,
784+ new ServiceProviderShimAnalyzer ( ) ,
785+ codeFixProvider ,
786+ DiagnosticIds . PreferTypedServiceProviderHelpers ,
787+ includeAzureFunctionsHelpers : true ) ;
788+ var expected = AnalyzerTestHelpers . NormalizeCode ( @"
789+ using System;
790+ using FastMoq;
791+ using FastMoq.Providers.MoqProvider;
792+ using FastMoq.AzureFunctions.Extensions;
793+
794+ namespace Microsoft.Azure.Functions.Worker
795+ {
796+ abstract class FunctionContext
797+ {
798+ public virtual IServiceProvider InstanceServices { get; set; }
799+ }
800+ }
801+
802+ class Sample
803+ {
804+ void Execute(Mocker Mocks, IServiceProvider provider)
805+ {
806+ var context = Mocks.GetOrCreateMock<Microsoft.Azure.Functions.Worker.FunctionContext>();
807+ context.AddFunctionContextInstanceServices(provider);
808+ }
809+ }" ) ;
810+
811+ Assert . Equal ( expected , fixedSource ) ;
812+ }
813+
654814 [ Fact ]
655815 public async Task KnownTypeAuthoringAnalyzer_ShouldReport_WhenContextAwareAddTypeOverloadIsUsed ( )
656816 {
0 commit comments