浏览代码

CI: add test launching for `near-client`

develop
Silvestr Predko 3 年前
父节点
当前提交
bd2e353512
共有 3 个文件被更改,包括 57 次插入53 次删除
  1. 22
    8
      .github/workflows/ci.yml
  2. 1
    0
      near-client/Cargo.toml
  3. 34
    45
      near-client/tests/rpc.rs

+ 22
- 8
.github/workflows/ci.yml 查看文件

@@ -50,14 +50,13 @@ jobs:
50 50
         uses: actions-rs/toolchain@v1
51 51
         with:
52 52
           toolchain: stable
53
-      - name: Install wasm32-unknown-unknown
54
-        run: rustup target add wasm32-unknown-unknown
53
+          target: wasm32-unknown-unknown
55 54
       - name: cargo build [web-client, near-client, common-api] for wasm
56 55
         run: cargo build -p web-client -p near-client -p common-api --target wasm32-unknown-unknown --release
57 56
       - name: cargo build [near-client, common-api]
58 57
         run: cargo build -p near-client -p common-api --tests --release
59
-  tests:
60
-    name: tests
58
+  tests-common-api:
59
+    name: tests-common-api
61 60
     needs: [clippy, fmt]
62 61
     runs-on: ubuntu-latest
63 62
     steps:
@@ -66,7 +65,22 @@ jobs:
66 65
         uses: actions-rs/toolchain@v1
67 66
         with:
68 67
           toolchain: stable
69
-      - name: Run unit-tests for [common-api]
70
-        run: cargo test -p common-api crypto
71
-      - name: Run integration-tests for [common-api]
72
-        run: cargo test -p common-api --test crypto
68
+      - name: Run tests for [common-api]
69
+        run: cargo test -p common-api --tests
70
+  tests-near:
71
+    name: tests-near
72
+    needs: [clippy, fmt]
73
+    runs-on: ubuntu-latest
74
+    steps:
75
+      - uses: actions/checkout@v3
76
+      - name: Install Rust Toolchain
77
+        uses: actions-rs/toolchain@v1
78
+        with:
79
+          toolchain: stable
80
+          target: wasm32-unknown-unknown
81
+      - name: Set up ssh access 
82
+        uses: webfactory/ssh-agent@v0.5.4
83
+        with:
84
+          ssh-private-key: ${{ secrets.SMARTCONTRACTS_SSH_PRIVATE_KEY }}
85
+      - name: Run tests for [near-client]
86
+        run: cargo test -p near-client --tests

+ 1
- 0
near-client/Cargo.toml 查看文件

@@ -27,5 +27,6 @@ git2 = "0.15.0"
27 27
 reqwest = { version = "0.11", features = ["json"] }
28 28
 rand = "0.8.5"
29 29
 rand_chacha = "0.3"
30
+tempfile = "3"
30 31
 tokio = { version = "1.2.1", features = ["full"] }
31 32
 workspaces = { git = "https://github.com/near/workspaces-rs.git", features = ["unstable"] }

+ 34
- 45
near-client/tests/rpc.rs 查看文件

@@ -8,11 +8,7 @@ use rand::{RngCore, SeedableRng};
8 8
 use rand_chacha::ChaChaRng;
9 9
 use reqwest::Url;
10 10
 use serde_json::json;
11
-use std::{
12
-    fs::{create_dir, read, write},
13
-    path::Path,
14
-    str::FromStr,
15
-};
11
+use std::{fs::write, path::Path, str::FromStr};
16 12
 use workspaces::{network::Sandbox, types::SecretKey, AccountId, Worker};
17 13
 
18 14
 // auxiliary structs and methods
@@ -50,55 +46,41 @@ async fn create_signer(
50 46
 
51 47
 async fn download_contract() -> Vec<u8> {
52 48
     let target = "https://github.com/near-examples/FT/raw/master/res/fungible_token.wasm";
53
-    let target_path = "../target/tmp-contracts";
49
+    let target_path = temp_dir().into_path();
54 50
     let fname = "contract.wasm";
55
-    let full_dest = format!("{}/{}", target_path, fname);
56
-
57
-    if !Path::new(&full_dest).exists() {
58
-        if !Path::new(&target_path).exists() {
59
-            create_dir(target_path).unwrap();
60
-        };
51
+    let full_dest = format!("{}/{}", target_path.to_string_lossy(), fname);
61 52
 
62
-        let contract_bytes = reqwest::get(target).await.unwrap().bytes().await.unwrap();
63
-        write(full_dest, &contract_bytes).unwrap();
64
-        contract_bytes.to_vec()
65
-    } else {
66
-        read(&full_dest).unwrap()
67
-    }
53
+    let contract_bytes = reqwest::get(target).await.unwrap().bytes().await.unwrap();
54
+    write(full_dest, &contract_bytes).unwrap();
55
+    contract_bytes.to_vec()
68 56
 }
69 57
 
70 58
 async fn clone_and_compile_wasm() -> Vec<u8> {
71
-    let tmp_path = "../target/tmp-contracts";
72
-    let repo_path = format!("{}/near-smartcontracts", tmp_path);
59
+    let tmp_path = temp_dir().into_path();
60
+    let repo_path = format!("{}/near-smartcontracts", tmp_path.to_string_lossy());
73 61
     let target_path = format!("{}/test-contract", repo_path);
74 62
     let repo_url = "git@github.com:Relayz-io/near-smartcontracts.git";
75 63
 
76
-    if !Path::new(&target_path).exists() {
77
-        if !Path::new(&tmp_path).exists() {
78
-            create_dir(tmp_path).unwrap();
79
-        }
64
+    // Prepare callbacks.
65
+    let mut callbacks = RemoteCallbacks::new();
66
+    callbacks.credentials(|_, username_from_url, _| {
67
+        let username = username_from_url
68
+            .ok_or_else(|| git2::Error::from_str("Parsing the given URL is failed"))
69
+            .unwrap();
70
+        Cred::ssh_key_from_agent(username)
71
+    });
80 72
 
81
-        // Prepare callbacks.
82
-        let mut callbacks = RemoteCallbacks::new();
83
-        callbacks.credentials(|_, username_from_url, _| {
84
-            let username = username_from_url
85
-                .ok_or_else(|| git2::Error::from_str("Parsing the given URL is failed"))
86
-                .unwrap();
87
-            Cred::ssh_key_from_agent(username)
88
-        });
89
-
90
-        // Prepare fetch options.
91
-        let mut fo = git2::FetchOptions::new();
92
-        fo.remote_callbacks(callbacks);
93
-
94
-        // Prepare builder.
95
-        let mut builder = git2::build::RepoBuilder::new();
96
-        builder.fetch_options(fo);
97
-        builder.branch("main");
98
-
99
-        // Clone the project.
100
-        builder.clone(repo_url, Path::new(&repo_path)).unwrap();
101
-    }
73
+    // Prepare fetch options.
74
+    let mut fo = git2::FetchOptions::new();
75
+    fo.remote_callbacks(callbacks);
76
+
77
+    // Prepare builder.
78
+    let mut builder = git2::build::RepoBuilder::new();
79
+    builder.fetch_options(fo);
80
+    builder.branch("main");
81
+
82
+    // Clone the project.
83
+    builder.clone(repo_url, Path::new(&repo_path)).unwrap();
102 84
 
103 85
     workspaces::compile_project(&target_path).await.unwrap()
104 86
 }
@@ -440,3 +422,10 @@ async fn delete_account() {
440 422
         }
441 423
     ));
442 424
 }
425
+
426
+fn temp_dir() -> tempfile::TempDir {
427
+    tempfile::Builder::new()
428
+        .prefix("near-client-test-")
429
+        .tempdir()
430
+        .unwrap()
431
+}

正在加载...
取消
保存