Skip to content

Commit

Permalink
Add Result class
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Jan 16, 2025
1 parent 766d0d8 commit 41c4ee0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
66 changes: 66 additions & 0 deletions helpers/result.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export class Result {
/**
* For storing and sharing results.
*
* @param {string} repoOwner - The owner of the repository.
* @param {string} repoName - The name of the repository.
*/
constructor (repoOwner, repoName) {
if (!repoOwner) {
this.log('repoOwner must be provided', 'error')
throw new Error('repoOwner must be provided')
}
if (!repoName) {
this.log('repoName must be provided', 'error')
throw new Error('repoName must be provided')
}
this.repoOwner = repoOwner
this.repoName = repoName
this.builtByGovernment = false
this.isPrototype = false
this.updatedAt = ''
this.createdAt = ''
this.directDependencies = []
this.indirectDependencies = []
this.errorsThrown = []
this.unknownLockFileType = false
this.isValid = false

// Non returned values
this.latestCommitSHA = ''
this.repoTree = []
}

validate () {
let valid = true

if (this.directDependencies.length === 0 && this.indirectDependencies.length === 0 && !this.unknownLockFileType) {
console.log('Result validation failed: No dependencies found, and no unknown lock file type')
valid = false
}

if (this.errorsThrown.length > 0) {
console.log('Result validation failed: Errors were thrown during analysis')
valid = false
}

return valid
}

getResult () {
return {
repoOwner: this.repoOwner,
repoName: this.repoName,
builtByGovernment: this.builtByGovernment,
updatedAt: this.updatedAt,
createdAt: this.createdAt,
directDependencies: this.directDependencies,
isPrototype: this.isPrototype,
isIndirect: this.directDependencies.length === 0,
indirectDependencies: this.indirectDependencies,
errorsThrown: this.errorsThrown,
unknownLockFileType: this.unknownLockFileType,
isValid: this.validate()
}
}
}
17 changes: 17 additions & 0 deletions helpers/result.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, it, expect } from 'vitest'
import { Result } from './result.mjs'

describe('Result', () => {
describe('validate', () => {
it('should return false if no dependencies and no unknown lock file type', () => {
const result = new Result('owner', 'repo')
expect(result.validate()).toBe(false)
})

it('should return false if errors were thrown during analysis', () => {
const result = new Result('owner', 'repo')
result.errorsThrown.push(new Error('Test error'))
expect(result.validate()).toBe(false)
})
})
})

0 comments on commit 41c4ee0

Please sign in to comment.