From 3a9dd291d20342595b14c53fdbcbdd0ec118194e Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 18 Nov 2022 19:48:06 +0100 Subject: [PATCH] [new] [#361] [#362] Add Graal test (@borkdude) --- .github/workflows/graal_test.yml | 32 ++++++++++++++++++++++++++ bb.edn | 10 +++++++++ bb/graal_test.clj | 35 +++++++++++++++++++++++++++++ project.clj | 9 +++++++- test/taoensso/timbre/graal_test.clj | 5 +++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/graal_test.yml create mode 100644 bb.edn create mode 100755 bb/graal_test.clj create mode 100644 test/taoensso/timbre/graal_test.clj diff --git a/.github/workflows/graal_test.yml b/.github/workflows/graal_test.yml new file mode 100644 index 00000000..056e5d31 --- /dev/null +++ b/.github/workflows/graal_test.yml @@ -0,0 +1,32 @@ +name: graal-test +on: [push, pull_request] + +jobs: + test: + strategy: + matrix: + java: ['17'] + os: [ubuntu-latest, macOS-latest, windows-latest] + + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - uses: graalvm/setup-graalvm@v1 + with: + version: 'latest' + java-version: ${{ matrix.java }} + components: 'native-image' + github-token: ${{ secrets.GITHUB_TOKEN }} + + - uses: DeLaGuardo/setup-clojure@10.0 + with: + lein: latest + bb: latest + + - uses: actions/cache@v3 + with: + path: ~/.m2/repository + key: deps-${{ hashFiles('deps.edn') }} + restore-keys: deps- + + - run: bb test-graal diff --git a/bb.edn b/bb.edn new file mode 100644 index 00000000..b3a0d682 --- /dev/null +++ b/bb.edn @@ -0,0 +1,10 @@ +{:paths ["bb"] + :tasks + {:requires ([graal-test]) + test-graal + {:doc "Run native-image tests" + :task + (do + (graal-test/uberjar) + (graal-test/native-image) + (graal-test/test-native-image))}}} diff --git a/bb/graal_test.clj b/bb/graal_test.clj new file mode 100755 index 00000000..54793e99 --- /dev/null +++ b/bb/graal_test.clj @@ -0,0 +1,35 @@ +#!/usr/bin/env bb + +(ns graal-test + (:require + [clojure.string :as str] + [babashka.fs :as fs] + [babashka.process :refer [shell]])) + +(defn uberjar [] + (let [command "lein with-profiles +graal-test uberjar" + command (if (fs/windows?) + (if (fs/which "lein") + command + ;; assume powershell module + (str "powershell.exe -command " (pr-str command))) + command)] + (shell command))) + +(defn executable [dir name] + (-> (fs/glob dir (if (fs/windows?) (str name ".{exe,bat,cmd}") name)) + first + fs/canonicalize + str)) + +(defn native-image [] + (let [graalvm-home (System/getenv "GRAALVM_HOME") + bin-dir (str (fs/file graalvm-home "bin"))] + (shell (executable bin-dir "gu") "install" "native-image") + (shell (executable bin-dir "native-image") "-jar" "target/graal-test.jar" "--no-fallback" "graal_test"))) + +(defn test-native-image [] + (let [{:keys [out]} + (shell {:out :string} (executable "." "graal_test") "1" "2" "3")] + (assert (str/includes? out (str '("1" "2" "3"))) out) + (println "Native image works!"))) diff --git a/project.clj b/project.clj index 26388948..67d57b49 100644 --- a/project.clj +++ b/project.clj @@ -64,7 +64,14 @@ [com.taoensso/nippy "3.2.0"] [com.taoensso/carmine "3.1.0" :exclusions [com.taoensso/timbre]] - [com.draines/postal "2.0.5"]]}} + [com.draines/postal "2.0.5"]]} + + :graal-test + {:dependencies [[org.clojure/clojure "1.11.1"] + [com.github.clj-easy/graal-build-time "0.1.4"]] + :main taoensso.timbre.graal-test + :aot [taoensso.timbre.graal-test] + :uberjar-name "graal-test.jar"}} :test-paths ["test" #_"src"] diff --git a/test/taoensso/timbre/graal_test.clj b/test/taoensso/timbre/graal_test.clj new file mode 100644 index 00000000..b320275c --- /dev/null +++ b/test/taoensso/timbre/graal_test.clj @@ -0,0 +1,5 @@ +(ns taoensso.timbre.graal-test + (:require [taoensso.timbre :as timbre]) + (:gen-class)) + +(defn -main [& args] (timbre/info args))