Skip to content

Commit

Permalink
Main work
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Jan 10, 2025
1 parent 4902a4e commit 2edba7c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
33 changes: 33 additions & 0 deletions bin/generate-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# Synopsis:
# Generate the tests for each exercise with a generator template.

# Example: generate tests for all exercises with a generator template
# bin/generate-tests

# Example: generate tests for a single exercise with a generator template
# bin/generate-tests two-fer

set -eo pipefail

die() { echo "$*" >&2; exit 1; }

required_tool() {
command -v "${1}" >/dev/null 2>&1 ||
die "${1} is required but not installed. Please install it and make sure it's in your PATH."
}

required_tool clj

exercise_slug="${1}"

pushd generators >/dev/null || die "Could not change to the 'generators' directory"

if [ -z "${exercise_slug}" ]; then
clj -X generator/run
else
clj -X generator/run :exercise "${exercise_slug}"
fi

popd >/dev/null
15 changes: 8 additions & 7 deletions generators/src/generator.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
(ns generator
(:require [canonical-data]
(:require [clojure.string :as str]
[canonical-data]
[templates]
[log]))

(defn- slugs-to-generate [args]
(defn- slugs-to-generate [slug]
(let [slugs templates/exercises-with-template]
(if-let [slug (first args)]
(if (str/blank? slug)
slugs
(if (contains? slugs slug)
[slug]
(log/error (str "No template found for exercise '" slug "'")))
slugs)))
(log/error (str "No template found for exercise '" slug "'"))))))

(defn- run [args]
(defn- run [{:keys [exercise]}]
(canonical-data/sync-repo)
(doseq [slug (slugs-to-generate args)]
(doseq [slug (slugs-to-generate (str exercise))]
(log/normal (str "Generating tests for exercise '" slug "'"))
(templates/generate-tests-file slug (canonical-data/test-cases slug))))
3 changes: 2 additions & 1 deletion generators/src/templates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
(file-seq)
(filter #(.isFile %))
(filter #(= "generator.template" (.getName %)))
(mapv #(-> % (.getParentFile) (.getParentFile) (.getName)))))
(map #(-> % (.getParentFile) (.getParentFile) (.getName)))
(set)))

(defn- render-template [data template]
(selmer/render (slurp template) data))
Expand Down

0 comments on commit 2edba7c

Please sign in to comment.