Skip to content

Commit

Permalink
test: functional tests for output file
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKozhin committed Jul 20, 2020
1 parent e0165a4 commit 0ea2dd1
Showing 1 changed file with 58 additions and 28 deletions.
86 changes: 58 additions & 28 deletions e2e/src/test/scala/e2e/FunctionalSpec.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package e2e

import java.io.{ByteArrayOutputStream, File, FileWriter, IOException, StringReader}
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, File, FileWriter, IOException, StringReader}
import java.nio.file.attribute.{AclEntry, AclEntryPermission, AclEntryType, AclFileAttributeView}
import java.nio.file.{Files, Path}
import java.nio.file.attribute.{AclEntry, AclEntryPermission, AclEntryType, AclFileAttributeView, PosixFilePermissions}

import org.scalatest.Assertion
import org.scalatest.concurrent.TimeLimitedTests
import org.scalatest.matchers.should
import org.scalatest.matchers.should.Matchers
import org.scalatest.time.{Seconds, Span}
import org.scalatest.wordspec.AnyWordSpec
Expand All @@ -16,18 +14,18 @@ import scala.io.BufferedSource

class FunctionalSpec extends AnyWordSpec with Matchers {
trait Files {
val inputFile: File = File.createTempFile("input", "txt");
val outputFile: File = File.createTempFile("invalid", "txt");
val inputFile: File = File.createTempFile("input", ".txt")
val outputFile: File = File.createTempFile("invalid", ".txt")

val writer = new FileWriter(inputFile);
val writer = new FileWriter(inputFile)
val source: BufferedSource = scala.io.Source.fromFile(outputFile)
}
def checkWithIncorrectArguments(args: Array[String], errorMessage: String) = {
val out = new ByteArrayOutputStream()
Console.withErr(out) {
Main.main(args);
Main.main(args)
}
out.toString should (include(errorMessage))
out.toString should include(errorMessage)
}

@throws[IOException]
Expand Down Expand Up @@ -56,26 +54,26 @@ class FunctionalSpec extends AnyWordSpec with Matchers {
"help mode" should {
"show usage-message when no arguments provided" in {
pendingUntilFixed {
val args = new Array[String](0);
Main.main(args);
val args = new Array[String](0)
Main.main(args)
val input = new StringReader("")
val out = new ByteArrayOutputStream()
Console.withIn(input) {
Console.withOut(out) {
Main.main(args);
Main.main(args)
}
}
out.toString shouldBe ("java -jar target/scala-2.13/geney.jar <options>")
out.toString shouldBe "java -jar target/scala-2.13/geney.jar <options>"
}
}
/*"show help-message when is called with option -h or --help" in new Files {
pendingUntilFixed {
val args: Array[String] = Array("-h", "--input", inputFile.getAbsolutePath);
val args: Array[String] = Array("-h", "--input", inputFile.getAbsolutePath)
val in = new StringReader("")
val out = new ByteArrayOutputStream()
Console.withIn(in) {
Console.withOut(out) {
Main.main(args);
Main.main(args)
}
}
out.toString should(include("CliParser") and
Expand Down Expand Up @@ -111,7 +109,7 @@ class FunctionalSpec extends AnyWordSpec with Matchers {
val args = Array("--input", inputFile.getAbsolutePath, "-k", "10", "-f", "fasta")
writer.write("ATGC")
writer.close()
Main.main(args);
Main.main(args)
} should have message "Incorrect format of input file."
}
}
Expand All @@ -121,7 +119,7 @@ class FunctionalSpec extends AnyWordSpec with Matchers {
val args = Array("--input", inputFile.getAbsolutePath, "-k", "10", "-f", "fastq")
writer.write("ATGC")
writer.close()
Main.main(args);
Main.main(args)
} should have message "Incorrect format of input file."
}
}
Expand All @@ -142,10 +140,20 @@ class FunctionalSpec extends AnyWordSpec with Matchers {
}
}
"Incorrect argument --output" should {
"show error-message when output file already exists" in new Files {
"show error message if output file already exists without '--force' flag" in new Files {
val args =
Array("--input", inputFile.getAbsolutePath, "--output", outputFile.getAbsolutePath, "-k", "10", "-f", "fasta")
checkWithIncorrectArguments(args, "Output file already exists")
writer.write(">id description\n ABCDE")

val out = new ByteArrayOutputStream()

Console.withOut(out) {
Main.main(args)
}

out.toString should include(
"File" + outputFile.getAbsolutePath + "already exists.\n Use flag '--force' to override existing file."
)
}
"show error-message when user does not have access to output file" in new Files {
val inaccessiblePath = inaccessible(Files.createTempDirectory("dirWithNoAccess")).toFile.getAbsolutePath
Expand All @@ -169,27 +177,49 @@ class FunctionalSpec extends AnyWordSpec with Matchers {
"use stdout when argument --output is not provided" in new Files {
pendingUntilFixed {
val args = Array("--input", inputFile.getAbsolutePath, "-k", "10", "-f", "fasta")
Main.main(args);
writer.write(">id description\n ABCDE");
writer.close(); val out = new ByteArrayOutputStream()
Main.main(args)
writer.write(">id description\n ABCDE")
writer.close()
val out = new ByteArrayOutputStream()
Console.withOut(out) {
Main.main(args);
Main.main(args)
}
out.toString shouldBe ("")
out.toString shouldBe ""
}
}
"use stdin when argument --input is not provided" in new Files {
pendingUntilFixed {
val outputPath = inputFile.getParent + "/output.txt";
val outputPath = inputFile.getParent + "/output.txt"
val args = Array("--output", outputPath, "-k", "10", "-f", "fasta")
Main.main(args);
Main.main(args)
val in = new StringReader(">id description\n ABCDE")
Console.withIn(in) {
Main.main(args);
Main.main(args)
}
source.getLines().toString() shouldBe ("")
source.getLines().toString() shouldBe ""
}
}
"use existing output file if flag '--force' is provided" in new Files {
val args =
Array("--input",
inputFile.getAbsolutePath,
"--output",
outputFile.getAbsolutePath,
"-k",
"10",
"-f",
"fasta",
"--force")
writer.write(">id description\n ABCDE")

val out = new ByteArrayOutputStream()

Console.withOut(out) {
Main.main(args)
}

source.getLines.toList should contain(">geney-cli v. 0.1")
}
}
}
}

0 comments on commit 0ea2dd1

Please sign in to comment.