Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(sbt-plugin): Fix HTTP mocks #31708

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 164 additions & 95 deletions lib/modules/datasource/sbt-plugin/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,127 @@ describe('modules/datasource/sbt-plugin/index', () => {
});

describe('getPkgReleases', () => {
beforeEach(() => {
it('returns null in case of errors', async () => {
httpMock
.scope('https://failed_repo/maven/')
.get('/org/scalatest/')
.reply(404)
.get('/org/scalatest/scalatest/')
.reply(404)
.get('/org.scalatest/')
.reply(404)
.get('/org.scalatest/scalatest/')
.reply(404);

httpMock
.scope('https://failed_repo')
.get('/maven/org/scalatest/')
.scope('https://repo.maven.apache.org/maven2/')
.get('/org/scalatest/')
.reply(404)
.get('/org/scalatest/scalaz/')
.reply(404);

httpMock
.scope('https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/')
.get('/org.scalatest/scalaz/')
.reply(404)
.get('/org.scalatest/')
.reply(404)
.get('/org/scalatest/scalaz/')
.reply(404)
.get('/org/scalatest/')
.reply(404);

expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.scalatest:scalatest',
registryUrls: ['https://failed_repo/maven'],
}),
).toBeNull();
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.scalatest:scalaz',
registryUrls: [],
}),
).toBeNull();
});

it('fetches sbt plugins', async () => {
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/')
.scope('https://repo.maven.apache.org/maven2/')
.get('/org/foundweekends/sbt-bintray/')
.reply(
200,
codeBlock`
<a href="../">../</a>
<a href="scalatest/">scalatest/</a>
<a href="scalatest_2.12/">scalatest_2.12/</a>
<a href="scalatest_sjs2.12/">scalatest_sjs2.12/</a>
<a href="scalatest_native2.12/">scalatest_native2.12/</a>
<html>
<head> </head>
<body>
<pre><a href="../">../</a></pre>
<pre><a href="scala_2.12/">scala_2.12/</a></pre>
</body>
</html>
`,
);
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/scalatest/')
)
.get('/org/foundweekends/sbt-bintray/scala_2.12/')
.reply(
200,
codeBlock`
<html>
<head> </head>
<body>
<pre><a href="../">../</a></pre>
<pre><a href="sbt_1.0/">sbt_1.0/</a></pre>
</body>
</html>
`,
)
.get('/org/foundweekends/sbt-bintray/scala_2.12/sbt_1.0/')
.reply(
200,
codeBlock`
<a href='../'>../</a>
<a href='1.2.0/'>1.2.0/</a>
<html>
<head> </head>
<body>
<pre><a href="../">../</a></pre>
<pre><a href="0.5.5/">0.5.5/</a></pre>
</body>
</html>
`,
);

httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/scalatest/scalatest_2.12/')
.reply(200, "<a href='1.2.3/'>4.5.6/</a>");
.scope('https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/')
.get('/org.foundweekends/sbt-bintray/')
.reply(404)
.get('/org.foundweekends/')
.reply(404)
.get('/org/foundweekends/sbt-bintray/')
.reply(404)
.get('/org/foundweekends/')
.reply(404);

expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.foundweekends:sbt-bintray',
registryUrls: [],
}),
).toEqual({
dependencyUrl:
'https://repo.maven.apache.org/maven2/org/foundweekends/sbt-bintray',
registryUrl: 'https://repo.maven.apache.org/maven2',
releases: [{ version: '0.5.5' }],
});
});

it('fetches sbt plugins 2', async () => {
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/foundweekends/sbt-bintray/')
.scope('https://repo.maven.apache.org/maven2/')
.get('/org/foundweekends/sbt-bintray/')
.reply(
200,
codeBlock`
Expand All @@ -84,10 +169,8 @@ describe('modules/datasource/sbt-plugin/index', () => {
</body>
</html>
`,
);
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/foundweekends/sbt-bintray/scala_2.12/')
)
.get('/org/foundweekends/sbt-bintray/scala_2.12/')
.reply(
200,
codeBlock`
Expand All @@ -99,10 +182,8 @@ describe('modules/datasource/sbt-plugin/index', () => {
</body>
</html>
`,
);
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/org/foundweekends/sbt-bintray/scala_2.12/sbt_1.0/')
)
.get('/org/foundweekends/sbt-bintray/scala_2.12/sbt_1.0/')
.reply(
200,
codeBlock`
Expand All @@ -117,8 +198,35 @@ describe('modules/datasource/sbt-plugin/index', () => {
);

httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/io/get-coursier/')
.scope('https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/')
.get('/org.foundweekends/sbt-bintray/')
.reply(404)
.get('/org.foundweekends/')
.reply(404)
.get('/org/foundweekends/sbt-bintray/')
.reply(404)
.get('/org/foundweekends/')
.reply(404);

expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.foundweekends:sbt-bintray_2.12',
registryUrls: [],
}),
).toEqual({
dependencyUrl:
'https://repo.maven.apache.org/maven2/org/foundweekends/sbt-bintray',
registryUrl: 'https://repo.maven.apache.org/maven2',
releases: [{ version: '0.5.5' }],
});
});

it('extracts URL from Maven POM file', async () => {
httpMock
.scope('https://repo.maven.apache.org/maven2/')
.get('/io/get-coursier/')
.reply(
200,
codeBlock`
Expand All @@ -132,10 +240,8 @@ describe('modules/datasource/sbt-plugin/index', () => {
>sbt-coursier_2.12_1.0.0-M6/</a
>
`,
);
httpMock
.scope('https://repo.maven.apache.org')
.get('/maven2/io/get-coursier/sbt-coursier_2.12_1.0/')
)
.get('/io/get-coursier/sbt-coursier_2.12_1.0/')
.reply(
200,
codeBlock`
Expand All @@ -144,11 +250,9 @@ describe('modules/datasource/sbt-plugin/index', () => {
<a href="2.0.0-RC6-2/">2.0.0-RC6-2/</a>
<a href="2.0.0-RC6-6/">2.0.0-RC6-6/</a>
`,
);
httpMock
.scope('https://repo.maven.apache.org')
)
.get(
'/maven2/io/get-coursier/sbt-coursier_2.12_1.0/2.0.0-RC6-6/sbt-coursier-2.0.0-RC6-6.pom',
'/io/get-coursier/sbt-coursier_2.12_1.0/2.0.0-RC6-6/sbt-coursier-2.0.0-RC6-6.pom',
)
.reply(
200,
Expand All @@ -160,64 +264,29 @@ describe('modules/datasource/sbt-plugin/index', () => {
</scm>
</project>
`,
);
});

// TODO: fix mocks
afterEach(() => httpMock.clear(false));

it('returns null in case of errors', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.scalatest:scalatest',
registryUrls: ['https://failed_repo/maven'],
}),
).toBeNull();
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.scalatest:scalaz',
registryUrls: [],
}),
).toBeNull();
});

it('fetches sbt plugins', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.foundweekends:sbt-bintray',
registryUrls: [],
}),
).toEqual({
dependencyUrl:
'https://repo.maven.apache.org/maven2/org/foundweekends/sbt-bintray',
registryUrl: 'https://repo.maven.apache.org/maven2',
releases: [{ version: '0.5.5' }],
});
});
)

it('fetches sbt plugins 2', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
datasource: SbtPluginDatasource.id,
packageName: 'org.foundweekends:sbt-bintray_2.12',
registryUrls: [],
}),
).toEqual({
dependencyUrl:
'https://repo.maven.apache.org/maven2/org/foundweekends/sbt-bintray',
registryUrl: 'https://repo.maven.apache.org/maven2',
releases: [{ version: '0.5.5' }],
});
});
.get('/io/get-coursier/sbt-coursier/')
.reply(404)
.get('/io/get-coursier/sbt-coursier_2.10_0.13/')
.reply(404)
.get('/io/get-coursier/sbt-coursier_2.12_1.0.0-M5/')
.reply(404)
.get('/io/get-coursier/sbt-coursier_2.12_1.0.0-M6/')
.reply(404)
.get(
'/io/get-coursier/sbt-coursier_2.10_0.13/2.0.0-RC6-6/sbt-coursier_2.10_0.13-2.0.0-RC6-6.pom',
)
.reply(404)
.get(
'/io/get-coursier/sbt-coursier_2.10_0.13/2.0.0-RC6-6/sbt-coursier-2.0.0-RC6-6.pom',
)
.reply(404)
.get(
'/io/get-coursier/sbt-coursier_2.12_1.0/2.0.0-RC6-6/sbt-coursier_2.12_1.0-2.0.0-RC6-6.pom',
)
.reply(404);

it('extracts URL from Maven POM file', async () => {
expect(
await getPkgReleases({
versioning: mavenVersioning.id,
Expand Down