|
| 1 | +package scalafix.tests.config |
| 2 | + |
| 3 | +import metaconfig.Conf |
| 4 | +import metaconfig.Configured |
| 5 | +import metaconfig.typesafeconfig._ |
| 6 | +import org.scalatest.funsuite.AnyFunSuite |
| 7 | +import scalafix.internal.rule.OrganizeImports |
| 8 | +import scalafix.v1.Configuration |
| 9 | + |
| 10 | +class OrganizeImportsConfigSuite extends AnyFunSuite { |
| 11 | + |
| 12 | + test("OrganizeImports should fail when RemoveUnused.imports is enabled") { |
| 13 | + val rawConfig = |
| 14 | + """|rules = [OrganizeImports] |
| 15 | + |OrganizeImports.removeUnused = true |
| 16 | + |RemoveUnused.imports = true |
| 17 | + |""".stripMargin |
| 18 | + |
| 19 | + val conf = Conf.parseString("test", rawConfig).get |
| 20 | + val config = |
| 21 | + Configuration().withConf(conf).withScalacOptions(List("-Wunused")) |
| 22 | + val rule = new OrganizeImports() |
| 23 | + |
| 24 | + val result = rule.withConfiguration(config) |
| 25 | + |
| 26 | + assert( |
| 27 | + result.isNotOk, |
| 28 | + "Expected OrganizeImports to fail with RemoveUnused.imports=true" |
| 29 | + ) |
| 30 | + |
| 31 | + result match { |
| 32 | + case Configured.NotOk(error) => |
| 33 | + val errorMsg = error.msg |
| 34 | + assert( |
| 35 | + errorMsg.contains("RemoveUnused.imports") && |
| 36 | + errorMsg.contains("OrganizeImports") && |
| 37 | + errorMsg.contains("should not be used together"), |
| 38 | + s"Error message should mention the conflict. Got: $errorMsg" |
| 39 | + ) |
| 40 | + case _ => |
| 41 | + fail("Expected Configured.NotOk") |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + test("OrganizeImports should succeed when RemoveUnused.imports is disabled") { |
| 46 | + val rawConfig = |
| 47 | + """|rules = [OrganizeImports] |
| 48 | + |OrganizeImports.removeUnused = true |
| 49 | + |RemoveUnused.imports = false |
| 50 | + |""".stripMargin |
| 51 | + |
| 52 | + val conf = Conf.parseString("test", rawConfig).get |
| 53 | + val config = |
| 54 | + Configuration().withConf(conf).withScalacOptions(List("-Wunused")) |
| 55 | + val rule = new OrganizeImports() |
| 56 | + |
| 57 | + val result = rule.withConfiguration(config) |
| 58 | + |
| 59 | + assert( |
| 60 | + result.isOk, |
| 61 | + "Expected OrganizeImports to succeed with RemoveUnused.imports=false" |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + test("OrganizeImports should succeed when RemoveUnused is not configured") { |
| 66 | + val rawConfig = |
| 67 | + """|rules = [OrganizeImports] |
| 68 | + |OrganizeImports.removeUnused = true |
| 69 | + |""".stripMargin |
| 70 | + |
| 71 | + val conf = Conf.parseString("test", rawConfig).get |
| 72 | + val config = |
| 73 | + Configuration().withConf(conf).withScalacOptions(List("-Wunused")) |
| 74 | + val rule = new OrganizeImports() |
| 75 | + |
| 76 | + val result = rule.withConfiguration(config) |
| 77 | + |
| 78 | + assert( |
| 79 | + result.isOk, |
| 80 | + "Expected OrganizeImports to succeed when RemoveUnused is not configured" |
| 81 | + ) |
| 82 | + } |
| 83 | +} |
0 commit comments