Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update subproject clean targets with absolute paths #99

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
- v1-test-
- run: lein deps
- run: lein check
- run: lein install
greglook marked this conversation as resolved.
Show resolved Hide resolved
- run: lein with-profile +ci test2junit
- run: rm -r ~/.m2/repository/lein-monolith
- store_test_results:
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

...
### Changed
- Subproject `:clean-targets` will automatically be updated with absolute paths to support running
the `clean` task as part of `lein monolith each`.
[#99](https://github.com/amperity/lein-monolith/pull/99)


## [1.10.0] - 2024-06-03
Expand Down
4 changes: 3 additions & 1 deletion example/libs/lib-b/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

:dependencies
[[org.clojure/clojure "1.10.1"]
[lein-monolith.example/lib-a "MONOLITH-SNAPSHOT"]])
[lein-monolith.example/lib-a "MONOLITH-SNAPSHOT"]]

:clean-targets ^{:protect false} ["target" "resources"])
2 changes: 1 addition & 1 deletion example/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"version++" ["version+"]}

:plugins
[[lein-monolith "1.10.0"]
[[lein-monolith "1.10.1-SNAPSHOT"]
[lein-pprint "1.2.0"]]

:dependencies
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject lein-monolith "1.10.0"
(defproject lein-monolith "1.10.1-SNAPSHOT"
:description "Leiningen plugin for managing subrojects within a monorepo."
:url "https://github.com/amperity/lein-monolith"
:license {:name "Apache License 2.0"
Expand Down
16 changes: 15 additions & 1 deletion src/lein_monolith/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@
:else nil))


(defn- with-absolute-clean-targets
"Returns the given project map with its clean targets updated to use absolute paths
to work around https://github.com/technomancy/leiningen/issues/2707"
[{:keys [root clean-targets] :as project}]
(let [abs-target-fn (fn [target]
(if (and (string? target)
(not (.isAbsolute (io/file target))))
(str (io/file root target))
target))
abs-clean-targets (with-meta (mapv abs-target-fn clean-targets) (meta clean-targets))]
(assoc project :clean-targets abs-clean-targets)))
DCardenasAmp marked this conversation as resolved.
Show resolved Hide resolved


(defn- read-subproject
"Reads a leiningen project definition from the given directory and returns
the loaded project map, or nil if the directory does not contain a valid
Expand All @@ -129,7 +142,8 @@
(let [project-file (io/file dir "project.clj")]
(when (.exists project-file)
(lein/debug "Reading subproject definition from" (str project-file))
(project/read-raw (str project-file)))))
(-> (project/read-raw (str project-file))
(with-absolute-clean-targets)))))


(defn read-subprojects!
Expand Down
20 changes: 20 additions & 0 deletions test/lein_monolith/config_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns lein-monolith.config-test
(:require
[clojure.java.io :as io]
[clojure.test :refer [deftest is testing]]
[lein-monolith.config :as config]
[lein-monolith.test-utils :refer [read-example-project]]))


(deftest read-subprojects
(testing "subproject clean targets use absolute paths"
(let [monolith (read-example-project)
subprojects (config/read-subprojects! monolith)
test-project (get subprojects 'lein-monolith.example/lib-b)
clean-targets (:clean-targets test-project)]
(is (map? test-project)
"lib-b subproject was loaded")
(is (= {:protect false} (meta clean-targets))
"metadata is preserved")
(is (= (set clean-targets) (set (filter #(.isAbsolute (io/file %)) clean-targets)))
"All clean target paths are absolute"))))
38 changes: 38 additions & 0 deletions test/lein_monolith/task/each_test.clj
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of a script-level example test but this is better!

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns lein-monolith.task.each-test
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[lein-monolith.config :as config]
[lein-monolith.task.each :as each]
[lein-monolith.test-utils :refer [read-example-project]]))


(defn- test-path
"Returns the path where the test file should exist for the given target path."
[subproject target]
(if (= :target-path target)
(str (:root subproject) "/target/test.txt")
(str target "/test.txt")))


(deftest clean-subprojects
(testing "Verify that the clean targets for each subproject are cleaned up by `lein monolith each clean`."
(let [monolith (read-example-project)
subprojects (config/read-subprojects! monolith)]
(doseq [[_subproject-name subproject] subprojects
target (:clean-targets subproject)]
(let [path (test-path subproject target)]
(is (str/starts-with? path (:root subproject))
"The test file path should be created within the subproject directory.")
(io/make-parents path)
(spit path "test")
(is (.exists (io/file path)) "The test file should have been created.")))
(each/run-tasks monolith {} ["clean"]) ; lein monolith each clean
(doseq [[_subproject-name subproject] subprojects
target (:clean-targets subproject)]
(let [path (test-path subproject target)
test-file (io/file path)
parent-dir (io/file (.getParent test-file))]
(is (not (.exists test-file)) "The test file should not exist after a lein clean")
(is (not (.exists parent-dir)) "The target directory should not exist after a lein clean"))))))