summaryrefslogtreecommitdiff
path: root/crashbox-worker/src/test/scala/io/crashbox/ci/build/ShellBuildersSpec.scala
blob: 941980463ed63fe52a049cca27454a906f2a9259 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package io.crashbox.ci
package build

import java.io.{ BufferedOutputStream, ByteArrayOutputStream, File }
import java.nio.file.Files
import java.net.URL
import org.eclipse.jgit.util.Paths
import scala.concurrent.duration._
import scala.concurrent.Await
import org.scalatest._

class ShellBuildersSpec extends FlatSpec with Matchers with ShellBuilders {

  val Timeout = 10.seconds

  def runScript(script: String): (Int, String, String) = {
    val stdout = new ByteArrayOutputStream(4096)
    val stderr = new ByteArrayOutputStream(4096)

    val result = TestUtil.withTempDir{ dir =>
      val exec = new File(dir, "crashbox")
      exec.createNewFile()
      Files.write(exec.toPath, script.getBytes)
      exec.setExecutable(true)

      Await.result(build(dir, stdout, stderr), Timeout)
    }
    stdout.close()
    stderr.close()

    (result, new String(stdout.toByteArray(), "utf-8"), new String(stderr.toByteArray(), "utf-8"))
  }

  "ShellBuilders" should "run a shell script" in {
    val script = """|#!/bin/sh
                    |echo "hello world"
                    |echo "foo" >&2
                    |""".stripMargin
    val (res, stdout, stderr) = runScript(script: String)

    assert(res == 0)
    assert(stdout == "hello world\n")
    assert(stderr == "foo\n")
  }

  it should "report a failed script" in {
    val script = """|#!/bin/sh
                    |exit 1
                    |""".stripMargin
    val (res, _, _) = runScript(script: String)

    assert(res == 1)
  }

}