Skip to content

Commit f5d02d8

Browse files
committed
enable strictNullChecks and strictPropertyInitialization
Fix ~652 null-safety errors across source and test files.
1 parent cfd29b4 commit f5d02d8

17 files changed

Lines changed: 93 additions & 83 deletions

test/business/definitionServiceTest.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Definition Service', () => {
5454
})
5555

5656
it('stores new definitions', async () => {
57-
const { service, coordinates } = setup(createDefinition(null, null, ['foo']))
57+
const { service, coordinates } = setup(createDefinition(undefined, undefined, ['foo']))
5858
await service.get(coordinates)
5959
expect(service.definitionStore.store.calledOnce).to.be.true
6060
expect(service.search.store.notCalled).to.be.true
@@ -79,7 +79,7 @@ describe('Definition Service', () => {
7979
})
8080

8181
it('logs and harvest new definitions with empty tools', async () => {
82-
const { service, coordinates } = setup(createDefinition(null, null, []))
82+
const { service, coordinates } = setup(createDefinition(undefined, undefined, []))
8383
await service.get(coordinates)
8484
// expect(service.logger.info.calledOnce).to.be.true
8585
// expect(service.logger.info.getCall(0).args[0]).to.eq('definition not available')
@@ -88,7 +88,7 @@ describe('Definition Service', () => {
8888
})
8989

9090
it('logs and harvests new definitions with undefined tools', async () => {
91-
const { service, coordinates } = setup(createDefinition(null, null, undefined))
91+
const { service, coordinates } = setup(createDefinition(undefined, undefined, undefined))
9292
await service.get(coordinates)
9393
// expect(service.logger.info.calledOnce).to.be.true
9494
// expect(service.logger.info.getCall(0).args[0]).to.eq('definition not available')
@@ -104,7 +104,7 @@ describe('Definition Service', () => {
104104
files: [{ path: 'bar.txt', attributions: ['Copyright Bob'] }],
105105
described: { releaseDate: '2018-08-09' }
106106
}
107-
const { service, coordinates } = setup(raw, null, curation)
107+
const { service, coordinates } = setup(raw, undefined, curation)
108108
const definition = await service.compute(coordinates)
109109
expect(definition.described.score.total).to.eq(30)
110110
expect(definition.described.toolScore.total).to.eq(0)
@@ -229,7 +229,7 @@ describe('Definition Service', () => {
229229

230230
data.forEach((expected, coordinatesString) => {
231231
it(`should have source location for ${coordinatesString} package`, async () => {
232-
const { service, coordinates } = setup(createDefinition(null, null, []), coordinatesString)
232+
const { service, coordinates } = setup(createDefinition(undefined, undefined, []), coordinatesString)
233233
const definition = await service.compute(coordinates)
234234
expect(definition.described.sourceLocation).to.be.deep.equal(expected)
235235
})
@@ -332,7 +332,7 @@ describe('Definition Service', () => {
332332

333333
beforeEach(() => {
334334
autoCurate = sinon.stub().resolves()
335-
;({ service, coordinates } = setup(createDefinition(null, null, ['scancode/3.2.2'])))
335+
;({ service, coordinates } = setup(createDefinition(undefined, undefined, ['scancode/3.2.2'])))
336336
service.curationService = {
337337
apply: (_coordinates, _curationSpec, definition) => Promise.resolve(definition),
338338
autoCurate
@@ -409,7 +409,7 @@ describe('Definition Service', () => {
409409
const firstCompute = new Promise(resolve => {
410410
resolveFirstCompute = resolve
411411
})
412-
deferredDefinition = createDefinition(null, null, ['scancode/3.2.2'])
412+
deferredDefinition = createDefinition(undefined, undefined, ['scancode/3.2.2'])
413413
sinon
414414
.stub(service, '_computeAndStore')
415415
.onFirstCall()
@@ -649,7 +649,11 @@ describe('Integration test', () => {
649649

650650
context('with stale definitions', () => {
651651
it('recomputes a definition with the updated schema version', async () => {
652-
const staleDef = { ...createDefinition(null, null, ['foo']), _meta: { schemaVersion: '1.0.0' }, coordinates }
652+
const staleDef = {
653+
...createDefinition(undefined, undefined, ['foo']),
654+
_meta: { schemaVersion: '1.0.0' },
655+
coordinates
656+
}
653657
const { service, store } = setupServiceForUpgrade(staleDef, recomputeHandler)
654658
const result = await service.get(coordinates)
655659
expect(result._meta.schemaVersion).to.eq('1.7.0')
@@ -666,7 +670,11 @@ describe('Integration test', () => {
666670
queue = memoryQueue.upgrade()
667671
recomputeHandler = delayedFactory({ logger, queue: { upgrade: () => queue, compute: memoryQueue.compute } })
668672
await recomputeHandler.initialize()
669-
staleDef = { ...createDefinition(null, null, ['foo']), _meta: { schemaVersion: '1.0.0' }, coordinates }
673+
staleDef = {
674+
...createDefinition(undefined, undefined, ['foo']),
675+
_meta: { schemaVersion: '1.0.0' },
676+
coordinates
677+
}
670678
})
671679

672680
handleVersionedDefinition()
@@ -781,7 +789,7 @@ describe('Integration test', () => {
781789
})
782790

783791
it('returns placeholder when no definition exists, then returns real definition after queue processed', async () => {
784-
const computedDef = { ...createDefinition(null, null, ['clearlydefined']), coordinates }
792+
const computedDef = { ...createDefinition(undefined, undefined, ['clearlydefined']), coordinates }
785793
const { service, store } = setupServiceForUpgrade(computedDef, recomputeHandler)
786794
store.get.resolves(null) // no stored definition initially — triggers delayed compute path
787795

@@ -811,7 +819,7 @@ describe('Integration test', () => {
811819
let harvestService
812820

813821
it('deletes the tracked in progress harvest after definition is computed', async () => {
814-
;({ service, coordinates, harvestService } = setup(createDefinition(null, null, ['foo'])))
822+
;({ service, coordinates, harvestService } = setup(createDefinition(undefined, undefined, ['foo'])))
815823
harvestService.done = sinon.stub().resolves(true)
816824
await service.computeAndStore(coordinates)
817825
expect(harvestService.done.calledOnce).to.be.true
@@ -938,7 +946,7 @@ function setup(
938946
const curator = {
939947
get: () => Promise.resolve(curation),
940948
apply: (_coordinates: unknown, _curationSpec: unknown, definition: unknown) =>
941-
Promise.resolve(Curation.apply(definition, curation)),
949+
Promise.resolve(Curation.apply(definition as any, curation as any)),
942950
autoCurate: () => {
943951
return
944952
}
@@ -962,7 +970,7 @@ function setup(
962970
)
963971
service.logger = { info: sinon.stub(), debug: sinon.stub() }
964972
service._harvest = sinon.stub()
965-
const coordinates = EntityCoordinates.fromString(coordinateSpec || 'npm/npmjs/-/test/1.0')
973+
const coordinates = EntityCoordinates.fromString(coordinateSpec || 'npm/npmjs/-/test/1.0')!
966974
return { coordinates, service, harvestService }
967975
}
968976

test/business/suggestionServiceTest.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const expect = chai.expect
2020

2121
const testDir = dirname(fileURLToPath(import.meta.url))
2222

23-
const testCoordinates = EntityCoordinates.fromString('npm/npmjs/-/test/10.0')
23+
const testCoordinates = EntityCoordinates.fromString('npm/npmjs/-/test/10.0')!
2424

2525
describe('Suggestion Service', () => {
2626
it('gets suggestion for missing declared license', async () => {
@@ -99,7 +99,7 @@ describe('Suggestion Service', () => {
9999

100100
it('queries related definitions with namespace', async () => {
101101
const now = DateTime.now()
102-
const coordinates = EntityCoordinates.fromString('npm/npmjs/@scope/scope-test/1.0.0')
102+
const coordinates = EntityCoordinates.fromString('npm/npmjs/@scope/scope-test/1.0.0')!
103103
const definition = createDefinition(coordinates, now, null, files)
104104
const service = setup(definition, [])
105105
await service.get(coordinates)
@@ -135,8 +135,8 @@ function createModifiedDefinition(
135135
) {
136136
const newCoordinates = EntityCoordinates.fromObject({
137137
...coordinates,
138-
revision: `${coordinates.revision.split('.')[0] + amount}.0`
139-
})
138+
revision: `${coordinates.revision!.split('.')[0] + amount}.0`
139+
})!
140140
const newFiles = files.map(file => {
141141
return { ...file, license, attributions }
142142
})

test/business/summarizerTest.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Summary Service', () => {
2121
const raw = loadEvidence(coordSpec)
2222

2323
const summaryService = SummaryService({})
24-
const summaries = summaryService.summarizeAll(coords, raw as any)
24+
const summaries = summaryService.summarizeAll(coords!, raw as any)
2525
const scancodeSummary = summaries['scancode']['32.3.0']
2626
expect(scancodeSummary.licensed.declared).to.equal('Apache-2.0')
2727
const licenseeSummary = summaries['licensee']['9.14.0']

test/providers/curation/github.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('Github Curation Service', () => {
155155

156156
it('fails if definition exists', async () => {
157157
const { service } = setup()
158-
const gitHubService = createService(service)
158+
const gitHubService = createService(service as any)
159159
const contributionPatch = {
160160
contributionInfo: {
161161
summary: 'test',
@@ -184,9 +184,9 @@ describe('Github Curation Service', () => {
184184
sinon
185185
.stub(service, 'listAll')
186186
.callsFake(async () => [
187-
EntityCoordinates.fromObject({ type: 'npm', provider: 'npmjs', name: 'test', revision: '1.0' })
187+
EntityCoordinates.fromObject({ type: 'npm', provider: 'npmjs', name: 'test', revision: '1.0' })!
188188
])
189-
const gitHubService = createService(service)
189+
const gitHubService = createService(service as any)
190190
sinon.stub(gitHubService, '_writePatch').callsFake(() => Promise.resolve())
191191

192192
const contributionPatch = {
@@ -267,7 +267,7 @@ describe('Github Curation Service', () => {
267267
sinon
268268
.stub(service, 'listAll')
269269
.callsFake(async () => [
270-
EntityCoordinates.fromObject({ type: 'npm', provider: 'npmjs', name: 'test', revision: '1.0' })
270+
EntityCoordinates.fromObject({ type: 'npm', provider: 'npmjs', name: 'test', revision: '1.0' })!
271271
])
272272
sinon.stub(service, 'list').callsFake(async () => [
273273
'npm/npmjs/-/test/1.0', // curated revision
@@ -278,7 +278,7 @@ describe('Github Curation Service', () => {
278278
])
279279
sinon.stub(service, 'getStored').callsFake(async () => undefined as any)
280280

281-
const gitHubService = createService(service, licenseMatcher, harvestStore)
281+
const gitHubService = createService(service as any, licenseMatcher as any, harvestStore as any)
282282
sinon.stub(gitHubService, '_getPatchesFromMergedPullRequest').resolves([component])
283283
sinon.stub(gitHubService, '_writePatch').callsFake(() => Promise.resolve())
284284
sinon.stub(gitHubService, 'list').callsFake(async () => {
@@ -350,7 +350,7 @@ describe('Github Curation Service', () => {
350350
beforeEach(() => {
351351
const { service } = setup()
352352
sinon.stub(service, 'getStored').resolves({
353-
coordinates: EntityCoordinates.fromString('npm/npmjs/-/express/5.0.0')
353+
coordinates: EntityCoordinates.fromString('npm/npmjs/-/express/5.0.0')!
354354
})
355355
const licenseMatcher = {
356356
process: sinon.stub().callsFake(() => matcherResult)
@@ -361,7 +361,7 @@ describe('Github Curation Service', () => {
361361
const harvestStore = {
362362
getAll: sinon.stub().resolves({})
363363
}
364-
gitHubService = createService(service, licenseMatcher, harvestStore, {} as any, store as any)
364+
gitHubService = createService(service as any, licenseMatcher as any, harvestStore as any, {} as any, store as any)
365365
// TODO: Should not stub private functions and private properties
366366
sinon.stub(gitHubService, 'github').value({
367367
rest: { users: { get: sinon.stub() } }
@@ -426,7 +426,7 @@ describe('Github Curation Service', () => {
426426
const store = {
427427
list: sinon.stub().callsFake(() => curationsAndContributions)
428428
}
429-
gitHubService = createService(definitionService, licenseMatcher, harvestStore, {} as any, store as any)
429+
gitHubService = createService(definitionService as any, licenseMatcher as any, harvestStore as any, {} as any, store as any)
430430
gitHubService.github = {
431431
rest: {
432432
users: { get: () => ({ name: 'clearlydefined-bot' }) },

test/providers/curation/githubPRs.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ describe('Curation service pr events', () => {
200200

201201
it('gets null curation if blob does not exist', async () => {
202202
const service = await createService({
203-
geitStubOverride: () => {
203+
geitStubOverride: (() => {
204204
return {
205205
tree: ref => {
206206
expect(ref).to.eq('branch')
207207
return Promise.resolve()
208208
},
209209
blob: () => Promise.resolve(Buffer.alloc(0))
210210
}
211-
}
211+
}) as any
212212
})
213213
const coordinates = EntityCoordinates.fromString('npm/npmjs/-/test/1.0.0')
214214
const content = await service._getCurations(coordinates)
@@ -226,7 +226,7 @@ revisions:
226226
declared: MIT
227227
`
228228
const service = await createService({
229-
geitStubOverride: () => {
229+
geitStubOverride: (() => {
230230
return {
231231
tree: ref => {
232232
expect(ref).to.eq('branch')
@@ -245,7 +245,7 @@ revisions:
245245
return Promise.resolve(Buffer.from(theYaml))
246246
}
247247
}
248-
}
248+
}) as any
249249
})
250250
const coordinates = EntityCoordinates.fromString('npm/npmjs/-/test/1.0.0')
251251
const content = await service._getCurations(coordinates)
@@ -266,15 +266,15 @@ revisions:
266266

267267
it('getCurations should use pr ref', async () => {
268268
const service = await createService({
269-
geitStubOverride: () => {
269+
geitStubOverride: (() => {
270270
return {
271271
tree: ref => {
272272
expect(ref).to.eq('refs/pull/123/head')
273273
return Promise.resolve()
274274
},
275275
blob: () => Promise.resolve(Buffer.alloc(0))
276276
}
277-
}
277+
}) as any
278278
})
279279
const coordinates = EntityCoordinates.fromString('npm/npmjs/-/test/1.0.0')
280280
const content = await service._getCurations(coordinates, 123)

test/providers/harvest/throttling/listBasedFilterTest.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { createSilentLogger } from '../../../helpers/mockLogger.ts'
99
describe('ListBasedFilter', () => {
1010
const logger = createSilentLogger()
1111

12-
function createCoord(coordString) {
13-
return EntityCoordinates.fromString(coordString)
12+
function createCoord(coordString): EntityCoordinates {
13+
return EntityCoordinates.fromString(coordString)!
1414
}
1515

1616
describe('isBlocked', () => {
@@ -96,8 +96,8 @@ describe('ListBasedFilter', () => {
9696
it('should return false for null or undefined coordinates', () => {
9797
const filter = new ListBasedFilter({ blacklist: ['npm/npmjs/-/test'], logger })
9898

99-
expect(filter.isBlocked(null)).to.be.false
100-
expect(filter.isBlocked(undefined)).to.be.false
99+
expect(filter.isBlocked(null as any)).to.be.false
100+
expect(filter.isBlocked(undefined as any)).to.be.false
101101
})
102102
})
103103
})
@@ -111,7 +111,7 @@ describe('ListBasedFilter', () => {
111111
})
112112

113113
it('should log warnings for invalid coordinates', () => {
114-
const warnings = []
114+
const warnings: string[] = []
115115
const mockLogger = {
116116
...createSilentLogger(),
117117
warn: (msg: string) => warnings.push(msg)

test/providers/store/azblobDefinition.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('azblob Definition store', () => {
1414
}
1515
const store = createStore(data)
1616
try {
17-
await store.list(EntityCoordinates.fromString('npm/npmjs/-/error/4.6.0'))
17+
await store.list(EntityCoordinates.fromString('npm/npmjs/-/error/4.6.0')!)
1818
throw new Error('should have thrown error')
1919
} catch (error) {
2020
expect((error as Error).message).to.eq('test error')
@@ -27,7 +27,7 @@ describe('azblob Definition store', () => {
2727
'npm/npmjs/-/co/revision/4.6.1.json': { metadata: { id: 'npm/npmjs/-/co/4.6.1' } }
2828
}
2929
const store = createStore(data)
30-
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/bogus/4.6.0'))
30+
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/bogus/4.6.0')!)
3131
expect(result.length).to.eq(0)
3232
})
3333

@@ -37,7 +37,7 @@ describe('azblob Definition store', () => {
3737
'npm/npmjs/-/co/revision/4.6.1.json': { metadata: { id: 'npm/npmjs/-/co/4.6.1' } }
3838
}
3939
const store = createStore(data)
40-
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.0'))
40+
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.0')!)
4141
expect(result).to.deep.equalInAnyOrder(['npm/npmjs/-/co/4.6.0'])
4242
})
4343

@@ -47,7 +47,7 @@ describe('azblob Definition store', () => {
4747
'npm/npmjs/-/co/revision/4.6.1.json': { metadata: { id: 'npm/npmjs/-/Co/4.6.1' } }
4848
}
4949
const store = createStore(data)
50-
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.1'))
50+
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.1')!)
5151
expect(result).to.deep.equalInAnyOrder(['npm/npmjs/-/Co/4.6.1'])
5252
})
5353

@@ -57,7 +57,7 @@ describe('azblob Definition store', () => {
5757
'npm/npmjs/-/co/revision/4.6.1.json': { metadata: { id: 'npm/npmjs/-/Co/4.6.1' } }
5858
}
5959
const store = createStore(data)
60-
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/co'))
60+
const result = await store.list(EntityCoordinates.fromString('npm/npmjs/-/co')!)
6161
expect(result).to.deep.equalInAnyOrder(['npm/npmjs/-/Co/4.6.0', 'npm/npmjs/-/Co/4.6.1'])
6262
})
6363

@@ -71,14 +71,14 @@ describe('azblob Definition store', () => {
7171

7272
it('deletes a definition', async () => {
7373
const store = createStore()
74-
await store.delete(EntityCoordinates.fromString('npm/npmjs/-/foo/1.0'))
74+
await store.delete(EntityCoordinates.fromString('npm/npmjs/-/foo/1.0')!)
7575
expect((store as any).blobService.deleteBlob.callCount).to.eq(1)
7676
expect((store as any).blobService.deleteBlob.args[0][1]).to.eq('npm/npmjs/-/foo/revision/1.0.json')
7777
})
7878

7979
it('does not throw deleting missing definition', async () => {
8080
const store = createStore()
81-
await store.delete(EntityCoordinates.fromString('npm/npmjs/-/missing/1.0'))
81+
await store.delete(EntityCoordinates.fromString('npm/npmjs/-/missing/1.0')!)
8282
expect((store as any).blobService.deleteBlob.callCount).to.eq(1)
8383
expect((store as any).blobService.deleteBlob.args[0][1]).to.eq('npm/npmjs/-/missing/revision/1.0.json')
8484
})
@@ -90,13 +90,13 @@ describe('azblob Definition store', () => {
9090
'npm/npmjs/-/co/revision/4.6.2.json': createDefinitionJson('npm/npmjs/-/co/4.6.2')
9191
}
9292
const store = createStore(data)
93-
const result = await store.get(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.1'))
94-
expect(result.coordinates).to.deep.eq(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.1'))
93+
const result = await store.get(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.1')!)
94+
expect(result.coordinates).to.deep.eq(EntityCoordinates.fromString('npm/npmjs/-/co/4.6.1')!)
9595
})
9696
})
9797

9898
function createDefinition(coordinates) {
99-
return { coordinates: EntityCoordinates.fromString(coordinates) }
99+
return { coordinates: EntityCoordinates.fromString(coordinates)! }
100100
}
101101

102102
function createDefinitionJson(coordinates) {

0 commit comments

Comments
 (0)