@@ -686,6 +686,117 @@ describe("Tests for the run method of CodeAnalyzer", () => {
686686 expect ( violations [ 0 ] . getCodeLocations ( ) [ 0 ] . getEndColumn ( ) ) . toBeUndefined ( ) ;
687687 } ) ;
688688
689+ it ( "When an engine returns a fix with a code location file that does not exist, then an error is thrown" , async ( ) => {
690+ const badViolation : engApi . Violation = stubs . getSampleViolationWithFixes ( ) ;
691+ badViolation . fixes ! [ 0 ] . location . file = 'test/doesNotExist' ;
692+ stubEngine1 . resultsToReturn = { violations : [ badViolation ] } ;
693+ await expect ( codeAnalyzer . run ( selection , sampleRunOptions ) ) . rejects . toThrow (
694+ getMessage ( 'EngineReturnedViolationWithCodeLocationFileThatDoesNotExist' ,
695+ 'stubEngine1' , 'stub1RuleA' , path . resolve ( 'test' , 'doesNotExist' ) ) ) ;
696+ } ) ;
697+
698+ it ( "When an engine returns a fix with an invalid startLine, then an error is thrown" , async ( ) => {
699+ const badViolation : engApi . Violation = stubs . getSampleViolationWithFixes ( ) ;
700+ badViolation . fixes ! [ 0 ] . location . startLine = - 1 ;
701+ stubEngine1 . resultsToReturn = { violations : [ badViolation ] } ;
702+ await expect ( codeAnalyzer . run ( selection , sampleRunOptions ) ) . rejects . toThrow (
703+ getMessage ( 'EngineReturnedViolationWithCodeLocationWithInvalidLineOrColumn' ,
704+ 'stubEngine1' , 'stub1RuleA' , 'startLine' , - 1 ) ) ;
705+ } ) ;
706+
707+ it ( "When an engine returns a fix with endLine before startLine, then an error is thrown" , async ( ) => {
708+ const badViolation : engApi . Violation = stubs . getSampleViolationWithFixes ( ) ;
709+ badViolation . fixes ! [ 0 ] . location . startLine = 10 ;
710+ badViolation . fixes ! [ 0 ] . location . endLine = 5 ;
711+ stubEngine1 . resultsToReturn = { violations : [ badViolation ] } ;
712+ await expect ( codeAnalyzer . run ( selection , sampleRunOptions ) ) . rejects . toThrow (
713+ getMessage ( 'EngineReturnedViolationWithCodeLocationWithEndLineBeforeStartLine' ,
714+ 'stubEngine1' , 'stub1RuleA' , 5 , 10 ) ) ;
715+ } ) ;
716+
717+ it ( "When an engine returns a suggestion with a code location file that does not exist, then an error is thrown" , async ( ) => {
718+ const badViolation : engApi . Violation = stubs . getSampleViolationWithSuggestions ( ) ;
719+ badViolation . suggestions ! [ 0 ] . location . file = 'test/doesNotExist' ;
720+ stubEngine1 . resultsToReturn = { violations : [ badViolation ] } ;
721+ await expect ( codeAnalyzer . run ( selection , sampleRunOptions ) ) . rejects . toThrow (
722+ getMessage ( 'EngineReturnedViolationWithCodeLocationFileThatDoesNotExist' ,
723+ 'stubEngine1' , 'stub1RuleA' , path . resolve ( 'test' , 'doesNotExist' ) ) ) ;
724+ } ) ;
725+
726+ it ( "When an engine returns a suggestion with an invalid startColumn, then an error is thrown" , async ( ) => {
727+ const badViolation : engApi . Violation = stubs . getSampleViolationWithSuggestions ( ) ;
728+ badViolation . suggestions ! [ 0 ] . location . startColumn = 0 ;
729+ stubEngine1 . resultsToReturn = { violations : [ badViolation ] } ;
730+ await expect ( codeAnalyzer . run ( selection , sampleRunOptions ) ) . rejects . toThrow (
731+ getMessage ( 'EngineReturnedViolationWithCodeLocationWithInvalidLineOrColumn' ,
732+ 'stubEngine1' , 'stub1RuleA' , 'startColumn' , 0 ) ) ;
733+ } ) ;
734+
735+ it ( "When an engine returns a violation with fixes, then getFixes returns the correct data" , async ( ) => {
736+ stubEngine1 . resultsToReturn = { violations : [ stubs . getSampleViolationWithFixes ( ) ] } ;
737+ const overallResults : RunResults = await codeAnalyzer . run ( selection , sampleRunOptions ) ;
738+ const violations : Violation [ ] = overallResults . getViolations ( ) ;
739+ expect ( violations ) . toHaveLength ( 1 ) ;
740+
741+ const fixes = violations [ 0 ] . getFixes ( ) ;
742+ expect ( fixes ) . toHaveLength ( 1 ) ;
743+ expect ( fixes [ 0 ] . getFixedCode ( ) ) . toEqual ( 'const correctedValue = true;' ) ;
744+ expect ( fixes [ 0 ] . getLocation ( ) . getFile ( ) ) . toEqual ( path . resolve ( 'test/config.test.ts' ) ) ;
745+ expect ( fixes [ 0 ] . getLocation ( ) . getStartLine ( ) ) . toEqual ( 3 ) ;
746+ expect ( fixes [ 0 ] . getLocation ( ) . getStartColumn ( ) ) . toEqual ( 6 ) ;
747+ expect ( fixes [ 0 ] . getLocation ( ) . getEndLine ( ) ) . toEqual ( 3 ) ;
748+ expect ( fixes [ 0 ] . getLocation ( ) . getEndColumn ( ) ) . toEqual ( 20 ) ;
749+ } ) ;
750+
751+ it ( "When an engine returns a violation with suggestions, then getSuggestions returns the correct data" , async ( ) => {
752+ stubEngine1 . resultsToReturn = { violations : [ stubs . getSampleViolationWithSuggestions ( ) ] } ;
753+ const overallResults : RunResults = await codeAnalyzer . run ( selection , sampleRunOptions ) ;
754+ const violations : Violation [ ] = overallResults . getViolations ( ) ;
755+ expect ( violations ) . toHaveLength ( 1 ) ;
756+
757+ const suggestions = violations [ 0 ] . getSuggestions ( ) ;
758+ expect ( suggestions ) . toHaveLength ( 2 ) ;
759+ expect ( suggestions [ 0 ] . getMessage ( ) ) . toEqual ( 'Consider using a boolean literal instead' ) ;
760+ expect ( suggestions [ 0 ] . getLocation ( ) . getFile ( ) ) . toEqual ( path . resolve ( 'test/config.test.ts' ) ) ;
761+ expect ( suggestions [ 1 ] . getMessage ( ) ) . toEqual ( 'Consider removing this unused variable' ) ;
762+ } ) ;
763+
764+ it ( "When an engine returns a violation with both fixes and suggestions, then both are accessible" , async ( ) => {
765+ stubEngine1 . resultsToReturn = { violations : [ stubs . getSampleViolationWithFixesAndSuggestions ( ) ] } ;
766+ const overallResults : RunResults = await codeAnalyzer . run ( selection , sampleRunOptions ) ;
767+ const violations : Violation [ ] = overallResults . getViolations ( ) ;
768+ expect ( violations ) . toHaveLength ( 1 ) ;
769+ expect ( violations [ 0 ] . getFixes ( ) ) . toHaveLength ( 2 ) ;
770+ expect ( violations [ 0 ] . getSuggestions ( ) ) . toHaveLength ( 1 ) ;
771+ } ) ;
772+
773+ it ( "When an engine returns a violation without fixes or suggestions, then getFixes and getSuggestions return empty arrays" , async ( ) => {
774+ stubEngine1 . resultsToReturn = { violations : [ stubs . getSampleViolationForStub1RuleA ( ) ] } ;
775+ const overallResults : RunResults = await codeAnalyzer . run ( selection , sampleRunOptions ) ;
776+ const violations : Violation [ ] = overallResults . getViolations ( ) ;
777+ expect ( violations ) . toHaveLength ( 1 ) ;
778+ expect ( violations [ 0 ] . getFixes ( ) ) . toEqual ( [ ] ) ;
779+ expect ( violations [ 0 ] . getSuggestions ( ) ) . toEqual ( [ ] ) ;
780+ } ) ;
781+
782+ it ( "When includeFixes and includeSuggestions are specified in RunOptions, they are forwarded to the engine" , async ( ) => {
783+ stubEngine1 . resultsToReturn = { violations : [ stubs . getSampleViolationForStub1RuleA ( ) ] } ;
784+ await codeAnalyzer . run ( selection , {
785+ ...sampleRunOptions ,
786+ includeFixes : true ,
787+ includeSuggestions : true
788+ } ) ;
789+ expect ( stubEngine1 . runRulesCallHistory [ 0 ] . runOptions . includeFixes ) . toBe ( true ) ;
790+ expect ( stubEngine1 . runRulesCallHistory [ 0 ] . runOptions . includeSuggestions ) . toBe ( true ) ;
791+ } ) ;
792+
793+ it ( "When includeFixes and includeSuggestions are not specified in RunOptions, they are undefined in the engine run options" , async ( ) => {
794+ stubEngine1 . resultsToReturn = { violations : [ stubs . getSampleViolationForStub1RuleA ( ) ] } ;
795+ await codeAnalyzer . run ( selection , sampleRunOptions ) ;
796+ expect ( stubEngine1 . runRulesCallHistory [ 0 ] . runOptions . includeFixes ) . toBeUndefined ( ) ;
797+ expect ( stubEngine1 . runRulesCallHistory [ 0 ] . runOptions . includeSuggestions ) . toBeUndefined ( ) ;
798+ } ) ;
799+
689800 it ( "When an engine throws an exception when running, then a result is returned with a Critical violation of type UnexpectedError" , async ( ) => {
690801 codeAnalyzer = createCodeAnalyzer ( ) ;
691802 await codeAnalyzer . addEnginePlugin ( new stubs . ThrowingEnginePlugin ( ) ) ;
0 commit comments