-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathSbtCompletionsSuite.scala
212 lines (185 loc) · 5.91 KB
/
SbtCompletionsSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package scalafix.internal.sbt
import coursierapi.{MavenRepository, Repository}
import org.eclipse.jgit.lib.AbbreviatedObjectId
import org.scalatest.Tag
import sbt.complete.Parser
import org.scalatest.funsuite.AnyFunSuite
class SbtCompletionsSuite extends AnyFunSuite {
val fs = new Fs()
val git = new Git(fs.workingDirectory)
fs.mkdir("foo")
fs.add("foo/f.scala", "object F")
fs.mkdir("bar")
fs.add("bar/b.scala", "object B")
fs.mkdir("buzz")
fs.add("my.conf", "conf.file = []")
git.commit()
// create at least 20 commits for diff-base log
(1 to 20).foreach { i =>
fs.add(s"f$i", s"f$i")
git.commit()
}
git.tag("v0.1.0")
val exampleDependency = {
import sbt._
"ch.epfl.scala" %% "example-scalafix-rule" % "1.4.0"
}
val mainArgs =
ScalafixInterface
.fromToolClasspath(
"2.12",
Seq(exampleDependency),
Seq(
Repository.central,
MavenRepository.of(
"https://oss.sonatype.org/content/repositories/snapshots"
)
)
)()
val loadedRules = mainArgs.availableRules.toList
val parser = new ScalafixCompletions(
workingDirectory = fs.workingDirectory.toAbsolutePath,
loadedRules = () => loadedRules,
terminalWidth = None
).parser
def checkCompletion(name: String, testTags: Tag*)(
assertCompletions: (List[String], List[String]) => Unit
): Unit = {
val option =
if (name == "all") ""
else name
test(name, testTags: _*) {
val input = " " + option
val completions =
Parser.completions(parser, input, 0).get.toList.sortBy(_.display)
val appends = completions.map(_.append)
val displays = completions.map(_.display)
assertCompletions(appends, displays)
}
}
def checkArgs(
name: String,
testTags: Tag*
)(assertArgs: Either[String, ShellArgs] => Unit): Unit = {
test(name, testTags: _*) {
val input = name
val args = Parser.parse(" " + input, parser)
assertArgs(args)
}
}
def isSha1(in: String): Boolean =
AbbreviatedObjectId.isId(in)
checkCompletion("all", SkipWindows) { (_, displays) =>
val obtained = displays.mkString("\n").trim
val expected =
"""|
|--auto-suppress-linter-errors
|--check
|--diff
|--diff-base=
|--files=
|--help
|--no-cache
|--rules=
|--stdout
|--syntactic
|--verbose
|--version
|DisableSyntax
| Reports an error for disabled features such as var or XML literals.
|ExplicitResultTypes
| Inserts type annotations for inferred public members. Only compatible with Scala 2.11.12, 2.12.13, 2.12.14, 2.12.15, 2.13.4, 2.13.5, 2.13.6.
|LeakingImplicitClassVal
| Adds 'private' to val parameters of implicit value classes
|NoAutoTupling
| Inserts explicit tuples for adapted argument lists for compatibility with -Yno-adapted-args
|NoValInForComprehension
| Removes deprecated val inside for-comprehension binders
|ProcedureSyntax
| Replaces deprecated procedure syntax with explicit ': Unit ='
|RemoveUnused
| Removes unused imports and terms that reported by the compiler under -Ywarn-unused
|SemanticRule
|SyntacticRule
|class:
|dependency:
|file:
|github:
|replace:
|""".stripMargin.trim.replaceAll("\r\n", "\n")
if (obtained != expected) {
println("\"\"\"|")
obtained.linesIterator.foreach { line =>
print(" |")
println(line)
}
println(" |\"\"\"")
fail("obtained != expected")
}
}
checkCompletion("--fil") { (appends, _) =>
assert(appends == Seq("es="))
}
// only provide values suggestion after the key of a key/value arg
checkCompletion("--diff-base ") { (appends, _) =>
assert(appends.nonEmpty)
assert(!appends.contains("--help"))
}
checkCompletion("--diff-base=", SkipWindows) { (appends, displays) =>
// branches
assert(displays.contains("master"))
// tag
assert(displays.contains("v0.1.0"))
// last 20 commits
appends.zip(displays).reverse.take(20).foreach { case (append, display) =>
assert(isSha1(append))
assert(display.endsWith("ago)"))
}
}
checkCompletion("-r=") { (appends, _) =>
assert(appends.contains("DisableSyntax"))
assert(appends.contains("class:"))
assert(!appends.contains("--help"))
}
checkCompletion("--rules file:bar/../", SkipWindows) { (appends, displays) =>
// resolve parent directories
assert(appends.contains("foo"))
assert(displays.contains("bar/../foo"))
}
// shortcut for --rules
checkArgs("ProcedureSyntax") { args =>
assert(args == Right(ShellArgs(rules = List("ProcedureSyntax"))))
}
// consume extra
checkArgs("--bash") { args =>
assert(args == Right(ShellArgs(extra = List("--bash"))))
}
checkArgs("--files --test") { args =>
assert(args == Left("""missing or invalid value
| --files --test
| ^""".stripMargin))
}
checkArgs("--test --rules=Foo --files=NotHere", SkipWindows) { args =>
assert(args == Left("""--files=NotHere
|missing or invalid value
| --test --rules=Foo --files=NotHere
| ^""".stripMargin))
}
checkArgs("--test -f= --rules=Foo", SkipWindows) { args =>
assert(args == Left("""Expected non-whitespace character
|missing or invalid value
| --test -f= --rules=Foo
| ^""".stripMargin))
}
checkArgs("--test -f --rules=Foo", SkipWindows) { args =>
assert(args == Left("""missing or invalid value
| --test -f --rules=Foo
| ^""".stripMargin))
}
checkArgs("--test --rules=Foo -f", SkipWindows) { args =>
assert(args == Left("""-f
|missing or invalid value
| --test --rules=Foo -f
| ^""".stripMargin))
}
}