Skip to content

Commit

Permalink
Fix auto schema version detection
Browse files Browse the repository at this point in the history
Fix and add unit tests for the following cases:
- when the component is not harvested or partially harvested
- update status once upon complete, to better handle multiple versions of harvest tool results
  • Loading branch information
qtomlinson committed Sep 25, 2024
1 parent a567ca8 commit 9aa0ec6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tools/integration/lib/harvestResultFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ class HarvestResultFetcher {
async fetchToolVersions(tools = defaultTools) {
const listHarvestResultApi = `${this.apiBaseUrl}/harvest/${this._coordinates}?form=list`
const harvestResultUrls = await this._fetch(listHarvestResultApi).then(r => r.json())
return tools.flatMap(tool =>
harvestResultUrls
return tools.flatMap(tool => {
const found = harvestResultUrls
.filter(url => url.includes(`/${tool}/`))
.map(url => url.substring(`${this._coordinates}/${tool}/`.length))
.map(version => [tool, version])
)
return found.length ? found : [[tool, '']]
})
}

async _pollForCompletion(poller) {
Expand Down Expand Up @@ -54,13 +55,14 @@ class HarvestResultFetcher {
async isHarvestComplete(toolVersions, startTime, statuses = new Map()) {
const harvestChecks = (toolVersions || []).map(async ([tool, toolVersion]) => {
const completed = statuses.get(tool)?.completed || (await this.isHarvestedbyTool(tool, toolVersion, startTime))
if (completed) statuses.set(tool, { toolVersion, completed })
if (completed && !statuses.get(tool)) statuses.set(tool, { toolVersion, completed })
return tool
})
return Promise.all(harvestChecks).then(tools => tools.every(tool => statuses.get(tool)?.completed))
}

async isHarvestedbyTool(tool, toolVersion, startTime = 0) {
if (!tool || !toolVersion) return false
const harvested = await this.fetchHarvestResult(tool, toolVersion)
if (!harvested._metadata) return false
const fetchedAt = new Date(harvested._metadata.fetchedAt)
Expand Down
74 changes: 74 additions & 0 deletions tools/integration/test/lib/harvesterResultFetcherTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ describe('HarvestResultFetcher', function () {
strictEqual(result, true)
})

it('should return false when tool version is not provided', async function () {
const result = await resultMonitor.isHarvestedbyTool('licensee', '')
strictEqual(result, false)
})

it('should detect when component is completely harvested', async function () {
sinon.stub(resultMonitor, 'fetchHarvestResult').resolves(metadata())
const result = await resultMonitor.isHarvestComplete(defaultToolChecks)
Expand Down Expand Up @@ -146,6 +151,17 @@ describe('HarvestResultFetcher', function () {
['scancode', '30.3.0']
])
})

it('should handle no result for tools', async function () {
const harvestResults = []
fetchStub.resolves(new Response(JSON.stringify(harvestResults)))
const toolVersions = await resultMonitor.fetchToolVersions()
deepStrictEqual(toolVersions, [
['licensee', ''],
['reuse', ''],
['scancode', '']
])
})
})

describe('pollForToolVersionsComplete', function () {
Expand Down Expand Up @@ -180,6 +196,64 @@ describe('HarvestResultFetcher', function () {
['scancode', '30.3.0']
])
})

it('should handle partially harvested results correctly', async function () {
sinon
.stub(resultMonitor, 'fetchToolVersions')
.onFirstCall()
.resolves([
['licensee', '9.14.0'],
['reuse', '3.2.2'],
['scancode', '']
])
.onSecondCall()
.resolves([
['licensee', '9.14.0'],
['reuse', '3.2.1'],
['reuse', '3.2.2'],
['scancode', '30.3.0']
])
sinon
.stub(resultMonitor, 'isHarvestedbyTool')
.withArgs('licensee', '9.14.0')
.resolves(true)
.withArgs('reuse', '3.2.1')
.resolves(true)
.withArgs('scancode', '30.3.0')
.resolves(true)
const toolVersions = await resultMonitor.pollForToolVersionsComplete(poller, Date.now())
deepStrictEqual(toolVersions, [
['licensee', '9.14.0'],
['reuse', '3.2.1'],
['scancode', '30.3.0']
])
})

it('should handle tool results with multiple schema versions correctly', async function () {
sinon.stub(resultMonitor, 'fetchToolVersions').resolves([
['licensee', '9.14.0'],
['reuse', '3.2.1'],
['reuse', '3.2.2'],
['scancode', '30.3.0']
])
sinon
.stub(resultMonitor, 'isHarvestedbyTool')
.withArgs('licensee', '9.14.0')
.onFirstCall()
.resolves(false)
.onSecondCall()
.resolves(true)
.withArgs('reuse', '3.2.1')
.resolves(true)
.withArgs('scancode', '30.3.0')
.resolves(true)
const toolVersions = await resultMonitor.pollForToolVersionsComplete(poller, Date.now())
deepStrictEqual(toolVersions, [
['reuse', '3.2.1'],
['scancode', '30.3.0'],
['licensee', '9.14.0']
])
})
})
})

Expand Down

0 comments on commit 9aa0ec6

Please sign in to comment.