Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(presets): extract generic platform preset fetch (#6467)
- Loading branch information
Showing
5 changed files
with
153 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { getName } from '../../../test/util'; | ||
import { Preset } from './common'; | ||
import { | ||
FetchPresetConfig, | ||
PRESET_DEP_NOT_FOUND, | ||
PRESET_NOT_FOUND, | ||
fetchPreset, | ||
} from './util'; | ||
|
||
const config: FetchPresetConfig = { | ||
pkgName: 'some/repo', | ||
filePreset: 'default', | ||
endpoint: 'endpoint', | ||
fetch: undefined, | ||
}; | ||
|
||
const fetch = jest.fn(() => Promise.resolve<Preset>({})); | ||
|
||
describe(getName(__filename), () => { | ||
beforeEach(() => { | ||
fetch.mockReset(); | ||
}); | ||
it('works', async () => { | ||
fetch.mockResolvedValue({ sub: { preset: { foo: true } } }); | ||
expect(await fetchPreset({ ...config, fetch })).toEqual({ | ||
sub: { preset: { foo: true } }, | ||
}); | ||
|
||
expect( | ||
await fetchPreset({ ...config, filePreset: 'some/sub', fetch }) | ||
).toEqual({ preset: { foo: true } }); | ||
|
||
expect( | ||
await fetchPreset({ ...config, filePreset: 'some/sub/preset', fetch }) | ||
).toEqual({ foo: true }); | ||
}); | ||
|
||
it('fails', async () => { | ||
fetch.mockRejectedValueOnce(new Error('fails')); | ||
await expect(fetchPreset({ ...config, fetch })).rejects.toThrow('fails'); | ||
}); | ||
|
||
it(PRESET_DEP_NOT_FOUND, async () => { | ||
fetch.mockResolvedValueOnce(null); | ||
await expect(fetchPreset({ ...config, fetch })).rejects.toThrow( | ||
PRESET_DEP_NOT_FOUND | ||
); | ||
|
||
fetch.mockRejectedValueOnce(new Error(PRESET_DEP_NOT_FOUND)); | ||
fetch.mockRejectedValueOnce(new Error(PRESET_DEP_NOT_FOUND)); | ||
await expect(fetchPreset({ ...config, fetch })).rejects.toThrow( | ||
PRESET_DEP_NOT_FOUND | ||
); | ||
}); | ||
|
||
it(PRESET_NOT_FOUND, async () => { | ||
fetch.mockResolvedValueOnce({}); | ||
await expect( | ||
fetchPreset({ ...config, filePreset: 'some/sub/preset', fetch }) | ||
).rejects.toThrow(PRESET_NOT_FOUND); | ||
|
||
fetch.mockResolvedValueOnce({ sub: {} }); | ||
await expect( | ||
fetchPreset({ ...config, filePreset: 'some/sub/preset', fetch }) | ||
).rejects.toThrow(PRESET_NOT_FOUND); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { logger } from '../../logger'; | ||
import { ensureTrailingSlash } from '../../util/url'; | ||
import { Preset } from './common'; | ||
|
||
export const PRESET_DEP_NOT_FOUND = 'dep not found'; | ||
export const PRESET_NOT_FOUND = 'preset not found'; | ||
|
||
export type PresetFetcher = ( | ||
repo: string, | ||
fileName: string, | ||
endpoint: string | ||
) => Promise<Preset>; | ||
|
||
export type FetchPresetConfig = { | ||
pkgName: string; | ||
filePreset: string; | ||
endpoint: string; | ||
fetch: PresetFetcher; | ||
}; | ||
|
||
export async function fetchPreset({ | ||
pkgName, | ||
filePreset, | ||
endpoint, | ||
fetch, | ||
}: FetchPresetConfig): Promise<Preset | undefined> { | ||
// eslint-disable-next-line no-param-reassign | ||
endpoint = ensureTrailingSlash(endpoint); | ||
const [fileName, presetName, subPresetName] = filePreset.split('/'); | ||
let jsonContent: any | undefined; | ||
if (fileName === 'default') { | ||
try { | ||
jsonContent = await fetch(pkgName, 'default.json', endpoint); | ||
} catch (err) { | ||
if (err.message !== PRESET_DEP_NOT_FOUND) { | ||
throw err; | ||
} | ||
logger.debug('default.json preset not found - trying renovate.json'); | ||
jsonContent = await fetch(pkgName, 'renovate.json', endpoint); | ||
} | ||
} else { | ||
jsonContent = await fetch(pkgName, `${fileName}.json`, endpoint); | ||
} | ||
|
||
if (!jsonContent) { | ||
throw new Error(PRESET_DEP_NOT_FOUND); | ||
} | ||
if (presetName) { | ||
const preset = jsonContent[presetName]; | ||
if (!preset) { | ||
throw new Error(PRESET_NOT_FOUND); | ||
} | ||
if (subPresetName) { | ||
const subPreset = preset[subPresetName]; | ||
if (!subPreset) { | ||
throw new Error(PRESET_NOT_FOUND); | ||
} | ||
return subPreset; | ||
} | ||
return preset; | ||
} | ||
return jsonContent; | ||
} |