From 9f108852cf25c268f38f9c574664da13e23af71c Mon Sep 17 00:00:00 2001 From: mcorbin Date: Tue, 9 Feb 2021 19:43:25 +0100 Subject: [PATCH] Fix bug on crate renaming (#36) Meuse was not generating the correct json metadata when a dependency was renamed. Fix https://github.com/mcorbin/meuse/issues/36 --- src/meuse/crate.clj | 18 +++++++++++++++--- test/meuse/crate_test.clj | 34 +++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/meuse/crate.clj b/src/meuse/crate.clj index d77bc9e..29d1531 100644 --- a/src/meuse/crate.clj +++ b/src/meuse/crate.clj @@ -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))))) diff --git a/test/meuse/crate_test.clj b/test/meuse/crate_test.clj index 0999d6a..9db329f 100644 --- a/test/meuse/crate_test.clj +++ b/test/meuse/crate_test.clj @@ -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)] @@ -43,7 +44,7 @@ (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" @@ -51,15 +52,38 @@ :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)