From 04a63e47199babb155070559105731d9aefffcae Mon Sep 17 00:00:00 2001 From: Hendrik Cannoodt Date: Wed, 3 Apr 2024 10:00:53 +0200 Subject: [PATCH] Fix an issue where if the first resource is not a script, the resource is silently dropped (#670) * Fix an issue where if the first resource is not a script, the resource is silently dropped * Fill in PR # * Update src/main/scala/io/viash/functionality/Functionality.scala Co-authored-by: Robrecht Cannoodt * Add other edge case in test --------- Co-authored-by: Robrecht Cannoodt --- CHANGELOG.md | 2 + .../viash/functionality/Functionality.scala | 5 +- .../viash/functionality/MainScriptTest.scala | 108 ++++++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/test/scala/io/viash/functionality/MainScriptTest.scala diff --git a/CHANGELOG.md b/CHANGELOG.md index f4bc9e316..4fd27ac06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ * `Executable`: Check whether a multiple output file argument contains a wildcard (PR #639). +* `Resources`: Fix an issue where if the first resource is not a script, the resource is silently dropped (PR #670). + # Viash 0.8.5 (2024-02-21): Bug fixes and documentation improvements Fix a bug when building a test docker container which requires a test resource. Additional improvements for the website documentation and support for the latest version of Nextflow are added. diff --git a/src/main/scala/io/viash/functionality/Functionality.scala b/src/main/scala/io/viash/functionality/Functionality.scala index eaf657381..790af8b53 100644 --- a/src/main/scala/io/viash/functionality/Functionality.scala +++ b/src/main/scala/io/viash/functionality/Functionality.scala @@ -372,9 +372,10 @@ case class Functionality( } def mainCode: Option[String] = mainScript.flatMap(_.read) // provide function to use resources.tail but that allows resources to be an empty list + // If mainScript ends up being None because the first resource isn't a script, return the whole list def additionalResources = resources match { - case _ :: tail => tail - case _ => List.empty[Resource] + case head :: tail if head.isInstanceOf[Script] => tail + case list => list } def isEnabled: Boolean = status != Status.Disabled diff --git a/src/test/scala/io/viash/functionality/MainScriptTest.scala b/src/test/scala/io/viash/functionality/MainScriptTest.scala new file mode 100644 index 000000000..ab485abcc --- /dev/null +++ b/src/test/scala/io/viash/functionality/MainScriptTest.scala @@ -0,0 +1,108 @@ +package io.viash.functionality + +import org.scalatest.funsuite.AnyFunSuite +import io.viash.helpers.Logger +import io.viash.functionality.resources.{BashScript, PlainFile} + +class MainScriptTest extends AnyFunSuite { + Logger.UseColorOverride.value = Some(false) + + test("Single script") { + val resources = List(BashScript(path = Some("foo.sh"))) + val fun = Functionality("fun", resources = resources) + + val mainScript = fun.mainScript + val additionalResources = fun.additionalResources + + assert(mainScript.isDefined) + assert(mainScript.get.path == Some("foo.sh")) + assert(additionalResources.isEmpty) + } + + test("Multiple scripts") { + val resources = List( + BashScript(path = Some("foo.sh")), + BashScript(path = Some("bar.sh")), + BashScript(path = Some("baz.sh")), + ) + val fun = Functionality("fun", resources = resources) + + val mainScript = fun.mainScript + val additionalResources = fun.additionalResources + + assert(mainScript.isDefined) + assert(mainScript.get.path == Some("foo.sh")) + assert(additionalResources.length == 2) + } + + test("Single text file") { + val resources = List(PlainFile(path = Some("foo.txt"))) + val fun = Functionality("fun", resources = resources) + + val mainScript = fun.mainScript + val additionalResources = fun.additionalResources + + assert(mainScript.isEmpty) + assert(additionalResources.length == 1) + assert(additionalResources.head.path == Some("foo.txt")) + } + + test("Multiple text files") { + val resources = List( + PlainFile(path = Some("foo.txt")), + PlainFile(path = Some("bar.txt")), + PlainFile(path = Some("baz.txt")), + ) + val fun = Functionality("fun", resources = resources) + + val mainScript = fun.mainScript + val additionalResources = fun.additionalResources + + assert(mainScript.isEmpty) + assert(additionalResources.length == 3) + } + + test("Mixed Script and text files, first is script") { + val resources = List( + BashScript(path = Some("foo.sh")), + PlainFile(path = Some("bar.txt")), + BashScript(path = Some("baz.sh")), + ) + + val fun = Functionality("fun", resources = resources) + + val mainScript = fun.mainScript + val additionalResources = fun.additionalResources + + assert(mainScript.isDefined) + assert(mainScript.get.path == Some("foo.sh")) + assert(additionalResources.length == 2) + } + + test("Mixed Script and text files, first is text file") { + val resources = List( + PlainFile(path = Some("foo.txt")), + BashScript(path = Some("bar.sh")), + PlainFile(path = Some("baz.txt")), + ) + + val fun = Functionality("fun", resources = resources) + + val mainScript = fun.mainScript + val additionalResources = fun.additionalResources + + assert(mainScript.isEmpty) + assert(additionalResources.length == 3) + } + + test("No resources") { + val resources = List.empty + val fun = Functionality("fun", resources = resources) + + val mainScript = fun.mainScript + val additionalResources = fun.additionalResources + + assert(mainScript.isEmpty) + assert(additionalResources.isEmpty) + } +} \ No newline at end of file