-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
9abf3b0
commit 9184f0d
Showing
20 changed files
with
308 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
maxColumn = 40 | ||
lineEndings=preserve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
runner.dialect=scala3 | ||
maxColumn = 40 | ||
lineEndings=preserve |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
test_cross_build/scalafmt/formatted/formatted-binary2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@main def UnformattedBinary(): Unit = | ||
val message = | ||
" unformatted binary " | ||
|
||
println(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
object Unformatted { | ||
val info = | ||
" unformatted file " | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
object Unformatted: | ||
val info = | ||
" unformatted file " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
9 changes: 9 additions & 0 deletions
9
test_cross_build/scalafmt/unformatted/unformatted-binary2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.