Skip to content

Commit

Permalink
Merge pull request #4 from timothypratley/detect-changes
Browse files Browse the repository at this point in the history
basic change detection
  • Loading branch information
daslu committed Aug 17, 2023
2 parents c89138a + f2f4558 commit 3945b5b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
22 changes: 10 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
# Install just one or all simultaneously
# The value must indicate a particular version of the tool, or use 'latest'
# to always provision the latest version
cli: 1.10.1.693 # Clojure CLI based on tools.deps
cli: latest # Clojure CLI based on tools.deps

# Optional step:
- name: Cache Clojure dependencies
Expand All @@ -38,20 +38,18 @@ jobs:
~/.deps.clj
# List all files containing dependencies:
key: cljdeps-${{ hashFiles('deps.edn') }}
# key: cljdeps-${{ hashFiles('deps.edn', 'bb.edn') }}
# key: cljdeps-${{ hashFiles('project.clj') }}
# key: cljdeps-${{ hashFiles('build.boot') }}
restore-keys: cljdeps-

- name: Generate tests
run: clojure -M:dev:gen --verbose

- name: Execute tests
run: clojure -M:test
- name: Execute tests and jar
run: clojure -T:build ci

- name: Deploy
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'org.scicloj/note-to-test'
env:
CLOJARS_PASSWORD: ${{ secrets.CLOJARSTOKEN }}
CLOJARS_USERNAME: myusername
run: clojure -T:build deploy
# TODO: for this to work: change myusername to a valid Clojars username, generate a token, add it as a secret in github project settings
# - name: Deploy
# if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'org.scicloj/note-to-test'
# env:
# CLOJARS_PASSWORD: ${{ secrets.CLOJARSTOKEN }}
# CLOJARS_USERNAME: myusername
# run: clojure -T:build deploy
2 changes: 2 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
:ns-default build}
:dev {:extra-paths ["test" "notebooks"]
:extra-deps {scicloj/tablecloth {:mvn/version "7.000-beta-51"}}}
;; `clojure -T:build test` will run the tests
:test {:extra-paths ["test" "notebooks"]
:extra-deps {org.clojure/test.check {:mvn/version "1.1.1"}
io.github.cognitect-labs/test-runner
{:git/tag "v0.5.1" :git/sha "dfb30dd"}
scicloj/tablecloth {:mvn/version "7.000-beta-51"}}}
;; `clojure -M:dev:gen verbose` will create the tests
:gen {:main-opts ["-m" "scicloj.note-to-test.v1.main"]}}}
6 changes: 3 additions & 3 deletions src/scicloj/note_to_test/v1/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
(gentest! \"notebooks/dum/dummy.clj\")
```
"
[source-path]
[source-path options]
(-> source-path
impl/prepare-context
impl/write-tests!))
(impl/write-tests! options)))

(defn gentests!
"Generate tests for all source files discovered in [dirs]."
Expand All @@ -29,7 +29,7 @@
^File file (file-seq (io/file dir))
:when (impl/clojure-source? file)]
(when verbose (println "Loading file" (str file)))
(cond-> (gentest! file)
(cond-> (gentest! file options)
verbose (println))))
[:success]))

Expand Down
42 changes: 26 additions & 16 deletions src/scicloj/note_to_test/v1/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@
(defn prepare-context [source-path]
(try
(load-file (str source-path))
(catch Exception e
(throw (ex-info "note-to-test: Exception on lode-file"
{:source-path source-path
:exception e}))))
(catch Exception ex
(throw (ex-info (str "note-to-test: Exception on load-file '" source-path "'")
{:source-path source-path}
ex))))
(let [forms (read-forms source-path)
ns-form (->> forms
(filter (begins-with? 'ns))
Expand Down Expand Up @@ -187,21 +187,31 @@
:codes-for-tests codes-for-tests}))


(defn write-tests! [context]
(let [{:keys [ns-symbol
(defn write-tests! [context options]
(let [{:keys [verbose accept]} options
{:keys [ns-symbol
ns-requires
test-ns-symbol
test-ns-requires
test-path
codes-for-tests]} context]
codes-for-tests]} context
prev-file (io/file test-path)
prev-content (when (.exists prev-file)
(slurp prev-file))
content (->> codes-for-tests
(map-indexed (fn [i code]
(->test code i (find-ns ns-symbol))))
(cons (->test-ns test-ns-symbol
test-ns-requires))
(string/join "\n"))]
(when verbose
(cond
(nil? prev-content) (println "note-to-test: CREATING" test-path)
(= content prev-content) (println "note-to-test: NO CHANGES" test-path)
;; TODO: perhaps print a nice diff? or give the line/column?
(not= content prev-content) (println "note-to-test: CHANGING" test-path)))
(when (not accept)
(throw (ex-info "note-to-test: Changes detected with --accept false" {})))
(io/make-parents test-path)
(->> codes-for-tests
(map-indexed (fn [i code]
(->test code
i
(find-ns ns-symbol))))
(cons (->test-ns test-ns-symbol
test-ns-requires))
(string/join "\n")
(spit test-path))
(spit test-path content)
[:wrote test-path]))
12 changes: 5 additions & 7 deletions src/scicloj/note_to_test/v1/main.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
(ns scicloj.note-to-test.v1.main
(:require [clojure.edn :as edn]
[clojure.tools.cli :as cli]
(:require [clojure.tools.cli :as cli]
[scicloj.note-to-test.v1.api :as ntt]))

(def cli-options
[["-d" "--dirs" :default ["notebooks"]]
["-a" "--accept"]
["-a" "--accept" :default true]
["-v" "--verbose"]
["-h" "--help"]])

(defn -main [& args]
(let [{:keys [options summary]} (cli/parse-opts args cli-options)
{:keys [exit-message ok? dirs]} options]
(if exit-message
(do (println exit-message)
(System/exit (if ok? 0 1)))
{:keys [dirs help]} options]
(if help
(println summary)
(ntt/gentests! dirs options))))

0 comments on commit 3945b5b

Please sign in to comment.