File: cli.rs

package info (click to toggle)
rust-hypothesis 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 472 kB
  • sloc: makefile: 38; sh: 1
file content (260 lines) | stat: -rw-r--r-- 8,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#![cfg(feature = "cli")]

use assert_cmd::Command;
use predicates::prelude::*;
use std::{env, thread, time};

use hypothesis::annotations::Annotation;

fn create_annotation(
    text: &str,
    username: &str,
    key: &str,
    group_id: &str,
) -> color_eyre::Result<String> {
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    let output = cmd
        .env("HYPOTHESIS_NAME", username)
        .env("HYPOTHESIS_KEY", key)
        .arg("annotations")
        .arg("create")
        .arg(&format!("--text={}", text))
        .arg(&format!("--group={}", group_id))
        .arg("www.example.com")
        .assert();
    let stdout = String::from_utf8(output.get_output().stdout.clone())?;
    let stdout = stdout.split("annotation ").last();
    assert!(stdout.is_some());
    Ok(stdout.unwrap().trim().to_string())
}

#[test]
#[ignore = "requires network access"]
fn add_and_delete_annotation() -> color_eyre::Result<()> {
    let group_id = env::var("TEST_GROUP_ID").unwrap_or_else(|_| "__world__".into());
    let username = env::var("HYPOTHESIS_NAME")?;
    let key = env::var("HYPOTHESIS_KEY")?;

    // Create a new annotation
    let id = create_annotation("test annotation comment", &username, &key, &group_id);
    assert!(id.is_ok());
    let id = id.unwrap();

    // Fetch created annotation
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("annotations")
        .arg("fetch")
        .arg(&id)
        .assert()
        .stdout(
            predicate::str::contains(&id).and(predicate::str::contains("test annotation comment")),
        );

    // Delete annotation
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("annotations")
        .arg("delete")
        .arg(&id)
        .assert()
        .stdout(predicate::str::starts_with("Deleted").and(predicate::str::contains(&id)));
    Ok(())
}

#[test]
#[ignore = "requires network access"]
fn update_annotation() -> color_eyre::Result<()> {
    let group_id = env::var("TEST_GROUP_ID").unwrap_or_else(|_| "__world__".into());
    let username = env::var("HYPOTHESIS_NAME")?;
    let key = env::var("HYPOTHESIS_KEY")?;

    // Create a new annotation
    let id = create_annotation("test annotation comment", &username, &key, &group_id);
    assert!(id.is_ok());
    let id = id.unwrap();

    // Update annotation
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("annotations")
        .arg("update")
        .arg("--text=\"test text 2\"")
        .arg(&id)
        .assert()
        .stdout(predicate::str::starts_with("Updated").and(predicate::str::contains(&id)));

    // Fetch updated annotation
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("annotations")
        .arg("fetch")
        .arg(&id)
        .assert()
        .stdout(predicate::str::contains(&id).and(predicate::str::contains("test text 2")));

    // Delete annotation
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("annotations")
        .arg("delete")
        .arg(&id)
        .assert()
        .success();
    Ok(())
}

#[test]
#[ignore = "requires network access"]
fn search_annotations() -> color_eyre::Result<()> {
    let group_id = env::var("TEST_GROUP_ID").unwrap_or_else(|_| "__world__".into());
    let username = env::var("HYPOTHESIS_NAME")?;
    let key = env::var("HYPOTHESIS_KEY")?;
    let ids = (0..4)
        .map(|i| create_annotation(&format!("test text {}", i), &username, &key, &group_id))
        .collect::<Result<Vec<_>, _>>()?;

    let duration = time::Duration::from_millis(500);
    thread::sleep(duration);
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    let output = cmd
        .env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("annotations")
        .arg("search")
        .arg("--limit=200")
        .arg(&format!("--group={}", group_id))
        .assert();
    let stdout = String::from_utf8(output.get_output().stdout.clone())?;
    let mut count = 0;
    for annotation in serde_json::Deserializer::from_str(&stdout).into_iter::<Annotation>() {
        if ids.contains(&annotation?.id) {
            count += 1;
        }
    }
    assert_eq!(count, 4);
    for id in ids {
        let mut cmd = Command::cargo_bin("hypothesis-client")?;
        cmd.env("HYPOTHESIS_NAME", &username)
            .env("HYPOTHESIS_KEY", &key)
            .arg("annotations")
            .arg("delete")
            .arg(id)
            .assert()
            .success();
    }
    Ok(())
}

fn create_group(
    name: &str,
    description: &str,
    username: &str,
    key: &str,
) -> color_eyre::Result<String> {
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    let output = cmd
        .env("HYPOTHESIS_NAME", username)
        .env("HYPOTHESIS_KEY", key)
        .arg("groups")
        .arg("create")
        .arg(name)
        .arg(description)
        .assert();
    let stdout = String::from_utf8(output.get_output().stdout.clone())?;
    let stdout = stdout.split("group ").last();
    assert!(stdout.is_some());
    Ok(stdout.unwrap().trim().to_string())
}

#[test]
#[ignore = "requires network access"]
fn create_and_leave_group() -> color_eyre::Result<()> {
    let username = env::var("HYPOTHESIS_NAME")?;
    let key = env::var("HYPOTHESIS_KEY")?;

    // Create a new group
    let group_id = create_group("test_name", "test description with spaces", &username, &key);
    assert!(group_id.is_ok());
    let group_id = group_id.unwrap();

    // Fetch created group
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("groups")
        .arg("fetch")
        .arg(&group_id)
        .assert()
        .stdout(predicate::str::contains(&group_id).and(predicate::str::contains("test_name")));

    // Leave group
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("groups")
        .arg("leave")
        .arg(&group_id)
        .assert()
        .stdout(predicate::str::starts_with(format!(
            "Left group {}",
            &group_id
        )));
    Ok(())
}

#[test]
#[ignore = "requires network access"]
fn update_group() -> color_eyre::Result<()> {
    let username = env::var("HYPOTHESIS_NAME")?;
    let key = env::var("HYPOTHESIS_KEY")?;

    // Create a new group
    let group_id = create_group("test_name", "test description with spaces", &username, &key);
    assert!(group_id.is_ok());
    let group_id = group_id.unwrap();

    // Update group
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("groups")
        .arg("update")
        .arg(&group_id)
        .arg("--name=test_group_2")
        .arg("--description=\"new description\"")
        .assert()
        .stdout(predicate::str::starts_with(format!(
            "Updated group {}",
            &group_id
        )));

    // Check that update worked
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("groups")
        .arg("fetch")
        .arg(&group_id)
        .assert()
        .stdout(predicate::str::contains(&group_id).and(predicate::str::contains("test_group_2")));

    // Leave group
    let mut cmd = Command::cargo_bin("hypothesis-client")?;
    cmd.env("HYPOTHESIS_NAME", &username)
        .env("HYPOTHESIS_KEY", &key)
        .arg("groups")
        .arg("leave")
        .arg(&group_id)
        .assert()
        .stdout(predicate::str::starts_with(format!(
            "Left group {}",
            group_id
        )));
    Ok(())
}