Skip to content

Commit

Permalink
@W-14689540@: (Part 6) Analyze results, set outputs, and create viola…
Browse files Browse the repository at this point in the history
…tion objects
  • Loading branch information
stephen-carter-at-sf committed Jan 15, 2024
1 parent 9425c7b commit 4a4554b
Show file tree
Hide file tree
Showing 11 changed files with 1,100 additions and 24 deletions.
1 change: 1 addition & 0 deletions __tests__/data/sampleRunDfaResults.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions __tests__/data/sampleRunResults.json

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions __tests__/fakes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommandOutput, Dependencies } from '../src/dependencies'
import { EnvironmentVariables, Inputs } from '../src/types'
import { CommandExecutor } from '../src/commands'
import { Results, ResultsFactory, Violation, ViolationLocation } from '../src/results'

export class FakeDependencies implements Dependencies {
startGroupCallHistory: { name: string }[] = []
Expand Down Expand Up @@ -102,3 +103,58 @@ export class FakeCommandExecutor implements CommandExecutor {
return this.runCodeAnalyzerReturnValue
}
}

export class FakeResultsFactory implements ResultsFactory {
createResultsReturnValue: Results = new FakeResults()
createResultsCallHistory: { resultsFile: string; isDfa: boolean }[] = []
createResults(resultsFile: string, isDfa: boolean): Results {
this.createResultsCallHistory.push({ resultsFile, isDfa })
return this.createResultsReturnValue
}
}

export class FakeResults implements Results {
getSev1ViolationCountReturnValue = 1
getSev1ViolationCountCallCount = 0
getSev1ViolationCount(): number {
this.getSev1ViolationCountCallCount++
return this.getSev1ViolationCountReturnValue
}

getSev2ViolationCountReturnValue = 2
getSev2ViolationCountCallCount = 0
getSev2ViolationCount(): number {
this.getSev2ViolationCountCallCount++
return this.getSev2ViolationCountReturnValue
}

getSev3ViolationCountReturnValue = 3
getSev3ViolationCountCallCount = 0
getSev3ViolationCount(): number {
this.getSev3ViolationCountCallCount++
return this.getSev3ViolationCountReturnValue
}

getTotalViolationCountReturnValue = 6
getTotalViolationCountCallCount = 0
getTotalViolationCount(): number {
this.getTotalViolationCountCallCount++
return this.getTotalViolationCountReturnValue
}

getViolationsSortedBySeverityReturnValue: Violation[] = []
getViolationsSortedBySeverityCallCount = 0
getViolationsSortedBySeverity(): Violation[] {
this.getViolationsSortedBySeverityCallCount++
return this.getViolationsSortedBySeverityReturnValue
}
}

export class FakeViolationLocation implements ViolationLocation {
compareToReturnValue = 0
compareToCallHistory: { other: ViolationLocation }[] = []
compareTo(other: ViolationLocation): number {
this.compareToCallHistory.push({ other })
return this.compareToReturnValue
}
}
69 changes: 54 additions & 15 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import * as main from '../src/main'
import { FakeCommandExecutor, FakeDependencies } from './fakes'
import { FakeCommandExecutor, FakeDependencies, FakeResultsFactory } from './fakes'
import { Inputs } from '../src/types'
import { INTERNAL_OUTFILE, MESSAGE_FCNS, MESSAGES, MIN_SCANNER_VERSION_REQUIRED } from '../src/constants'

describe('main run Tests', () => {
let dependencies: FakeDependencies
let commandExecutor: FakeCommandExecutor
let resultsFactory: FakeResultsFactory

beforeEach(async () => {
dependencies = new FakeDependencies()
commandExecutor = new FakeCommandExecutor()
resultsFactory = new FakeResultsFactory()
})

it('Test default values', async () => {
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(commandExecutor.isSalesforceCliInstalledCallCount).toEqual(1)

Expand All @@ -27,14 +29,46 @@ describe('main run Tests', () => {
expect(dependencies.uploadArtifactCallHistory).toHaveLength(1)
expect(dependencies.uploadArtifactCallHistory).toContainEqual({
artifactName: 'code-analyzer-results',
artifactFiles: ['SalesforceCodeAnalyzerResults.json']
artifactFiles: [INTERNAL_OUTFILE]
})

expect(dependencies.setOutputCallHistory).toHaveLength(1)
expect(resultsFactory.createResultsCallHistory).toHaveLength(1)
expect(resultsFactory.createResultsCallHistory).toContainEqual({
resultsFile: INTERNAL_OUTFILE,
isDfa: false
})

expect(dependencies.setOutputCallHistory).toHaveLength(5)
expect(dependencies.setOutputCallHistory).toContainEqual({
name: 'exit-code',
value: '0'
})
expect(dependencies.setOutputCallHistory).toContainEqual({
name: 'num-violations',
value: '6'
})
expect(dependencies.setOutputCallHistory).toContainEqual({
name: 'num-sev1-violations',
value: '1'
})
expect(dependencies.setOutputCallHistory).toContainEqual({
name: 'num-sev2-violations',
value: '2'
})
expect(dependencies.setOutputCallHistory).toContainEqual({
name: 'num-sev3-violations',
value: '3'
})

expect(dependencies.infoCallHistory).toContainEqual({
infoMessage:
'outputs:\n' +
' exit-code: 0\n' +
' num-violations: 6\n' +
' num-sev1-violations: 1\n' +
' num-sev2-violations: 2\n' +
' num-sev3-violations: 3'
})

expect(dependencies.failCallHistory).toHaveLength(0)
})
Expand All @@ -45,7 +79,7 @@ describe('main run Tests', () => {
runArgs: '-o myFile.html --normalize-severity -t ./src',
resultsArtifactName: 'customArtifactName'
}
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(commandExecutor.isSalesforceCliInstalledCallCount).toEqual(1)

Expand All @@ -62,7 +96,12 @@ describe('main run Tests', () => {
artifactFiles: ['myFile.html']
})

expect(dependencies.setOutputCallHistory).toHaveLength(1)
expect(resultsFactory.createResultsCallHistory).toHaveLength(1)
expect(resultsFactory.createResultsCallHistory).toContainEqual({
resultsFile: INTERNAL_OUTFILE,
isDfa: true
})

expect(dependencies.setOutputCallHistory).toContainEqual({
name: 'exit-code',
value: '0'
Expand All @@ -73,7 +112,7 @@ describe('main run Tests', () => {

it('Test nonzero exit code from command call', async () => {
commandExecutor.runCodeAnalyzerReturnValue = 987
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(dependencies.setOutputCallHistory).toContainEqual({
name: 'exit-code',
Expand All @@ -90,7 +129,7 @@ describe('main run Tests', () => {
}
}
dependencies = new ThrowingDependencies()
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(commandExecutor.runCodeAnalyzerCallHistory).toHaveLength(0)
expect(dependencies.uploadArtifactCallHistory).toHaveLength(0)
Expand All @@ -102,7 +141,7 @@ describe('main run Tests', () => {

it('Test missing --normalize-severity from run arguments', async () => {
dependencies.getInputsReturnValue.runArgs = '--outfile results.xml'
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(commandExecutor.runCodeAnalyzerCallHistory).toHaveLength(0)
expect(dependencies.uploadArtifactCallHistory).toHaveLength(0)
Expand All @@ -114,7 +153,7 @@ describe('main run Tests', () => {

it('Test when Salesforce CLI is not already installed and we install it successfully', async () => {
commandExecutor.isSalesforceCliInstalledReturnValue = false
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(dependencies.warnCallHistory).toHaveLength(1)
expect(dependencies.warnCallHistory).toContainEqual({
Expand All @@ -128,7 +167,7 @@ describe('main run Tests', () => {
it('Test when Salesforce CLI is not already installed and we fail to install it', async () => {
commandExecutor.isSalesforceCliInstalledReturnValue = false
commandExecutor.installSalesforceCliReturnValue = false
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(dependencies.warnCallHistory).toHaveLength(1)
expect(dependencies.warnCallHistory).toContainEqual({
Expand All @@ -142,7 +181,7 @@ describe('main run Tests', () => {

it('Test when sfdx-scanner plugin is not already installed and we install it successfully', async () => {
commandExecutor.isMinimumScannerPluginInstalledReturnValue = false
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(dependencies.warnCallHistory).toHaveLength(1)
expect(dependencies.warnCallHistory).toContainEqual({
Expand All @@ -159,7 +198,7 @@ describe('main run Tests', () => {
it('Test when sfdx-scanner plugin is not already installed and we fail to install it', async () => {
commandExecutor.isMinimumScannerPluginInstalledReturnValue = false
commandExecutor.installScannerPluginReturnValue = false
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(dependencies.warnCallHistory).toHaveLength(1)
expect(dependencies.warnCallHistory).toContainEqual({
Expand All @@ -176,7 +215,7 @@ describe('main run Tests', () => {

it('Test when the internal outfile file does not exist after run then we fail', async () => {
dependencies.fileExistsReturnValue = false
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(commandExecutor.runCodeAnalyzerCallHistory).toHaveLength(1)
expect(dependencies.failCallHistory).toHaveLength(1)
Expand All @@ -192,7 +231,7 @@ describe('main run Tests', () => {
resultsArtifactName: 'customArtifactName'
}
dependencies.fileExistsReturnValue = false
await main.run(dependencies, commandExecutor)
await main.run(dependencies, commandExecutor, resultsFactory)

expect(commandExecutor.runCodeAnalyzerCallHistory).toHaveLength(1)
expect(dependencies.failCallHistory).toHaveLength(1)
Expand Down
Loading

0 comments on commit 4a4554b

Please sign in to comment.