Skip to content

Commit c8ba505

Browse files
authored
Internal script to rename files as per standard (#117)
Fixed naming conventions in all unit tests, added name validation to ci task
1 parent ce27ce0 commit c8ba505

File tree

132 files changed

+247
-147
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+247
-147
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ lazy val spark_scala = (project in file("spark-scala"))
715715

716716
addCommandAlias(
717717
"ci",
718-
";compile;test:compile;it:compile;scalafmtCheckAll;test"
718+
";compile;test:compile;it:compile;scalafmtCheckAll;validateUnitTestNames;test"
719719
)
720720

721721
addCommandAlias(

cats-effects/src/test/scala/com/baeldung/scala/catseffects/CancellationSpec.scala renamed to cats-effects/src/test/scala/com/baeldung/scala/catseffects/CancellationUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean
1010

1111
import scala.concurrent.duration.DurationInt
1212

13-
class CancellationSpec extends AnyWordSpec with Matchers {
13+
class CancellationUnitTest extends AnyWordSpec with Matchers {
1414

1515
"Cancellation" should {
1616
"cancel the fiber directly and execute the action on cancellation" in {

cats-effects/src/test/scala/com/baeldung/scala/catseffects/ErrorHandlingSpec.scala renamed to cats-effects/src/test/scala/com/baeldung/scala/catseffects/ErrorHandlingUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.scalatest.wordspec.AnyWordSpec
1010
import scala.util.Try
1111
import scala.util.control.NoStackTrace;
1212

13-
class ErrorHandlingSpec extends AnyWordSpec with Matchers {
13+
class ErrorHandlingUnitTest extends AnyWordSpec with Matchers {
1414

1515
"Calculator" should {
1616
"return left value using attempt in case of division by zero" in {

internal-scripts/ClassRenamer.scala

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/** This is an internal scala-cli script written just to reduce manual work of
2+
* renaming files and class names to follow naming standards. This doesn't fully correct all the class names,
3+
* however reduces the manual effort by 90%.
4+
*/
5+
//> using toolkit default
6+
import os._
7+
8+
object RenameClassNames {
9+
def main(args: Array[String]): Unit = {
10+
val directory = os.pwd / os.up
11+
12+
os.walk(directory)
13+
.filter(_.ext == "scala")
14+
.filter(_.toString.contains("src/test/scala"))
15+
.filterNot(_.toString.endsWith("UnitTest.scala"))
16+
.filter(f =>
17+
f.toString.endsWith("Test.scala") || f.toString.endsWith("Spec.scala") || f.toString.endsWith("Tests.scala") || f.toString.endsWith("Suite.scala")
18+
)
19+
.foreach { _filePath =>
20+
val _fileName = _filePath.last
21+
22+
val newFileName = _filePath.toString
23+
.replace("Test.scala", "UnitTest.scala")
24+
.replace("Spec.scala", "UnitTest.scala")
25+
.replace("Suite.scala", "UnitTest.scala")
26+
.replace("Tests.scala", "UnitTest.scala")
27+
28+
// rename file
29+
val newFilePath = os.Path(newFileName)
30+
os.move(_filePath, newFilePath)
31+
32+
val content = os.read(newFilePath)
33+
val existingClassName = getClassFromContent(content)
34+
35+
val fileNameWithoutExtension = newFilePath.last.dropRight(6)
36+
37+
def isTestClass(existingClassName: String): Boolean = {
38+
existingClassName
39+
.endsWith("Spec") || existingClassName.endsWith("Test")
40+
}
41+
42+
// Rename the class if it doesn't match the filename
43+
if (
44+
isTestClass(
45+
existingClassName
46+
) && existingClassName.toLowerCase != fileNameWithoutExtension.toLowerCase
47+
) {
48+
// Update the content with the new class name
49+
val updatedContent = content.replaceAll(
50+
s"class\\s+$existingClassName",
51+
s"class $fileNameWithoutExtension"
52+
)
53+
54+
// Now, rename the class
55+
os.write.over(newFilePath, updatedContent)
56+
println(
57+
s"Renamed class in ${newFilePath.last} to $fileNameWithoutExtension"
58+
)
59+
}
60+
}
61+
}
62+
63+
// Extract class name from file content
64+
def getClassFromContent(content: String): String = {
65+
val classNameRegex = """class\s+(\w+)""".r
66+
val matchResult = classNameRegex.findFirstMatchIn(content)
67+
matchResult match {
68+
case Some(m) => m.group(1)
69+
case None => ""
70+
}
71+
}
72+
}

play-scala/application-tests/test/com/baeldung/arrival/actions/SourceActionsTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/actions/SourceActionsUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import play.api.mvc.Headers
88
import play.api.mvc.Results.NoContent
99
import play.api.test.{FakeRequest, Helpers}
1010

11-
class SourceActionsTest
11+
class SourceActionsUnitTest
1212
extends AnyWordSpec
1313
with SourceActions
1414
with ScalaFutures {

play-scala/application-tests/test/com/baeldung/arrival/controller/ArrivalControllerH2Test.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/controller/ArrivalControllerH2UnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import play.api.libs.ws.{WSClient, WSResponse}
1111

1212
import scala.concurrent.Future
1313

14-
class ArrivalControllerH2Test
14+
class ArrivalControllerH2UnitTest
1515
extends AnyWordSpec
1616
with WsScalaTestClient
1717
with GuiceOneServerPerTest

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalDecoratorServiceTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalDecoratorServiceUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.scalatestplus.play.MixedPlaySpec
55
import play.api.Configuration
66
import play.api.inject.guice.GuiceApplicationBuilder
77

8-
class ArrivalDecoratorServiceTest extends MixedPlaySpec {
8+
class ArrivalDecoratorServiceUnitTest extends MixedPlaySpec {
99

1010
"ArrivalDecoratorService#decorate" should {
1111
"mark as short an arrival with plane name length = 5" in new App(

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceH2Test.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceH2UnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.scalatestplus.play.guice.GuiceOneAppPerTest
88

99
import scala.language.postfixOps
1010

11-
class ArrivalServiceH2Test
11+
class ArrivalServiceH2UnitTest
1212
extends AnyWordSpec
1313
with GuiceOneAppPerTest
1414
with ScalaFutures

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceIsolatedTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceIsolatedUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.scalatestplus.play.guice.GuiceOneAppPerTest
1313
import play.api.inject.guice.GuiceApplicationBuilder
1414
import play.api.{Application, Configuration, inject}
1515

16-
class ArrivalServiceIsolatedTest
16+
class ArrivalServiceIsolatedUnitTest
1717
extends AnyWordSpec
1818
with GuiceOneAppPerTest
1919
with ScalaFutures {

play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceMocksTest.scala renamed to play-scala/application-tests/test/com/baeldung/arrival/service/ArrivalServiceMocksUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import slick.dbio.{DBIO, SuccessAction}
1515

1616
import scala.concurrent.Future
1717

18-
class ArrivalServiceMocksTest
18+
class ArrivalServiceMocksUnitTest
1919
extends AnyWordSpec
2020
with GuiceOneAppPerTest
2121
with ScalaFutures

0 commit comments

Comments
 (0)