From 7de9dbd35399d6c25073aa01eb96b57d4768ab11 Mon Sep 17 00:00:00 2001
From: Nathan Daly <nhdaly@gmail.com>
Date: Fri, 20 Jul 2018 11:08:27 -0400
Subject: [PATCH 1/2] Separate out the build files for each example.

Now, each file in test/build_examples/<f>.jl is a standalone example build
file for its corresponding examples/<f>.jl file. test/BuildApp.jl now
simply cycles through these example files.
---
 test/BuildApp.jl             | 45 ++++++++++++++----------------------
 test/build_examples/blink.jl | 23 ++++++++++++++++++
 test/build_examples/hello.jl | 10 ++++++++
 3 files changed, 50 insertions(+), 28 deletions(-)
 create mode 100644 test/build_examples/blink.jl
 create mode 100644 test/build_examples/hello.jl

diff --git a/test/BuildApp.jl b/test/BuildApp.jl
index 5f67fd8..0f12df8 100644
--- a/test/BuildApp.jl
+++ b/test/BuildApp.jl
@@ -1,15 +1,11 @@
 using Base.Test
 using ApplicationBuilder; using BuildApp;
 
-examples_blink = joinpath(@__DIR__, "..", "examples", "blink.jl")
-examples_hello = joinpath(@__DIR__, "..", "examples", "hello.jl")
-
 builddir = mktempdir()
 @assert isdir(builddir)
 
 @testset "HelloWorld.app" begin
-@test 0 == BuildApp.build_app_bundle(examples_hello;
-                             verbose=true, appname="HelloWorld", builddir=builddir)
+@test 0 == include("build_examples/hello.jl")
 @test isdir("$builddir/HelloWorld.app")
 @test success(`$builddir/HelloWorld.app/Contents/MacOS/hello`)
 
@@ -25,33 +21,26 @@ builddir = mktempdir()
 end
 end
 
+
+function testRunAndKillProgramSucceeds(cmd)
+    out, _, p = readandwrite(cmd) # Make sure it runs correctly
+    sleep(1)
+    process_exited(p) && (println("Test Failed: failed to launch: \n", readstring(out)); return false)
+    sleep(10)
+    process_exited(p) && (println("Test Failed: Process died: \n", readstring(out)); return false)
+    # Manually kill program after it's been running for a bit.
+    kill(p); sleep(1)
+    process_exited(p) || (println("Test Failed: Process failed to exit: \n", readstring(out)); return false)
+    return true
+end
+
 @testset "HelloBlink.app" begin
-blinkPkg = Pkg.dir("Blink")
-httpParserPkg = Pkg.dir("HttpParser")
-mbedTLSPkg = Pkg.dir("MbedTLS")
-
-@test 0 == BuildApp.build_app_bundle(examples_blink;
-    verbose = true,
-    resources = [joinpath(blinkPkg, "deps","Julia.app"),
-                 joinpath(blinkPkg, "src","AtomShell","main.js"),
-                 joinpath(blinkPkg, "src","content","main.html"),
-                 joinpath(blinkPkg, "res")],
-    libraries = [joinpath(httpParserPkg, "deps","usr","lib","libhttp_parser.dylib"),
-                 joinpath(mbedTLSPkg, "deps","usr","lib","libmbedcrypto.2.dylib")],
-    appname="HelloBlink", builddir=builddir)
+@test 0 == include("build_examples/blink.jl")
 
 @test isdir("$builddir/HelloBlink.app")
 # Test that it copied the correct files
 @test isdir("$builddir/HelloBlink.app/Contents/Libraries")
 @test isfile("$builddir/HelloBlink.app/Contents/Resources/main.js")
-
-# Manually kill HelloBlink, since it waits for user input.
-@async begin
-    sleep(15) # wait til blink has started up
-    run(`pkill blink`)
-end
-try # expect failure due to pkill, so not really much to test.
-    run(`$builddir/HelloBlink.app/Contents/MacOS/blink`)
-end
-
+# Test that it runs correctly
+@test testRunAndKillProgramSucceeds(`$builddir/HelloBlink.app/Contents/MacOS/blink`)
 end
diff --git a/test/build_examples/blink.jl b/test/build_examples/blink.jl
new file mode 100644
index 0000000..25afe9a
--- /dev/null
+++ b/test/build_examples/blink.jl
@@ -0,0 +1,23 @@
+using ApplicationBuilder; using BuildApp
+
+examples_blink = joinpath(@__DIR__, "..", "..", "examples", "blink.jl")
+
+# Allow this file to be called either as a standalone file to build the above
+# example, or from runtests.jl using a provided builddir.
+isdefined(:builddir) || (builddir=mktempdir())
+
+blinkPkg = Pkg.dir("Blink")
+httpParserPkg = Pkg.dir("HttpParser")
+mbedTLSPkg = Pkg.dir("MbedTLS")
+
+@assert blinkPkg != nothing "Blink is not installed!"
+
+BuildApp.build_app_bundle(examples_blink;
+    verbose = true,
+    resources = [joinpath(blinkPkg, "deps","Julia.app"),
+                 joinpath(blinkPkg, "src","AtomShell","main.js"),
+                 joinpath(blinkPkg, "src","content","main.html"),
+                 joinpath(blinkPkg, "res")],
+    libraries = [joinpath(httpParserPkg, "deps","usr","lib","libhttp_parser.dylib"),
+                 joinpath(mbedTLSPkg, "deps","usr","lib","libmbedcrypto.2.dylib")],
+    appname="HelloBlink", builddir=builddir)
diff --git a/test/build_examples/hello.jl b/test/build_examples/hello.jl
new file mode 100644
index 0000000..88c4b80
--- /dev/null
+++ b/test/build_examples/hello.jl
@@ -0,0 +1,10 @@
+using ApplicationBuilder; using BuildApp
+
+examples_hello = joinpath(@__DIR__, "..", "..", "examples", "hello.jl")
+
+# Allow this file to be called either as a standalone file to build the above
+# example, or from runtests.jl using a provided builddir.
+isdefined(:builddir) || (builddir=mktempdir())
+
+BuildApp.build_app_bundle(examples_hello;
+                          verbose=true, appname="HelloWorld", builddir=builddir)

From e8f262f089197fe868ca59c2a565a4a7c9f10bd2 Mon Sep 17 00:00:00 2001
From: Nathan Daly <nhdaly@gmail.com>
Date: Fri, 20 Jul 2018 11:31:41 -0400
Subject: [PATCH 2/2] Make Blink lib names generic by reading off Module.

---
 test/build_examples/blink.jl | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/test/build_examples/blink.jl b/test/build_examples/blink.jl
index 25afe9a..0f76ff8 100644
--- a/test/build_examples/blink.jl
+++ b/test/build_examples/blink.jl
@@ -12,12 +12,16 @@ mbedTLSPkg = Pkg.dir("MbedTLS")
 
 @assert blinkPkg != nothing "Blink is not installed!"
 
+using Blink
+
 BuildApp.build_app_bundle(examples_blink;
     verbose = true,
     resources = [joinpath(blinkPkg, "deps","Julia.app"),
                  joinpath(blinkPkg, "src","AtomShell","main.js"),
                  joinpath(blinkPkg, "src","content","main.html"),
                  joinpath(blinkPkg, "res")],
-    libraries = [joinpath(httpParserPkg, "deps","usr","lib","libhttp_parser.dylib"),
-                 joinpath(mbedTLSPkg, "deps","usr","lib","libmbedcrypto.2.dylib")],
+    # Get the current library names directly from the packages that use them,
+    # which keeps this build script robust against lib version changes.
+    libraries = [HttpParser.lib,
+                 MbedTLS.libmbedcrypto],
     appname="HelloBlink", builddir=builddir)