First things first, let's install clojure
locally. For Windows Subsytem for Linux, this is what I ran. You can find instructions for your specific system at Install Clojure.
sudo apt-get update
sudo apt install openjdk-17-jre-headless
sudo apt install rlwrap
curl -O https://download.clojure.org/install/linux-install-1.11.1.1208.sh && chmod +x linux-install-1.11.1.1208.sh
sudo ./linux-install-1.11.1.1208.sh
rm ./linux-install-1.11.1.1208.sh
- Let's start with .gitignore so we don't commit files we don't need to. (You can find it)
- To manage our project, we will use the default Clojure CLI Tools. To do that we need to create a basic
deps.edn
file in the root:
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}}
:aliases {:dev {:extra-paths ["test"]}
;; clj -X:test-runner
:test-runner {:extra-paths ["test"]
:extra-deps {io.github.cognitect-labs/test-runner
{:git/tag "v0.5.1" :git/sha "dfb30dd"}}
:main-opts ["-m" "cognitect.test-runner"]
:exec-fn cognitect.test-runner.api/test}}}
So far we have defined our dependencies (just Clojure for now) and two aliases which will come in handy during development and tests. dev
alias helps us load the test
directory during development and test-runner
helps us run all the tests in our project.
- Time to add some code. Let's start with
src/core.clj
:
(ns core)
(defn plus [a b]
(+ a b))
(defn run [opts]
(println "Hello world, the sum of 2 and 2 is" (plus 2 2)))
And now a basic test test/core_test.clj
:
(ns core-test
(:require [clojure.test :refer [is deftest]]
[core :refer [plus]]))
(deftest adding-numbers
(is (= 4 (plus 2 2))))
To test that everything above works properly try the following two commands:
clj -X:test-runner
; ...
; Ran 1 tests containing 1 assertions.
; ...
clj -X core/run
; Hello world, the sum of 2 and 2 is 4
Personally, I use VSCode with the Calva Extension for local development but you can have your pick from the several options suggested here.
Now that we have a basic project set up, let's setup some basic GitHub actions.
To start with, let's add a workflow to run tests everytime we push to master. Create a file .github/workflows/test.yaml
:
name: Test 🧪
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Prepare Java
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
- name: Prepare Clojure
uses: DeLaGuardo/[email protected]
with:
cli: 1.11.1.1208
- name: Run Tests
run: clojure -X:test-runner
It might be useful to trigger jobs manually. Let's add a workflow that we can trigger from the GitHub UI which outputs our hello world
. Create a file .github/workflow/manual.yaml
:
name: Manual 🏃
on: [workflow_dispatch]
jobs:
manual:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Prepare Java
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
- name: Prepare Clojure
uses: DeLaGuardo/[email protected]
with:
cli: 1.11.1.1208
- name: Run
run: clojure -X core/run