Skip to content

Commit

Permalink
fix(sbt): normalize scala versions (#5154)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jan 15, 2020
1 parent 856eb33 commit 6459775
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 50 deletions.
24 changes: 22 additions & 2 deletions lib/manager/sbt/extract.ts
@@ -1,5 +1,6 @@
import { DEFAULT_MAVEN_REPO } from '../maven/extract';
import { PackageFile, PackageDependency } from '../common';
import { get } from '../../versioning';

const isComment = (str: string): boolean => /^\s*\/\//.test(str);

Expand All @@ -20,6 +21,25 @@ const isScalaVersion = (str: string): boolean =>
const getScalaVersion = (str: string): string =>
str.replace(/^\s*scalaVersion\s*:=\s*"/, '').replace(/"\s*$/, '');

/*
https://www.scala-sbt.org/release/docs/Cross-Build.html#Publishing+conventions
*/
const normalizeScalaVersion = (str: string): string => {
// istanbul ignore if
if (!str) return str;
const versioning = get('maven');
if (versioning.isVersion(str)) {
// Do not normalize unstable versions
if (!versioning.isStable(str)) return str;
// Do not normalize versions prior to 2.10
if (!versioning.isGreaterThan(str, '2.10.0')) return str;
}
if (/^\d+\.\d+\.\d+$/.test(str))
return str.replace(/^(\d+)\.(\d+)\.\d+$/, '$1.$2');
// istanbul ignore next
return str;
};

const isScalaVersionVariable = (str: string): boolean =>
/^\s*scalaVersion\s*:=\s*[_a-zA-Z][_a-zA-Z0-9]*\s*$/.test(str);

Expand Down Expand Up @@ -176,7 +196,7 @@ function parseSbtLine(
if (!isComment(line)) {
if (isScalaVersion(line)) {
isMultiDeps = false;
scalaVersion = getScalaVersion(line);
scalaVersion = normalizeScalaVersion(getScalaVersion(line));
} else if (isScalaVersionVariable(line)) {
isMultiDeps = false;
scalaVersionVariable = getScalaVersionVariable(line);
Expand Down Expand Up @@ -244,7 +264,7 @@ function parseSbtLine(
scalaVersion ||
(scalaVersionVariable &&
variables[scalaVersionVariable] &&
variables[scalaVersionVariable].val),
normalizeScalaVersion(variables[scalaVersionVariable].val)),
};
if (deps.length) return { deps };
return null;
Expand Down
92 changes: 46 additions & 46 deletions test/manager/sbt/__snapshots__/extract.spec.ts.snap
@@ -1,13 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lib/manager/sbt/extract extractPackageFile() extract deps from native scala file with variables 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "0.7.1",
"datasource": "sbt",
"depName": "com.example:foo_2.13.0-RC5",
"fileReplacePosition": 168,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
Object {
"currentValue": "1.2.3",
"datasource": "sbt",
"depName": "com.abc:abc",
"fileReplacePosition": 120,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
],
}
`;

exports[`lib/manager/sbt/extract extractPackageFile() extracts deps for generic use-cases 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "0.0.1",
"datasource": "sbt",
"depName": "org.example:foo",
"fileReplacePosition": 133,
"fileReplacePosition": 132,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -20,8 +45,8 @@ Object {
Object {
"currentValue": "0.0.2",
"datasource": "sbt",
"depName": "org.example:bar_2.12",
"fileReplacePosition": 189,
"depName": "org.example:bar_2.9.10",
"fileReplacePosition": 188,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -34,8 +59,8 @@ Object {
Object {
"currentValue": "0.0.3",
"datasource": "sbt",
"depName": "org.example:baz_2.12",
"fileReplacePosition": 253,
"depName": "org.example:baz_2.9.10",
"fileReplacePosition": 252,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -49,7 +74,7 @@ Object {
"currentValue": "0.0.4",
"datasource": "sbt",
"depName": "org.example:qux",
"fileReplacePosition": 288,
"fileReplacePosition": 287,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -63,7 +88,7 @@ Object {
"currentValue": "0.0.5",
"datasource": "sbt",
"depName": "org.example:quux",
"fileReplacePosition": 347,
"fileReplacePosition": 346,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -76,9 +101,9 @@ Object {
Object {
"currentValue": "0.0.6",
"datasource": "sbt",
"depName": "org.example:quuz_2.12",
"depName": "org.example:quuz_2.9.10",
"depType": "test",
"fileReplacePosition": 527,
"fileReplacePosition": 526,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -93,7 +118,7 @@ Object {
"datasource": "sbt",
"depName": "org.example:corge",
"depType": "Provided",
"fileReplacePosition": 585,
"fileReplacePosition": 584,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -108,7 +133,7 @@ Object {
"datasource": "sbt",
"depName": "org.example:grault",
"depType": "Test",
"fileReplacePosition": 480,
"fileReplacePosition": 479,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -123,7 +148,7 @@ Object {
"datasource": "sbt",
"depName": "org.example:waldo",
"depType": "plugin",
"fileReplacePosition": 987,
"fileReplacePosition": 986,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -144,7 +169,7 @@ Object {
"currentValue": "0.0.1",
"datasource": "sbt",
"depName": "org.example:foo",
"fileReplacePosition": 192,
"fileReplacePosition": 195,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -158,7 +183,7 @@ Object {
"currentValue": "0.0.2",
"datasource": "sbt",
"depName": "org.example:bar_2.12",
"fileReplacePosition": 248,
"fileReplacePosition": 251,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -172,7 +197,7 @@ Object {
"currentValue": "0.0.3",
"datasource": "sbt",
"depName": "org.example:baz_2.12",
"fileReplacePosition": 312,
"fileReplacePosition": 315,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -186,7 +211,7 @@ Object {
"currentValue": "0.0.4",
"datasource": "sbt",
"depName": "org.example:qux",
"fileReplacePosition": 347,
"fileReplacePosition": 350,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -200,7 +225,7 @@ Object {
"currentValue": "0.0.5",
"datasource": "sbt",
"depName": "org.example:quux",
"fileReplacePosition": 406,
"fileReplacePosition": 409,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -215,7 +240,7 @@ Object {
"datasource": "sbt",
"depName": "org.example:quuz_2.12",
"depType": "test",
"fileReplacePosition": 550,
"fileReplacePosition": 553,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -230,7 +255,7 @@ Object {
"datasource": "sbt",
"depName": "org.example:corge",
"depType": "Provided",
"fileReplacePosition": 608,
"fileReplacePosition": 611,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -245,7 +270,7 @@ Object {
"datasource": "sbt",
"depName": "org.example:grault",
"depType": "Test",
"fileReplacePosition": 48,
"fileReplacePosition": 51,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand All @@ -260,7 +285,7 @@ Object {
"datasource": "sbt",
"depName": "org.example:waldo",
"depType": "plugin",
"fileReplacePosition": 1010,
"fileReplacePosition": 1013,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
"https://example.com/repos/1/",
Expand Down Expand Up @@ -299,28 +324,3 @@ Object {
],
}
`;

exports[`lib/manager/sbt/extract extractPackageFile() extract deps from native scala file with variables 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "0.7.1",
"datasource": "sbt",
"depName": "com.example:foo_2.13.0-RC5",
"fileReplacePosition": 168,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
Object {
"currentValue": "1.2.3",
"datasource": "sbt",
"depName": "com.abc:abc",
"fileReplacePosition": 120,
"registryUrls": Array [
"https://repo.maven.apache.org/maven2",
],
},
],
}
`;
2 changes: 1 addition & 1 deletion test/manager/sbt/_fixtures/sample.sbt
@@ -1,4 +1,4 @@
scalaVersion := "2.12.10"
scalaVersion := "2.9.10"

// libraryDependencies += "org.example" % "foo" % "0.0.0"
libraryDependencies += "org.example" % "foo" % "0.0.1"
Expand Down
2 changes: 1 addition & 1 deletion test/manager/sbt/_fixtures/scala-version-variable.sbt
@@ -1,4 +1,4 @@
val ScalaVersion = "2.12"
val ScalaVersion = "2.12.10"
val versionExample = "0.0.8"

scalaVersion := ScalaVersion
Expand Down

0 comments on commit 6459775

Please sign in to comment.