Skip to content

Commit 9184f0d

Browse files
Add scalafmt test with multiple Scala versions (#1589)
* Extract common functions for testing scalafmt * Add scalafmt test with multiple Scala versions --------- Co-authored-by: mkuta <[email protected]>
1 parent 9abf3b0 commit 9184f0d

20 files changed

+308
-62
lines changed

test/shell/test_cross_build.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
33
. "${dir}"/test_runner.sh
44
. "${dir}"/test_helper.sh
5+
. "${dir}"/test_scalafmt_helper.sh
56
runner=$(get_test_runner "${1:-local}")
67

78
cd test_cross_build
@@ -12,4 +13,14 @@ function test_cross_build() {
1213
bazel shutdown;
1314
}
1415

16+
function test_scalafmt() {
17+
run_formatting scalafmt binary2 binary2
18+
run_formatting scalafmt binary3 binary3
19+
run_formatting scalafmt library2 library2
20+
run_formatting scalafmt library3 library3
21+
run_formatting scalafmt test2 test2
22+
run_formatting scalafmt test3 test3
23+
}
24+
1525
$runner test_cross_build
26+
$runner test_scalafmt

test/shell/test_scalafmt.sh

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,22 @@
22
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
33
. "${dir}"/test_runner.sh
44
. "${dir}"/test_helper.sh
5+
. "${dir}"/test_scalafmt_helper.sh
56
runner=$(get_test_runner "${1:-local}")
67

7-
backup_unformatted() {
8-
FILE_PATH=$1
9-
FILENAME=$2
10-
cp $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
11-
}
12-
13-
restore_unformatted_before_exit() {
14-
FILE_PATH=$1
15-
FILENAME=$2
16-
cp $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala $FILE_PATH/unformatted/unformatted-$FILENAME.scala
17-
rm -f $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
18-
}
19-
20-
run_formatting() {
21-
set +e
22-
23-
FILE_PATH="$( dirname $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) )"/scalafmt
24-
RULE_TYPE=$1
25-
FILENAME=$2
26-
27-
#on windows scalafmt targets need to be run using bash.
28-
#TODO: improve the scalafmt funcitonality so we don't need to use the run_under mechanism
29-
local run_under = ""
30-
if is_windows; then
31-
run_under="--run_under=bash"
32-
fi
33-
34-
bazel run //test/scalafmt:formatted-$RULE_TYPE.format-test $run_under
35-
if [ $? -ne 0 ]; then
36-
echo -e "${RED} formatted-$RULE_TYPE.format-test should be a formatted target. $NC"
37-
exit 1
38-
fi
39-
40-
bazel run //test/scalafmt:unformatted-$RULE_TYPE.format-test $run_under
41-
if [ $? -eq 0 ]; then
42-
echo -e "${RED} unformatted-$RULE_TYPE.format-test should be an unformatted target. $NC"
43-
exit 1
44-
fi
45-
46-
backup_unformatted $FILE_PATH $FILENAME
47-
# format unformatted*.scala
48-
49-
bazel run //test/scalafmt:unformatted-$RULE_TYPE.format $run_under
50-
if [ $? -ne 0 ]; then
51-
echo -e "${RED} unformatted-$RULE_TYPE.format should run formatting. $NC"
52-
restore_unformatted_before_exit $FILE_PATH $FILENAME
53-
exit 1
54-
fi
55-
56-
diff $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/formatted/formatted-$FILENAME.scala
57-
if [ $? -ne 0 ]; then
58-
echo -e "${RED} unformatted-$FILENAME.scala should be the same as formatted-$FILENAME.scala after formatting. $NC"
59-
restore_unformatted_before_exit $FILE_PATH $FILENAME
60-
exit 1
61-
fi
62-
restore_unformatted_before_exit $FILE_PATH $FILENAME
63-
}
64-
658
test_scalafmt_binary() {
66-
run_formatting binary encoding
9+
run_formatting test/scalafmt binary encoding
6710
}
6811

6912
test_scalafmt_library() {
70-
run_formatting library encoding
13+
run_formatting test/scalafmt library encoding
7114
}
7215

7316
test_scalafmt_test() {
74-
run_formatting test test
17+
run_formatting test/scalafmt test test
7518
}
7619
test_custom_conf() {
77-
run_formatting custom-conf custom-conf
20+
run_formatting test/scalafmt custom-conf custom-conf
7821
}
7922

8023
$runner test_scalafmt_binary

test/shell/test_scalafmt_helper.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
backup_unformatted() {
2+
FILE_PATH=$1
3+
FILENAME=$2
4+
cp $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
5+
}
6+
7+
restore_unformatted_before_exit() {
8+
FILE_PATH=$1
9+
FILENAME=$2
10+
cp $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala $FILE_PATH/unformatted/unformatted-$FILENAME.scala
11+
rm -f $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
12+
}
13+
14+
run_formatting() {
15+
set +e
16+
17+
PACKAGE_DIR=$1
18+
RULE_TYPE=$2
19+
FILENAME=$3
20+
21+
#on windows scalafmt targets need to be run using bash.
22+
#TODO: improve the scalafmt funcitonality so we don't need to use the run_under mechanism
23+
local run_under=""
24+
if is_windows; then
25+
run_under="--run_under=bash"
26+
fi
27+
28+
bazel run //$PACKAGE_DIR:formatted-$RULE_TYPE.format-test $run_under
29+
if [ $? -ne 0 ]; then
30+
echo -e "${RED} formatted-$RULE_TYPE.format-test should be a formatted target. $NC"
31+
exit 1
32+
fi
33+
34+
bazel run //$PACKAGE_DIR:unformatted-$RULE_TYPE.format-test $run_under
35+
if [ $? -eq 0 ]; then
36+
echo -e "${RED} unformatted-$RULE_TYPE.format-test should be an unformatted target. $NC"
37+
exit 1
38+
fi
39+
40+
backup_unformatted $PACKAGE_DIR $FILENAME
41+
# format unformatted*.scala
42+
43+
bazel run //$PACKAGE_DIR:unformatted-$RULE_TYPE.format $run_under
44+
if [ $? -ne 0 ]; then
45+
echo -e "${RED} unformatted-$RULE_TYPE.format should run formatting. $NC"
46+
restore_unformatted_before_exit $PACKAGE_DIR $FILENAME
47+
exit 1
48+
fi
49+
50+
diff $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/formatted/formatted-$FILENAME.scala
51+
if [ $? -ne 0 ]; then
52+
echo -e "${RED} unformatted-$FILENAME.scala should be the same as formatted-$FILENAME.scala after formatting. $NC"
53+
restore_unformatted_before_exit $PACKAGE_DIR $FILENAME
54+
exit 1
55+
fi
56+
restore_unformatted_before_exit $FILE_PATH $FILENAME
57+
}

test_cross_build/WORKSPACE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ http_archive(
1111
],
1212
)
1313

14+
http_archive(
15+
name = "rules_proto",
16+
sha256 = "8e7d59a5b12b233be5652e3d29f42fba01c7cbab09f6b3a8d0a57ed6d1e9a0da",
17+
strip_prefix = "rules_proto-7e4afce6fe62dbff0a4a03450143146f9f2d7488",
18+
urls = [
19+
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz",
20+
"https://github.com/bazelbuild/rules_proto/archive/7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz",
21+
],
22+
)
23+
24+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
25+
26+
rules_proto_dependencies()
27+
28+
rules_proto_toolchains()
29+
1430
local_repository(
1531
name = "io_bazel_rules_scala",
1632
path = "..",
@@ -38,3 +54,13 @@ rules_scala_toolchain_deps_repositories()
3854
load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")
3955

4056
scala_register_toolchains()
57+
58+
load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")
59+
60+
scalatest_repositories()
61+
62+
scalatest_toolchain()
63+
64+
load("@io_bazel_rules_scala//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_repositories")
65+
66+
scalafmt_repositories()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
maxColumn = 40
2+
lineEndings=preserve
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
runner.dialect=scala3
2+
maxColumn = 40
3+
lineEndings=preserve

test_cross_build/scalafmt/BUILD

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
load(
2+
"scalafmt_rules.bzl",
3+
"scalafmt_scala_binary",
4+
"scalafmt_scala_library",
5+
"scalafmt_scala_test",
6+
)
7+
8+
filegroup(
9+
name = "scala2-conf",
10+
srcs = [".scalafmt2.conf"],
11+
)
12+
13+
filegroup(
14+
name = "scala3-conf",
15+
srcs = [".scalafmt3.conf"],
16+
)
17+
18+
scalafmt_scala_library(
19+
name = "unformatted-library2",
20+
srcs = ["unformatted/unformatted-library2.scala"],
21+
config = ":scala2-conf",
22+
format = True,
23+
scala_version = "2.13.12",
24+
)
25+
26+
scalafmt_scala_library(
27+
name = "formatted-library2",
28+
srcs = ["formatted/formatted-library2.scala"],
29+
config = ":scala2-conf",
30+
format = True,
31+
scala_version = "2.13.12",
32+
)
33+
34+
scalafmt_scala_library(
35+
name = "unformatted-library3",
36+
srcs = ["unformatted/unformatted-library3.scala"],
37+
config = ":scala3-conf",
38+
format = True,
39+
scala_version = "3.3.1",
40+
)
41+
42+
scalafmt_scala_library(
43+
name = "formatted-library3",
44+
srcs = ["formatted/formatted-library3.scala"],
45+
config = ":scala3-conf",
46+
format = True,
47+
scala_version = "3.3.1",
48+
)
49+
50+
scalafmt_scala_binary(
51+
name = "unformatted-binary2",
52+
srcs = ["unformatted/unformatted-binary2.scala"],
53+
config = ":scala2-conf",
54+
format = True,
55+
main_class = "UnformattedBinary",
56+
scala_version = "2.12.18",
57+
)
58+
59+
scalafmt_scala_library(
60+
name = "formatted-binary2",
61+
srcs = ["formatted/formatted-binary2.scala"],
62+
config = ":scala2-conf",
63+
format = True,
64+
main_class = "UnformattedBinary",
65+
scala_version = "2.12.18",
66+
)
67+
68+
scalafmt_scala_binary(
69+
name = "unformatted-binary3",
70+
srcs = ["unformatted/unformatted-binary3.scala"],
71+
config = ":scala3-conf",
72+
format = True,
73+
main_class = "UnformattedBinary",
74+
scala_version = "3.2.1",
75+
)
76+
77+
scalafmt_scala_library(
78+
name = "formatted-binary3",
79+
srcs = ["formatted/formatted-binary3.scala"],
80+
config = ":scala3-conf",
81+
format = True,
82+
main_class = "UnformattedBinary",
83+
scala_version = "3.2.1",
84+
)
85+
86+
scalafmt_scala_test(
87+
name = "unformatted-test2",
88+
srcs = ["unformatted/unformatted-test2.scala"],
89+
config = ":scala2-conf",
90+
format = True,
91+
scala_version = "2.12.18",
92+
)
93+
94+
scalafmt_scala_test(
95+
name = "formatted-test2",
96+
srcs = ["formatted/formatted-test2.scala"],
97+
config = ":scala2-conf",
98+
format = True,
99+
scala_version = "2.12.18",
100+
)
101+
102+
#default scala version is 3.1.0
103+
scalafmt_scala_test(
104+
name = "unformatted-test3",
105+
srcs = ["unformatted/unformatted-test3.scala"],
106+
config = ":scala3-conf",
107+
format = True,
108+
)
109+
110+
scalafmt_scala_test(
111+
name = "formatted-test3",
112+
srcs = ["formatted/formatted-test3.scala"],
113+
config = ":scala3-conf",
114+
format = True,
115+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object UnformattedBinary {
2+
val info =
3+
" unformatted file "
4+
5+
def main(
6+
args: Array[String]
7+
): Unit = {
8+
println(info)
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@main def UnformattedBinary(): Unit =
2+
val message =
3+
" unformatted binary "
4+
5+
println(message)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Unformatted {
2+
val info =
3+
" unformatted file "
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Unformatted:
2+
val info =
3+
" unformatted file "
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import org.scalatest.flatspec._
2+
3+
class Test extends AnyFlatSpec {
4+
5+
"Test" should "be formatted" in {
6+
assert(true)
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import org.scalatest.flatspec._
2+
3+
class Test extends AnyFlatSpec:
4+
5+
"Test" should "be formatted" in {
6+
assert(true)
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load(
2+
"@io_bazel_rules_scala//scala:advanced_usage/scala.bzl",
3+
"make_scala_binary",
4+
"make_scala_library",
5+
"make_scala_test",
6+
)
7+
load(
8+
"@io_bazel_rules_scala//scala/scalafmt:phase_scalafmt_ext.bzl",
9+
"ext_scalafmt",
10+
)
11+
12+
scalafmt_scala_binary = make_scala_binary(ext_scalafmt)
13+
14+
scalafmt_scala_library = make_scala_library(ext_scalafmt)
15+
16+
scalafmt_scala_test = make_scala_test(ext_scalafmt)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
object UnformattedBinary {
3+
val info = " unformatted file "
4+
5+
def main(args: Array[String]): Unit =
6+
{
7+
println(info)
8+
}
9+
}

0 commit comments

Comments
 (0)