Skip to content

Commit

Permalink
fix(presets): handle null values from azure (#17236)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Aug 17, 2022
1 parent 551a40c commit 2525f71
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
48 changes: 48 additions & 0 deletions 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({});
});
});
});
8 changes: 5 additions & 3 deletions lib/config/presets/local/common.ts
Expand Up @@ -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;
}
Expand All @@ -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(
Expand Down
20 changes: 18 additions & 2 deletions 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,
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
});
});
});
10 changes: 6 additions & 4 deletions lib/modules/platform/azure/index.ts
Expand Up @@ -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,
Expand All @@ -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({
Expand Down

0 comments on commit 2525f71

Please sign in to comment.