{"id":415,"date":"2024-10-19T10:34:07","date_gmt":"2024-10-19T02:34:07","guid":{"rendered":"https:\/\/cococat.top\/?p=415"},"modified":"2024-10-19T10:34:07","modified_gmt":"2024-10-19T02:34:07","slug":"rust-note-9-cargo","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/10\/19\/rust-note-9-cargo\/","title":{"rendered":"rust \u7b14\u8bb0 9\uff1acargo \u6742\u8bb0"},"content":{"rendered":"<h2>1. \u547d\u4ee4\u884c\u8fd0\u884c\u7279\u5b9a\u5355\u6d4b<\/h2>\n<p>\u603b\u6709\u9700\u8981\u7528\u547d\u4ee4\u884c\u8dd1\u5355\u6d4b\u7684\u573a\u666f\uff0c\u6bd4\u5982 vscode \u4e0a\u7684 rust-analyzer \u63d2\u4ef6\u7a81\u7136\u574f\u6389\u3002\u3002\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u9700\u8981\uff1a<\/p>\n<pre><code class=\"language-shell\">cargo test --package my_project --bin test -- api::handler::test --show-output\ncargo test --package my_project --lib -- api::handler::test --show-output\n<\/code><\/pre>\n<p>\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>package\uff1a\u8fd9\u4e2a\u662f\u4e3a\u4e86 workspace \u6a21\u5f0f\u800c\u8bbe\u8ba1\u7684\u3002\u5bf9\u4e8e\u5c0f\u578b\u9879\u76ee\u4e2d\u5e38\u89c1\u7684\u5355 package workspace\uff0c\u5c31\u7528 Cargo.toml \u91cc\u5b9a\u4e49\u7684\u5305\u540d\u5373\u53ef\u3002<\/li>\n<li>bin\uff1a\u6307\u5b9a\u4e8c\u8fdb\u5236\u5bf9\u8c61\u540d\u5b57\u3002\u5982\u679c\u9700\u8981\u6307\u5b9a src\/bin \u76ee\u5f55\u4e0b\u7684\u5355\u6d4b\u5bf9\u8c61\uff0c\u5219\u7528\u8fd9\u4e2a\u3002\u82e5\u65e0\uff0c\u5219\u8fd8\u662f\u7528\u548c Cargo.toml \u540c\u540d\u7684\u5373\u53ef<\/li>\n<li>lib\uff1a\u5982\u679c\u662f lib \u5305\u4e2d\u7684\u5355\u6d4b\u5219\u7528\u8fd9\u4e2a<\/li>\n<\/ul>\n<p>\u6700\u540e\u6307\u5b9a\u6d4b\u8bd5\u5bf9\u8c61\uff0c\u6ce8\u610f\u524d\u9762\u8981\u6709\u4e00\u4e2a <code> -- <\/code> \u3002<br \/>\n\u5047\u8bbe\u6211\u4eec\u5728 src\/api\/handler.rs \u4e2d\u5b58\u5728\u8fd9\u6837\u7684\u5355\u6d4b\u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"language-rust\">#[cfg(test)]\nmod tests {\n    #[test]\n    fn test_1() {\n        \/\/ ....\n    }\n\n    #[test]\n    fn test_2() {\n        \/\/....\n    }\n}<\/code><\/pre>\n<p>\u90a3\u4e48\uff0c\u53ef\u4ee5\u7528\u8fd9\u6837\u4e24\u79cd\u65b9\u5f0f\u8fd0\u884c\u5355\u6d4b\uff1a<\/p>\n<ul>\n<li><code>-- api::handler::tests<\/code> \u8fd0\u884c <code>tests<\/code> module \u4e2d\u7684\u6240\u6709\u5355\u6d4b<\/li>\n<li><code>-- api::handler::tests::test_2 --exact<\/code> \u6307\u5b9a\u8fd0\u884c test_2 \u8fd9\u4e2a\u5355\u6d4b\u51fd\u6570<\/li>\n<\/ul>\n<h2>2.  no authentication methods succeeded \u95ee\u9898<\/h2>\n<p>\u8fd9\u4e2a\u95ee\u9898\u5e38\u89c1\u4e8e\u4f01\u4e1a\u5185\u7684\u5f00\u53d1\uff0ccargo \u65e0\u6cd5\u62c9\u53d6\u79c1\u6709 gitlab \u4e0a\u7684\u4ee3\u7801\uff0c\u5bfc\u81f4\u65e0\u6cd5\u7f16\u8bd1\u3002\u89e3\u51b3\u65b9\u6848\u5176\u5b9e\u5728\u62a5\u9519\u4fe1\u606f\u4e2d\u5c31\u6709\u8bf4\u660e\uff1a<\/p>\n<pre><code>Caused by:\nfailed to authenticate when downloading repository\n* attempted ssh-agent authentication, but no usernames succeeded: `git`\n\nif the git CLI succeeds then `net.git-fetch-with-cli` may help here\nhttps:\/\/doc.rust-lang.org\/cargo\/reference\/config.html#netgit-fetch-with-cli<\/code><\/pre>\n<p>\u5373\uff0c\u5728\u5de5\u7a0b\u6839\u76ee\u5f55\u521b\u5efa .cargo\/config.toml \uff0c\u5e76\u5728\u5176\u4e2d\u52a0\u5165\u5982\u4e0b\u7247\u6bb5\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-toml\">[net]\ngit-fetch-with-cli = true<\/code><\/pre>\n<p>\u8fd9\u6837 cargo \u4f1a\u8c03\u7528\u7cfb\u7edf\u4e0a\u7684 git \u4e8c\u8fdb\u5236\u6765\u62c9\u4ee3\u7801\uff0c\u800c\u5927\u5bb6\u7684 git \u60f3\u5fc5\u662f\u5df2\u7ecf\u914d\u7f6e\u597d\u4e86\u4f01\u4e1a\u4ed3\u5e93\u7684\u9274\u6743\u7684\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u4e3b\u8981\u8fd8\u662f cargo \u9ed8\u8ba4\u4f7f\u7528\u7684 libgit2 \u5bf9\u9274\u6743\u652f\u6301\u4e0d\u53cb\u597d\u5bfc\u81f4\u7684\u3002\u66f4\u8be6\u7ec6\u7684\u8bf4\u660e\u53ef\u4ee5\u53c2\u8003 <a href=\"https:\/\/docs.shipyard.rs\/configuration\/git-fetch-with-cli.html\">https:\/\/docs.shipyard.rs\/configuration\/git-fetch-with-cli.html<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u547d\u4ee4\u884c\u8fd0\u884c\u7279\u5b9a\u5355\u6d4b \u603b\u6709\u9700\u8981\u7528\u547d\u4ee4\u884c\u8dd1\u5355\u6d4b\u7684\u573a\u666f\uff0c\u6bd4\u5982 vscode \u4e0a\u7684 rust-analyzer  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[24],"class_list":["post-415","post","type-post","status-publish","format-standard","hentry","category-rust","tag-rust"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/415\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}