Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue where if the first resource is not a script, the resource is silently dropped #670

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading