Skip to content

Commit

Permalink
Fix bug on crate renaming (#36)
Browse files Browse the repository at this point in the history
Meuse was not generating the correct json metadata when a dependency
was renamed.
Fix #36
  • Loading branch information
mcorbin committed Feb 9, 2021
1 parent 250ee13 commit 9f10885
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/meuse/crate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@
(alength #^bytes byte-array))))))

(def git-metadata-keys [:name :vers :deps :cksum :features :yanked :links])
(def deps-metadata-keys [:name :version_req :features :optional :default_features :target :kind :registry :explicit_name_in_toml])
(def deps-keys-renamed {:version_req :req
:explicit_name_in_toml :package})
(def deps-metadata-keys [:name :version_req :features :optional :default_features :target :kind :registry :package])
(def deps-keys-renamed {:version_req :req})

(defn handler-crate-renaming
"Handle renaming for a dependency.
:explicit_name_in_toml is the new name of the dependency if the dependency
is renamed. If set, the git metadata should have this value in :name and
the old/real name in :package"
[dep]
(if-let [new-name (:explicit_name_in_toml dep)]
(assoc dep
:name new-name
:package (:name dep))
dep))

(defn raw-metadata->metadata
"Converts the raw metadata from `cargo publish` into metadata which will be
stored in the Git repository."
[raw-metadata]
(-> (select-keys raw-metadata git-metadata-keys)
(update :deps (fn [deps] (map handler-crate-renaming deps)))
(update :deps (fn [deps] (map #(select-keys % deps-metadata-keys) deps)))
(update :deps (fn [deps] (map #(set/rename-keys % deps-keys-renamed) deps)))))

Expand Down
34 changes: 29 additions & 5 deletions test/meuse/crate_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
(defn create-publish-request
[metadata crate-file]
(let [metadata (-> metadata json/generate-string .getBytes)
metadata-length [(bit-and (unchecked-byte (count metadata)) 0xFF)
size (count metadata)
metadata-length [(bit-and (unchecked-byte size) 0xFF)
(byte 0)
(byte 0)
(byte 0)]
Expand All @@ -43,23 +44,46 @@
(nil? (check-size (byte-array 10) 11)))))

(deftest request->crate-test
(testing "valid request"
(testing "valid request without renaming"
(let [crate-file "random content"
metadata {:name "bar"
:vers "1.0.1"
:yanked false
:foo "bar"
:deps [{:version_req "0.1.0"
:foo "bar"
:explicit_name_in_toml "toto"}]
:name "foo"}]
:cksum (digest/sha-256 crate-file)}
request (create-publish-request metadata crate-file)]
(is (= {:raw-metadata metadata
:git-metadata {:name "bar"
:vers "1.0.1"
:yanked false
:deps [{:req "0.1.0"
:package "toto"}]
:deps [{:name "foo"
:req "0.1.0"}]
:cksum (digest/sha-256 crate-file)}
:crate-file (String. (.getBytes crate-file))}
(-> (request->crate request)
(update :crate-file #(String. #^bytes %)))))))
(testing "valid request with renaming"
(let [crate-file "random content"
metadata {:name "bar"
:vers "1.0.1"
:yanked false
:foo "bar"
:deps [{:version_req "0.1.0"
:foo "bar"
:name "foo"
:explicit_name_in_toml "renamed"}]
:cksum (digest/sha-256 crate-file)}
request (create-publish-request metadata crate-file)]
(is (= {:raw-metadata metadata
:git-metadata {:name "bar"
:vers "1.0.1"
:yanked false
:deps [{:name "renamed"
:req "0.1.0"
:package "foo"}]
:cksum (digest/sha-256 crate-file)}
:crate-file (String. (.getBytes crate-file))}
(-> (request->crate request)
Expand Down

1 comment on commit 9f10885

@mexus
Copy link

@mexus mexus commented on 9f10885 Feb 9, 2021

Choose a reason for hiding this comment

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

👍 🥇

Please sign in to comment.