diff --git a/lib/config/presets/local/common.spec.ts b/lib/config/presets/local/common.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..6bf9872b10c3b0a1540d3335a5f4da36503e74f9 --- /dev/null +++ b/lib/config/presets/local/common.spec.ts @@ -0,0 +1,48 @@ +import { platform } from '../../../../test/util'; +import { ExternalHostError } from '../../../types/errors/external-host-error'; +import { PRESET_DEP_NOT_FOUND } from '../util'; +import { fetchJSONFile, getPresetFromEndpoint } from './common'; + +describe('config/presets/local/common', () => { + describe('fetchJSONFile', () => { + it('throws for null', async () => { + platform.getRawFile.mockResolvedValueOnce(null); + + await expect(fetchJSONFile('some/repo', 'default.json')).rejects.toThrow( + PRESET_DEP_NOT_FOUND + ); + }); + + it('throws for ExternalHostError', async () => { + platform.getRawFile.mockRejectedValueOnce( + new ExternalHostError(new Error()) + ); + + await expect(fetchJSONFile('some/repo', 'default.json')).rejects.toThrow( + ExternalHostError + ); + }); + + it('throws for Error', async () => { + platform.getRawFile.mockRejectedValueOnce(new Error()); + + await expect(fetchJSONFile('some/repo', 'default.json')).rejects.toThrow( + PRESET_DEP_NOT_FOUND + ); + }); + }); + + describe('getPresetFromEndpoint', () => { + it('works', async () => { + platform.getRawFile.mockResolvedValueOnce('{}'); + expect( + await getPresetFromEndpoint( + 'some/repo', + 'default.json', + undefined, + 'dummy' + ) + ).toEqual({}); + }); + }); +}); diff --git a/lib/config/presets/local/common.ts b/lib/config/presets/local/common.ts index 3271595554cb9c8d32200ae88869c3f965e99e0d..7648fdd56f313ebff850082afd991ab7058d4b91 100644 --- a/lib/config/presets/local/common.ts +++ b/lib/config/presets/local/common.ts @@ -13,7 +13,6 @@ export async function fetchJSONFile( try { raw = await platform.getRawFile(fileName, repo); } catch (err) { - // istanbul ignore if: not testable with nock if (err instanceof ExternalHostError) { throw err; } @@ -26,8 +25,11 @@ export async function fetchJSONFile( throw new Error(PRESET_DEP_NOT_FOUND); } - // TODO: null check #7154 - return parsePreset(raw!); + if (!raw) { + throw new Error(PRESET_DEP_NOT_FOUND); + } + + return parsePreset(raw); } export function getPresetFromEndpoint( diff --git a/lib/modules/platform/azure/index.spec.ts b/lib/modules/platform/azure/index.spec.ts index e6a881cdb56a314c0f14e7910fe95a729b573053..b02867336a23367ed8ba92658b7443dd22ae7b1a 100644 --- a/lib/modules/platform/azure/index.spec.ts +++ b/lib/modules/platform/azure/index.spec.ts @@ -1,10 +1,12 @@ import { Readable } from 'stream'; import is from '@sindresorhus/is'; +import type { IGitApi } from 'azure-devops-node-api/GitApi'; import { GitPullRequestMergeStrategy, GitStatusState, PullRequestStatus, } from 'azure-devops-node-api/interfaces/GitInterfaces.js'; +import { partial } from '../../../../test/util'; import { REPOSITORY_ARCHIVED, REPOSITORY_NOT_FOUND, @@ -167,13 +169,13 @@ describe('modules/platform/azure/index', () => { if (is.string(args)) { return azure.initRepo({ repository: args, - } as any); + }); } return azure.initRepo({ repository: 'some/repo', ...args, - } as any); + }); } describe('initRepo', () => { @@ -1308,6 +1310,10 @@ describe('modules/platform/azure/index', () => { }); describe('getJsonFile()', () => { + beforeEach(async () => { + await initRepo(); + }); + it('returns file content', async () => { const data = { foo: 'bar' }; azureApi.gitApi.mockImplementationOnce( @@ -1396,5 +1402,15 @@ describe('modules/platform/azure/index', () => { expect(res).toEqual(data); expect(gitApiMock.getItemContent.mock.calls).toMatchSnapshot(); }); + + it('returns null', async () => { + azureApi.gitApi.mockResolvedValueOnce( + partial<IGitApi>({ + getRepositories: jest.fn(() => Promise.resolve([])), + }) + ); + const res = await azure.getJsonFile('file.json', 'foo/bar'); + expect(res).toBeNull(); + }); }); }); diff --git a/lib/modules/platform/azure/index.ts b/lib/modules/platform/azure/index.ts index 223288f16ffc31b7d971ad9218704ac2bfd4f5ff..3259fc07486a010228ab87340588755a3089334d 100644 --- a/lib/modules/platform/azure/index.ts +++ b/lib/modules/platform/azure/index.ts @@ -134,13 +134,16 @@ export async function getRawFile( repoId = config.repoId; } + if (!repoId) { + return null; + } + const versionDescriptor: GitVersionDescriptor = { version: branchOrTag, } as GitVersionDescriptor; const buf = await azureApiGit.getItemContent( - // TODO #7154 - repoId!, + repoId, fileName, undefined, undefined, @@ -161,8 +164,7 @@ export async function getJsonFile( branchOrTag?: string ): Promise<any | null> { const raw = await getRawFile(fileName, repoName, branchOrTag); - // TODO #7154 - return JSON5.parse(raw!); + return raw ? JSON5.parse(raw) : null; } export async function initRepo({