Skip to content

Commit

Permalink
refactor(maven): Use valid timestamps in testing code (#8700)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
zharinov and viceice committed Feb 15, 2021
1 parent 90087e2 commit 37a751d
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions lib/datasource/maven/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ const config = {
datasource,
};

function timestamp(version: string): string {
const millis = (version.replace(/[^1-9]/g, '') + '000').slice(0, 3);
return `2020-01-01T00:00:00.${millis}Z`;
}

function generateReleases(versions: string[], ts = false): Release[] {
return versions.map((version) => {
if (ts) {
const releaseTimestamp = timestamp(version);
return { version, releaseTimestamp };
}
return { version };
});
}

describe('datasource/maven', () => {
beforeEach(() => {
hostRules.add({
Expand Down Expand Up @@ -95,21 +110,17 @@ describe('datasource/maven', () => {
const path = `/maven2/mysql/mysql-connector-java/${v}/mysql-connector-java-${v}.pom`;
nock('https://repo.maven.apache.org')
.head(path)
.reply(status, '', { 'Last-Modified': `good timestamp for ${v}` });
.reply(status, '', { 'Last-Modified': timestamp(v) });
nock('http://frontend_for_private_s3_repository')
.head(path)
.reply(status, '', { 'Last-Modified': `bad timestamp for ${v}` });
.reply(status, '', { 'Last-Modified': timestamp(v) });
});
});

afterEach(() => {
nock.enableNetConnect();
});

function generateReleases(versions: string[]): Release[] {
return versions.map((v) => ({ version: v }));
}

describe('getReleases', () => {
it('should return empty if library is not found', async () => {
const releases = await getPkgReleases({
Expand Down Expand Up @@ -166,12 +177,7 @@ describe('datasource/maven', () => {
depName: 'mysql:mysql-connector-java',
registryUrls: ['https://repo.maven.apache.org/maven2/'],
});
expect(releases.releases).toEqual(
generateReleases(MYSQL_VERSIONS).map(({ version }) => ({
version,
releaseTimestamp: `good timestamp for ${version}`,
}))
);
expect(releases.releases).toEqual(generateReleases(MYSQL_VERSIONS, true));
});

it('should return all versions of a specific library if a repository fails', async () => {
Expand All @@ -186,12 +192,7 @@ describe('datasource/maven', () => {
'http://empty_repo',
],
});
expect(releases.releases).toEqual(
generateReleases(MYSQL_VERSIONS).map(({ version }) => ({
version,
releaseTimestamp: `good timestamp for ${version}`,
}))
);
expect(releases.releases).toEqual(generateReleases(MYSQL_VERSIONS, true));
});

it('should throw external-host-error if default maven repo fails', async () => {
Expand Down Expand Up @@ -220,12 +221,7 @@ describe('datasource/maven', () => {
'ftp://protocol_error_repo',
],
});
expect(releases.releases).toEqual(
generateReleases(MYSQL_VERSIONS).map(({ version }) => ({
version,
releaseTimestamp: `good timestamp for ${version}`,
}))
);
expect(releases.releases).toEqual(generateReleases(MYSQL_VERSIONS, true));
});

it('should return all versions of a specific library if a repository fails because invalid metadata file is found in another repository', async () => {
Expand All @@ -250,12 +246,7 @@ describe('datasource/maven', () => {
'http://invalid_metadata_repo/maven2/',
],
});
expect(releases.releases).toEqual(
generateReleases(MYSQL_VERSIONS).map(({ version }) => ({
version,
releaseTimestamp: `good timestamp for ${version}`,
}))
);
expect(releases.releases).toEqual(generateReleases(MYSQL_VERSIONS, true));
});

it('should return all versions of a specific library if a repository fails because a metadata file is not xml', async () => {
Expand All @@ -273,12 +264,7 @@ describe('datasource/maven', () => {
'http://invalid_metadata_repo/maven2/',
],
});
expect(releases.releases).toEqual(
generateReleases(MYSQL_VERSIONS).map(({ version }) => ({
version,
releaseTimestamp: `good timestamp for ${version}`,
}))
);
expect(releases.releases).toEqual(generateReleases(MYSQL_VERSIONS, true));
});

it('should return all versions of a specific library if a repository does not end with /', async () => {
Expand Down

0 comments on commit 37a751d

Please sign in to comment.