Skip to content

Commit

Permalink
Fix an issue where if the first resource is not a script, the resourc…
Browse files Browse the repository at this point in the history
…e 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 <rcannood@gmail.com>

* Add other edge case in test

---------

Co-authored-by: Robrecht Cannoodt <rcannood@gmail.com>
  • Loading branch information
Grifs and rcannood authored Apr 3, 2024
1 parent 6fcf7b6 commit 04a63e4
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/io/viash/functionality/Functionality.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
108 changes: 108 additions & 0 deletions src/test/scala/io/viash/functionality/MainScriptTest.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 04a63e4

Please sign in to comment.