refactor(dart): migrate to class based datasource (#10134)

This commit is contained in:
Jamie Magee 2021-05-24 21:42:14 -07:00 committed by GitHub
parent 94f05138b8
commit 7b0934592f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 56 deletions

View file

@ -2,7 +2,7 @@ import * as bitbucketTags from './bitbucket-tags';
import { CdnJsDatasource } from './cdnjs';
import { ClojureDatasource } from './clojure';
import * as crate from './crate';
import * as dart from './dart';
import { DartDatasource } from './dart';
import * as docker from './docker';
import * as galaxy from './galaxy';
import * as galaxyCollection from './galaxy-collection';
@ -39,7 +39,7 @@ api.set('bitbucket-tags', bitbucketTags);
api.set('cdnjs', new CdnJsDatasource());
api.set('clojure', new ClojureDatasource());
api.set('crate', crate);
api.set('dart', dart);
api.set('dart', new DartDatasource());
api.set('docker', docker);
api.set('galaxy', galaxy);
api.set('galaxy-collection', galaxyCollection);

View file

@ -1,7 +1,7 @@
import { getPkgReleases } from '..';
import * as httpMock from '../../../test/http-mock';
import { getName, loadJsonFixture } from '../../../test/util';
import { id as datasource } from '.';
import { DartDatasource } from '.';
const body = loadJsonFixture('shared_preferences.json');
@ -20,7 +20,10 @@ describe(getName(), () => {
it('returns null for empty result', async () => {
httpMock.scope(baseUrl).get('/non_sense').reply(200, null);
expect(
await getPkgReleases({ datasource, depName: 'non_sense' })
await getPkgReleases({
datasource: DartDatasource.id,
depName: 'non_sense',
})
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
@ -35,7 +38,7 @@ describe(getName(), () => {
.reply(200, withoutVersions);
expect(
await getPkgReleases({
datasource,
datasource: DartDatasource.id,
depName: 'shared_preferences',
})
).toBeNull();
@ -50,7 +53,7 @@ describe(getName(), () => {
.reply(200, withoutLatest);
expect(
await getPkgReleases({
datasource,
datasource: DartDatasource.id,
depName: 'shared_preferences',
})
).toBeNull();
@ -61,7 +64,7 @@ describe(getName(), () => {
httpMock.scope(baseUrl).get('/shared_preferences').reply(404);
expect(
await getPkgReleases({
datasource,
datasource: DartDatasource.id,
depName: 'shared_preferences',
})
).toBeNull();
@ -72,7 +75,7 @@ describe(getName(), () => {
let e;
try {
await getPkgReleases({
datasource,
datasource: DartDatasource.id,
depName: 'shared_preferences',
});
} catch (err) {
@ -86,7 +89,7 @@ describe(getName(), () => {
httpMock.scope(baseUrl).get('/shared_preferences').replyWithError('');
expect(
await getPkgReleases({
datasource,
datasource: DartDatasource.id,
depName: 'shared_preferences',
})
).toBeNull();
@ -95,7 +98,7 @@ describe(getName(), () => {
it('processes real data', async () => {
httpMock.scope(baseUrl).get('/shared_preferences').reply(200, body);
const res = await getPkgReleases({
datasource,
datasource: DartDatasource.id,
depName: 'shared_preferences',
});
expect(res).toMatchSnapshot();

View file

@ -1,15 +1,20 @@
import { ExternalHostError } from '../../types/errors/external-host-error';
import { Http, HttpResponse } from '../../util/http';
import { HttpResponse } from '../../util/http';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import type { DartResult } from './types';
export const id = 'dart';
export const defaultRegistryUrls = ['https://pub.dartlang.org/'];
export const customRegistrySupport = false;
export class DartDatasource extends Datasource {
static readonly id = 'dart';
const http = new Http(id);
constructor() {
super(DartDatasource.id);
}
export async function getReleases({
readonly customRegistrySupport = false;
readonly defaultRegistryUrls = ['https://pub.dartlang.org/'];
async getReleases({
lookupName,
registryUrl,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
@ -18,15 +23,9 @@ export async function getReleases({
let raw: HttpResponse<DartResult> = null;
try {
raw = await http.getJson<DartResult>(pkgUrl);
raw = await this.http.getJson<DartResult>(pkgUrl);
} catch (err) {
if (
err.statusCode === 429 ||
(err.statusCode >= 500 && err.statusCode < 600)
) {
throw new ExternalHostError(err);
}
throw err;
this.handleGenericErrors(err);
}
const body = raw?.body;
@ -52,6 +51,6 @@ export async function getReleases({
}
}
}
return result;
}
}

View file

@ -1,5 +1,5 @@
import { safeLoad } from 'js-yaml';
import * as datasourceDart from '../../datasource/dart';
import { DartDatasource } from '../../datasource/dart';
import { logger } from '../../logger';
import type { PackageDependency, PackageFile } from '../types';
@ -54,7 +54,7 @@ export function extractPackageFile(
if (deps.length) {
return {
packageFile,
datasource: datasourceDart.id,
datasource: DartDatasource.id,
deps,
};
}