From 851aef1d000f1a25c8a6868b8903b2d02cfd8917 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 6 Oct 2019 14:01:56 -0700 Subject: [PATCH 1/2] Fix scripted parser crash In a local progress, I was able to induce a crash in tab completions because the group key did not exist in pairMap. --- main/src/main/scala/sbt/ScriptedPlugin.scala | 2 +- project/Scripted.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main/src/main/scala/sbt/ScriptedPlugin.scala b/main/src/main/scala/sbt/ScriptedPlugin.scala index 0cd518d27..b503781aa 100644 --- a/main/src/main/scala/sbt/ScriptedPlugin.scala +++ b/main/src/main/scala/sbt/ScriptedPlugin.scala @@ -144,7 +144,7 @@ object ScriptedPlugin extends AutoPlugin { // Grabs the filenames from a given test group in the current page definition. def pagedFilenames(group: String, page: ScriptedTestPage): Seq[String] = { - val files = pairMap(group).toSeq.sortBy(_.toLowerCase) + val files = pairMap.get(group).toSeq.flatten.sortBy(_.toLowerCase) val pageSize = files.size / page.total // The last page may loose some values, so we explicitly keep them val dropped = files.drop(pageSize * (page.page - 1)) diff --git a/project/Scripted.scala b/project/Scripted.scala index 30e2868b3..ba2685340 100644 --- a/project/Scripted.scala +++ b/project/Scripted.scala @@ -61,7 +61,7 @@ object Scripted { // Grabs the filenames from a given test group in the current page definition. def pagedFilenames(group: String, page: ScriptedTestPage): Seq[String] = { - val files = pairMap(group).toSeq.sortBy(_.toLowerCase) + val files = pairMap.get(group).toSeq.flatten.sortBy(_.toLowerCase) val pageSize = if (page.total == 0) files.size else files.size / page.total // The last page may loose some values, so we explicitly keep them val dropped = files.drop(pageSize * (page.page - 1)) From 2dc6f2d2d5e4993eab9503dc27b512d48a87d007 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 6 Oct 2019 14:05:56 -0700 Subject: [PATCH 2/2] Limit scripted page numbers The completions were generating page numbers that didn't make sense if there were a small number of scripted tests. For example, suppose that there were only two tests defined, it would generate *1of3 *2of3 and *3of3 completions even though there weren't even three tests. --- main/src/main/scala/sbt/ScriptedPlugin.scala | 5 ++++- project/Scripted.scala | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/main/src/main/scala/sbt/ScriptedPlugin.scala b/main/src/main/scala/sbt/ScriptedPlugin.scala index b503781aa..fe41e772b 100644 --- a/main/src/main/scala/sbt/ScriptedPlugin.scala +++ b/main/src/main/scala/sbt/ScriptedPlugin.scala @@ -136,7 +136,10 @@ object ScriptedPlugin extends AutoPlugin { val groupP = token(id.examples(pairMap.keySet)) <~ token('/') // A parser for page definitions - val pageNumber = NatBasic & not('0', "zero page number") + val pageNumber = (NatBasic & not('0', "zero page number")).flatMap { i => + if (i <= pairs.size) Parser.success(i) + else Parser.failure(s"$i exceeds the number of tests (${pairs.size})") + } val pageP: Parser[ScriptedTestPage] = ("*" ~> pageNumber ~ ("of" ~> pageNumber)) flatMap { case (page, total) if page <= total => success(ScriptedTestPage(page, total)) case (page, total) => failure(s"Page $page was greater than $total") diff --git a/project/Scripted.scala b/project/Scripted.scala index ba2685340..10a52f10d 100644 --- a/project/Scripted.scala +++ b/project/Scripted.scala @@ -53,7 +53,10 @@ object Scripted { val groupP = token(id.examples(pairMap.keySet)) <~ token('/') // A parser for page definitions - val pageNumber = NatBasic & not('0', "zero page number") + val pageNumber = (NatBasic & not('0', "zero page number")).flatMap { i => + if (i <= pairs.size) Parser.success(i) + else Parser.failure(s"$i exceeds the number of tests (${pairs.size})") + } val pageP: Parser[ScriptedTestPage] = ("*" ~> pageNumber ~ ("of" ~> pageNumber)) flatMap { case (page, total) if page <= total => success(ScriptedTestPage(page, total)) case (page, total) => failure(s"Page $page was greater than $total")