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
use std::env::{current_dir, set_current_dir, temp_dir};
use std::io::{stdout, Write};
use std::str::FromStr;

use khonsu_tools::publish;
use khonsu_tools::universal::clap::{self, Parser};
use khonsu_tools::universal::devx_cmd::{run, Cmd};
use khonsu_tools::universal::{anyhow, audit, DefaultConfig};
use serde::Serialize;

#[derive(Parser, Debug)]
pub enum Commands {
    TestMatrix,
    Test {
        #[clap(long)]
        fail_on_warnings: bool,
    },
    #[clap(flatten)]
    Tools(khonsu_tools::Commands),
}

fn main() -> anyhow::Result<()> {
    if std::env::args().len() > 1 {
        let command = Commands::parse();
        match command {
            Commands::TestMatrix => generate_test_matrix_output(),
            Commands::Test { fail_on_warnings } => run_all_tests(fail_on_warnings),
            Commands::Tools(command) => command.execute::<Config>(),
        }
    } else {
        run_all_tests(true)
    }
}

enum Config {}

impl khonsu_tools::Config for Config {
    type Publish = Self;
    type Universal = Self;
}

impl khonsu_tools::universal::Config for Config {
    type Audit = Self;
    type CodeCoverage = DefaultConfig;
}

impl audit::Config for Config {
    fn args() -> Vec<String> {
        vec![
            String::from("--all-features"),
            String::from("--exclude=xtask"),
            String::from("--exclude=benchmarks"),
            // examples that include other dependencies, which aren't actually
            // indicative of the security of BonsaiDb.
            String::from("--exclude=axum"),
            String::from("--exclude=acme"),
            String::from("--exclude=view-histogram"),
        ]
    }
}

impl publish::Config for Config {
    fn paths() -> Vec<String> {
        vec![
            String::from("crates/bonsaidb-macros"),
            String::from("crates/bonsaidb-core"),
            String::from("crates/bonsaidb-utils"),
            String::from("crates/bonsaidb-local"),
            String::from("crates/bonsaidb-server"),
            String::from("crates/bonsaidb-client"),
            String::from("crates/bonsaidb-keystorage-s3"),
            String::from("crates/bonsaidb"),
        ]
    }
}

#[derive(Serialize)]
struct TestSuite {
    cargo_args: &'static str,
    toolchain: &'static str,
}

#[derive(Serialize)]
struct TestMatrix {
    include: &'static [TestSuite],
}

fn all_tests() -> &'static [TestSuite] {
    &[
        TestSuite {
            cargo_args: "--all-features",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--all-features",
            toolchain: "1.70",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-core --no-default-features",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-local --no-default-features",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-local --no-default-features --features encryption",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-local --no-default-features --features compression",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-local --no-default-features --features async",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-local --no-default-features --features password-hashing",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-local --no-default-features --features token-authentication",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-local --no-default-features --features encryption,compression",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features --features password-hashing",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features --features token-authentication",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features --features encryption",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features --features encryption,compression",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features --features compression",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features --features websockets",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-server --no-default-features --features acme",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb --no-default-features --features server,client,test-util",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb --no-default-features --features server,client,test-util,token-authentication,websockets",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb --no-default-features --features server,client,test-util,password-hashing,websockets",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb --no-default-features --features server,client,test-util,password-hashing,acme",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb --no-default-features --features server,client,test-util,password-hashing,compression",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb --no-default-features --features server,client,test-util,password-hashing,encryption",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args:
                "--package bonsaidb --no-default-features --features server,client,test-util,acme,websockets",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args:
                "--package bonsaidb --no-default-features --features server,client,test-util,acme,websockets,password-hashing",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-files --no-default-features",
            toolchain: "stable",
        },
        TestSuite {
            cargo_args: "--package bonsaidb-macros",
            toolchain: "stable",
        },
    ]
}

fn generate_test_matrix_output() -> anyhow::Result<()> {
    let stdout = stdout();
    let mut stdout = stdout.lock();
    stdout.write_all(b"test-matrix=")?;
    stdout.write_all(&serde_json::to_vec(all_tests())?)?;
    stdout.write_all(b"\n")?;
    Ok(())
}

fn run_all_tests(fail_on_warnings: bool) -> anyhow::Result<()> {
    let executing_dir = current_dir()?;
    let mut all_tests = all_tests().iter().enumerate().collect::<Vec<_>>();

    if let Some(last_index) = last_succeeded_index() {
        let recently_finished = all_tests.drain(..last_index + 1).collect::<Vec<_>>();
        all_tests.extend(recently_finished);
    }

    for (index, test) in all_tests {
        println!("Running clippy for {}", test.cargo_args);
        let mut clippy = Cmd::new("cargo");
        let mut clippy = clippy
            .arg("clippy")
            .arg("--all-targets")
            .env("RUST_TOOLCHAIN", test.toolchain);

        for arg in test.cargo_args.split(' ') {
            clippy = clippy.arg(arg);
        }

        if fail_on_warnings {
            clippy = clippy.arg("--").arg("-D").arg("warnings");
        }

        clippy.run()?;

        println!("Running tests for {}", test.cargo_args);
        let mut cargo = Cmd::new("cargo");
        let mut cargo = cargo
            .arg("test")
            .arg("--all-targets")
            .env("RUST_TOOLCHAIN", test.toolchain);

        for arg in test.cargo_args.split(' ') {
            cargo = cargo.arg(arg);
        }
        cargo.run()?;
        set_last_succeeded_index(index);
    }

    println!("Running clippy for wasm32 client");
    set_current_dir(executing_dir)?;
    let mut clippy = Cmd::new("cargo");
    let mut clippy = clippy.args([
        "clippy",
        "--target",
        "wasm32-unknown-unknown",
        "--target-dir",
        "target/wasm",
        "--package",
        "bonsaidb-client",
    ]);
    if fail_on_warnings {
        clippy = clippy.arg("--").arg("-D").arg("warnings");
    }

    clippy.run()?;

    println!("Generating docs");
    run!("cargo", "doc", "--all-features", "--no-deps")?;
    Ok(())
}

fn set_last_succeeded_index(index: usize) {
    drop(std::fs::write(
        temp_dir().join("bonsaidb-test-all-index"),
        index.to_string().as_bytes(),
    ));
}

fn last_succeeded_index() -> Option<usize> {
    std::fs::read_to_string(temp_dir().join("bonsaidb-test-all-index"))
        .ok()
        .and_then(|contents| usize::from_str(&contents).ok())
}