aboutsummaryrefslogtreecommitdiff
path: root/tests/tutorial/mod.rs
blob: 5d969cf260b0dc96d08df412526968f193e57b20 (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::env::set_current_dir;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::process::{Command, Stdio};
use std::thread::sleep;
use std::time::Duration;

use rand::prelude::*;

trait ExitStatusExt {
    fn check(self);
}

impl ExitStatusExt for std::process::ExitStatus {
    fn check(self) {
        assert!(self.success());
    }
}

const CARGO: &str = env!("CARGO");
const PROJECT_DIR: &str = env!("CARGO_MANIFEST_DIR");
const TOSIN_ADMIN: &str = env!("CARGO_BIN_EXE_tosin-admin");

fn get(url: &str) -> (hyper::StatusCode, String) {
    let get = async {
        use hyper::Client;

        let client = Client::new();

        let res = client.get(url.parse().unwrap()).await.unwrap();

        let status = res.status();

        let body = hyper::body::to_bytes(res).await.unwrap();
        let body = String::from_utf8_lossy(&body).into_owned();

        (status, body)
    };
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(get)
}

pub fn step1() {
    // tosin-admin start-project {dest}
    set_current_dir(PROJECT_DIR).unwrap();
    set_current_dir("target").unwrap();
    if fs::metadata("tests").is_err() {
        fs::create_dir("tests").unwrap();
    }
    set_current_dir("tests").unwrap();
    fs::write("Cargo.toml", "[workspace]\nmembers = ['tutorial']").unwrap();
    if fs::metadata("tutorial").is_ok() {
        fs::remove_dir_all("tutorial").unwrap();
    }
    Command::new(TOSIN_ADMIN)
        .args(&["start-project", "tutorial"])
        .status()
        .unwrap()
        .check();
    set_current_dir("tutorial").unwrap();
    assert!(fs::metadata("Cargo.toml").is_ok());
    assert!(fs::read_to_string("Cargo.toml")
        .unwrap()
        .contains("tosin = "));
    assert!(fs::metadata("src/main.rs").is_ok());
    assert!(fs::read_to_string("src/main.rs")
        .unwrap()
        .contains("tosin::main!"));

    // cargo run run-server
    let port = thread_rng().gen_range(8081u16..9000u16);
    let mut server = Command::new(CARGO)
        .args(&["run", "run-server", &format!("{}", port)])
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();
    let mut server_output = String::new();
    let server_stdout = server.stdout.take().unwrap();
    let mut server_stdout = BufReader::new(server_stdout);
    server_stdout.read_line(&mut server_output).unwrap();
    assert!(server_output.contains(&format!("http://127.0.0.1:{}", port)));
    sleep(Duration::from_secs_f32(0.5));
    let server_poke = get(&format!("http://127.0.0.1:{}/", port));
    assert_eq!(server_poke.0, hyper::StatusCode::NOT_FOUND);
    if let Ok(Some(exit_status)) = server.try_wait() {
        exit_status.check();
    }
    server.kill().unwrap();

    // tosin-admin start-app polls
    Command::new(TOSIN_ADMIN)
        .args(&["start-app", "polls"])
        .status()
        .unwrap()
        .check();
    assert!(fs::metadata("src/polls/mod.rs").is_ok());

    // write views.rs
    fs::write(
        "src/polls/views.rs",
        r#"
use tosin::http::{Reply, Response};

pub fn index() -> Response {
    "Hello, world. You're at the polls index.".into_response()
}
    "#,
    )
    .unwrap();

    // write urls.rs
    fs::write(
        "src/polls/urls.rs",
        r#"
use tosin::urls::{UrlMap, url_map};

use super::views;

pub fn urls() -> UrlMap {
    url_map! {
        => views::index, // TODO name: "index"
    }
}
    "#,
    )
    .unwrap();

    // update main.rs
    fs::write(
        "src/main.rs",
        r#"
#[macro_use] extern crate diesel;

use tosin::Settings;
use tosin::contrib::admin;
use tosin::db::backend::Connectable;
use tosin::urls::{UrlMap, url_map};

mod polls;

fn urls() -> UrlMap {
    url_map! {
        "polls" / ..polls::urls(),
        "admin" / ..admin::site::urls(),
    }
}

fn settings() -> Settings<impl Connectable> {
    Settings {
        ..Settings::default()
    }
}

tosin::main!(urls(), settings());
    "#,
    )
    .unwrap();

    // poke that new route
    let port = thread_rng().gen_range(8081u16..9000u16);
    let mut server = Command::new(CARGO)
        .args(&["run", "run-server", &format!("{}", port)])
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();
    let mut server_output = String::new();
    let server_stdout = server.stdout.take().unwrap();
    let mut server_stdout = BufReader::new(server_stdout);
    server_stdout.read_line(&mut server_output).unwrap();
    assert!(server_output.contains(&format!("http://127.0.0.1:{}", port)));
    sleep(Duration::from_secs_f32(0.5));
    let server_poke = get(&format!("http://127.0.0.1:{}/polls/", port));
    assert_eq!(server_poke.0, hyper::StatusCode::OK);
    assert_eq!(server_poke.1, "Hello, world. You're at the polls index.");
    server.kill().unwrap();

    // vibe check
    let test_tutorial1 = Path::new("src");
    let example_tutorial1 = Path::new(PROJECT_DIR).join("examples/tutorial01");
    for file in &[
        "main.rs",
        "polls/mod.rs",
        "polls/migrations/mod.rs",
        "polls/models.rs",
        "polls/urls.rs",
        "polls/views.rs",
    ] {
        let test_path = test_tutorial1.join(file);
        let test_file = fs::read_to_string(test_path).unwrap();
        let example_path = example_tutorial1.join(file);
        let example_file = fs::read_to_string(example_path).unwrap();
        assert_eq!(test_file.trim(), example_file.trim());
    }
}

pub fn step2() {
    step1();
    // update main.rs
    fs::write(
        "src/main.rs",
        r#"
#[macro_use] extern crate diesel;

use tosin::Settings;
use tosin::contrib::admin;
use tosin::db::backend::Connectable;
use tosin::urls::{UrlMap, url_map};

mod polls;

fn urls() -> UrlMap {
    url_map! {
        "polls" / ..polls::urls(),
        "admin" / ..admin::site::urls(),
    }
}

fn settings() -> Settings<impl Connectable> {
    Settings {
        installed_apps: &[
            &admin::APP,
        ],
        ..Settings::default()
    }
}

tosin::main!(urls(), settings());
    "#,
    )
    .unwrap();

    // cargo run migrate
    Command::new(CARGO)
        .args(&["run", "migrate"])
        .status()
        .unwrap()
        .check();

    // update Cargo.toml
    fs::write(
        "Cargo.toml",
        r#"
[package]
name = "tutorial"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4"
diesel = { version = "1.4.4", features = ["sqlite"] }
tosin = { path = "../../.." }
    "#,
    )
    .unwrap();

    // update polls/models.rs
    fs::write(
        "src/polls/models.rs",
        r#"
use tosin::db::models::{Model, Id, gather};

#[derive(Model)]
pub struct Question {
    id: Option<Id>,
    #[model(max_length=200)]
    question_text: String,
    /// date published
    pub_date: chrono::NaiveDateTime,
}

#[derive(Model)]
pub struct Choice {
    id: Option<Id>,
    #[model(Question, on_delete=Cascade)]
    question: Id,
    #[model(max_length=200)]
    choice_text: String,
    #[model(default = 0)]
    votes: usize,
}

gather!();
    "#,
    )
    .unwrap();

    // update main.rs
    fs::write(
        "src/main.rs",
        r#"
#[macro_use] extern crate diesel;

use tosin::Settings;
use tosin::contrib::admin;
use tosin::db::backend::Connectable;
use tosin::urls::{UrlMap, url_map};

mod polls;

fn urls() -> UrlMap {
    url_map! {
        "polls" / ..polls::urls(),
        "admin" / ..admin::site::urls(),
    }
}

fn settings() -> Settings<impl Connectable> {
    Settings {
        installed_apps: &[
            &polls::APP,
            &admin::APP,
        ],
        ..Settings::default()
    }
}

tosin::main!(urls(), settings());
    "#,
    )
    .unwrap();

    // cargo run make-migrations
    Command::new(CARGO)
        .args(&["run", "make-migrations"])
        .status()
        .unwrap()
        .check();

    // cargo run migrate
    Command::new(CARGO)
        .args(&["run", "migrate"])
        .status()
        .unwrap()
        .check();
}