Skip to content

Commit

Permalink
Add scalafmt test with multiple Scala versions (#1589)
Browse files Browse the repository at this point in the history
* Extract common functions for testing scalafmt

* Add scalafmt test with multiple Scala versions

---------

Co-authored-by: mkuta <[email protected]>
  • Loading branch information
aszady and mateuszkuta256 authored Jun 17, 2024
1 parent 9abf3b0 commit 9184f0d
Show file tree
Hide file tree
Showing 20 changed files with 308 additions and 62 deletions.
11 changes: 11 additions & 0 deletions test/shell/test_cross_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
. "${dir}"/test_scalafmt_helper.sh
runner=$(get_test_runner "${1:-local}")

cd test_cross_build
Expand All @@ -12,4 +13,14 @@ function test_cross_build() {
bazel shutdown;
}

function test_scalafmt() {
run_formatting scalafmt binary2 binary2
run_formatting scalafmt binary3 binary3
run_formatting scalafmt library2 library2
run_formatting scalafmt library3 library3
run_formatting scalafmt test2 test2
run_formatting scalafmt test3 test3
}

$runner test_cross_build
$runner test_scalafmt
67 changes: 5 additions & 62 deletions test/shell/test_scalafmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,22 @@
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
. "${dir}"/test_scalafmt_helper.sh
runner=$(get_test_runner "${1:-local}")

backup_unformatted() {
FILE_PATH=$1
FILENAME=$2
cp $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
}

restore_unformatted_before_exit() {
FILE_PATH=$1
FILENAME=$2
cp $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala $FILE_PATH/unformatted/unformatted-$FILENAME.scala
rm -f $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
}

run_formatting() {
set +e

FILE_PATH="$( dirname $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) )"/scalafmt
RULE_TYPE=$1
FILENAME=$2

#on windows scalafmt targets need to be run using bash.
#TODO: improve the scalafmt funcitonality so we don't need to use the run_under mechanism
local run_under = ""
if is_windows; then
run_under="--run_under=bash"
fi

bazel run //test/scalafmt:formatted-$RULE_TYPE.format-test $run_under
if [ $? -ne 0 ]; then
echo -e "${RED} formatted-$RULE_TYPE.format-test should be a formatted target. $NC"
exit 1
fi

bazel run //test/scalafmt:unformatted-$RULE_TYPE.format-test $run_under
if [ $? -eq 0 ]; then
echo -e "${RED} unformatted-$RULE_TYPE.format-test should be an unformatted target. $NC"
exit 1
fi

backup_unformatted $FILE_PATH $FILENAME
# format unformatted*.scala

bazel run //test/scalafmt:unformatted-$RULE_TYPE.format $run_under
if [ $? -ne 0 ]; then
echo -e "${RED} unformatted-$RULE_TYPE.format should run formatting. $NC"
restore_unformatted_before_exit $FILE_PATH $FILENAME
exit 1
fi

diff $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/formatted/formatted-$FILENAME.scala
if [ $? -ne 0 ]; then
echo -e "${RED} unformatted-$FILENAME.scala should be the same as formatted-$FILENAME.scala after formatting. $NC"
restore_unformatted_before_exit $FILE_PATH $FILENAME
exit 1
fi
restore_unformatted_before_exit $FILE_PATH $FILENAME
}

test_scalafmt_binary() {
run_formatting binary encoding
run_formatting test/scalafmt binary encoding
}

test_scalafmt_library() {
run_formatting library encoding
run_formatting test/scalafmt library encoding
}

test_scalafmt_test() {
run_formatting test test
run_formatting test/scalafmt test test
}
test_custom_conf() {
run_formatting custom-conf custom-conf
run_formatting test/scalafmt custom-conf custom-conf
}

$runner test_scalafmt_binary
Expand Down
57 changes: 57 additions & 0 deletions test/shell/test_scalafmt_helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
backup_unformatted() {
FILE_PATH=$1
FILENAME=$2
cp $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
}

restore_unformatted_before_exit() {
FILE_PATH=$1
FILENAME=$2
cp $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala $FILE_PATH/unformatted/unformatted-$FILENAME.scala
rm -f $FILE_PATH/unformatted/unformatted-$FILENAME.backup.scala
}

run_formatting() {
set +e

PACKAGE_DIR=$1
RULE_TYPE=$2
FILENAME=$3

#on windows scalafmt targets need to be run using bash.
#TODO: improve the scalafmt funcitonality so we don't need to use the run_under mechanism
local run_under=""
if is_windows; then
run_under="--run_under=bash"
fi

bazel run //$PACKAGE_DIR:formatted-$RULE_TYPE.format-test $run_under
if [ $? -ne 0 ]; then
echo -e "${RED} formatted-$RULE_TYPE.format-test should be a formatted target. $NC"
exit 1
fi

bazel run //$PACKAGE_DIR:unformatted-$RULE_TYPE.format-test $run_under
if [ $? -eq 0 ]; then
echo -e "${RED} unformatted-$RULE_TYPE.format-test should be an unformatted target. $NC"
exit 1
fi

backup_unformatted $PACKAGE_DIR $FILENAME
# format unformatted*.scala

bazel run //$PACKAGE_DIR:unformatted-$RULE_TYPE.format $run_under
if [ $? -ne 0 ]; then
echo -e "${RED} unformatted-$RULE_TYPE.format should run formatting. $NC"
restore_unformatted_before_exit $PACKAGE_DIR $FILENAME
exit 1
fi

diff $FILE_PATH/unformatted/unformatted-$FILENAME.scala $FILE_PATH/formatted/formatted-$FILENAME.scala
if [ $? -ne 0 ]; then
echo -e "${RED} unformatted-$FILENAME.scala should be the same as formatted-$FILENAME.scala after formatting. $NC"
restore_unformatted_before_exit $PACKAGE_DIR $FILENAME
exit 1
fi
restore_unformatted_before_exit $FILE_PATH $FILENAME
}
26 changes: 26 additions & 0 deletions test_cross_build/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ http_archive(
],
)

http_archive(
name = "rules_proto",
sha256 = "8e7d59a5b12b233be5652e3d29f42fba01c7cbab09f6b3a8d0a57ed6d1e9a0da",
strip_prefix = "rules_proto-7e4afce6fe62dbff0a4a03450143146f9f2d7488",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz",
"https://github.com/bazelbuild/rules_proto/archive/7e4afce6fe62dbff0a4a03450143146f9f2d7488.tar.gz",
],
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()

local_repository(
name = "io_bazel_rules_scala",
path = "..",
Expand Down Expand Up @@ -38,3 +54,13 @@ rules_scala_toolchain_deps_repositories()
load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_toolchains")

scala_register_toolchains()

load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain")

scalatest_repositories()

scalatest_toolchain()

load("@io_bazel_rules_scala//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_repositories")

scalafmt_repositories()
2 changes: 2 additions & 0 deletions test_cross_build/scalafmt/.scalafmt2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
maxColumn = 40
lineEndings=preserve
3 changes: 3 additions & 0 deletions test_cross_build/scalafmt/.scalafmt3.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
runner.dialect=scala3
maxColumn = 40
lineEndings=preserve
115 changes: 115 additions & 0 deletions test_cross_build/scalafmt/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
load(
"scalafmt_rules.bzl",
"scalafmt_scala_binary",
"scalafmt_scala_library",
"scalafmt_scala_test",
)

filegroup(
name = "scala2-conf",
srcs = [".scalafmt2.conf"],
)

filegroup(
name = "scala3-conf",
srcs = [".scalafmt3.conf"],
)

scalafmt_scala_library(
name = "unformatted-library2",
srcs = ["unformatted/unformatted-library2.scala"],
config = ":scala2-conf",
format = True,
scala_version = "2.13.12",
)

scalafmt_scala_library(
name = "formatted-library2",
srcs = ["formatted/formatted-library2.scala"],
config = ":scala2-conf",
format = True,
scala_version = "2.13.12",
)

scalafmt_scala_library(
name = "unformatted-library3",
srcs = ["unformatted/unformatted-library3.scala"],
config = ":scala3-conf",
format = True,
scala_version = "3.3.1",
)

scalafmt_scala_library(
name = "formatted-library3",
srcs = ["formatted/formatted-library3.scala"],
config = ":scala3-conf",
format = True,
scala_version = "3.3.1",
)

scalafmt_scala_binary(
name = "unformatted-binary2",
srcs = ["unformatted/unformatted-binary2.scala"],
config = ":scala2-conf",
format = True,
main_class = "UnformattedBinary",
scala_version = "2.12.18",
)

scalafmt_scala_library(
name = "formatted-binary2",
srcs = ["formatted/formatted-binary2.scala"],
config = ":scala2-conf",
format = True,
main_class = "UnformattedBinary",
scala_version = "2.12.18",
)

scalafmt_scala_binary(
name = "unformatted-binary3",
srcs = ["unformatted/unformatted-binary3.scala"],
config = ":scala3-conf",
format = True,
main_class = "UnformattedBinary",
scala_version = "3.2.1",
)

scalafmt_scala_library(
name = "formatted-binary3",
srcs = ["formatted/formatted-binary3.scala"],
config = ":scala3-conf",
format = True,
main_class = "UnformattedBinary",
scala_version = "3.2.1",
)

scalafmt_scala_test(
name = "unformatted-test2",
srcs = ["unformatted/unformatted-test2.scala"],
config = ":scala2-conf",
format = True,
scala_version = "2.12.18",
)

scalafmt_scala_test(
name = "formatted-test2",
srcs = ["formatted/formatted-test2.scala"],
config = ":scala2-conf",
format = True,
scala_version = "2.12.18",
)

#default scala version is 3.1.0
scalafmt_scala_test(
name = "unformatted-test3",
srcs = ["unformatted/unformatted-test3.scala"],
config = ":scala3-conf",
format = True,
)

scalafmt_scala_test(
name = "formatted-test3",
srcs = ["formatted/formatted-test3.scala"],
config = ":scala3-conf",
format = True,
)
10 changes: 10 additions & 0 deletions test_cross_build/scalafmt/formatted/formatted-binary2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object UnformattedBinary {
val info =
" unformatted file "

def main(
args: Array[String]
): Unit = {
println(info)
}
}
5 changes: 5 additions & 0 deletions test_cross_build/scalafmt/formatted/formatted-binary3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@main def UnformattedBinary(): Unit =
val message =
" unformatted binary "

println(message)
4 changes: 4 additions & 0 deletions test_cross_build/scalafmt/formatted/formatted-library2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Unformatted {
val info =
" unformatted file "
}
3 changes: 3 additions & 0 deletions test_cross_build/scalafmt/formatted/formatted-library3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Unformatted:
val info =
" unformatted file "
8 changes: 8 additions & 0 deletions test_cross_build/scalafmt/formatted/formatted-test2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import org.scalatest.flatspec._

class Test extends AnyFlatSpec {

"Test" should "be formatted" in {
assert(true)
}
}
7 changes: 7 additions & 0 deletions test_cross_build/scalafmt/formatted/formatted-test3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import org.scalatest.flatspec._

class Test extends AnyFlatSpec:

"Test" should "be formatted" in {
assert(true)
}
16 changes: 16 additions & 0 deletions test_cross_build/scalafmt/scalafmt_rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load(
"@io_bazel_rules_scala//scala:advanced_usage/scala.bzl",
"make_scala_binary",
"make_scala_library",
"make_scala_test",
)
load(
"@io_bazel_rules_scala//scala/scalafmt:phase_scalafmt_ext.bzl",
"ext_scalafmt",
)

scalafmt_scala_binary = make_scala_binary(ext_scalafmt)

scalafmt_scala_library = make_scala_library(ext_scalafmt)

scalafmt_scala_test = make_scala_test(ext_scalafmt)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

object UnformattedBinary {
val info = " unformatted file "

def main(args: Array[String]): Unit =
{
println(info)
}
}
Loading

0 comments on commit 9184f0d

Please sign in to comment.