axoupdater-cli 0.7.2

Self-updater executable for use with cargo-dist
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::env::consts::EXE_SUFFIX;

use axoasset::LocalAsset;
use axoprocess::Cmd;
use camino::{Utf8Path, Utf8PathBuf};
use tempfile::TempDir;

static BIN: &str = env!("CARGO_BIN_EXE_axoupdater");
static RECEIPT_TEMPLATE: &str = r#"{"binaries":["axolotlsay"],"install_prefix":"INSTALL_PREFIX","provider":{"source":"cargo-dist","version":"CARGO_DIST_VERSION"},"source":{"app_name":"axolotlsay","name":"cargodisttest","owner":"mistydemeo","release_type":"github"},"version":"VERSION"}"#;

// Handle aarch64 later
fn triple() -> String {
    match std::env::consts::OS {
        "windows" => "x86_64-pc-windows-msvc".to_owned(),
        "macos" => {
            if std::env::consts::ARCH == "x86_64" {
                "x86_64-apple-darwin".to_owned()
            } else {
                "aarch64-apple-darwin".to_owned()
            }
        }
        "linux" => {
            if std::env::consts::ARCH == "x86_64" {
                "x86_64-unknown-linux-gnu".to_owned()
            } else {
                "aarch64-unknown-linux-gnu".to_owned()
            }
        }
        _ => unimplemented!(),
    }
}

fn axolotlsay_tarball_path(version: &str) -> String {
    let triple = triple();
    format!("https://github.com/mistydemeo/cargodisttest/releases/download/v{version}/axolotlsay-{triple}.tar.gz")
}

fn install_receipt(version: &str, cargo_dist_version: &str, prefix: &Utf8PathBuf) -> String {
    RECEIPT_TEMPLATE
        .replace("INSTALL_PREFIX", &prefix.to_string().replace('\\', "\\\\"))
        .replace("CARGO_DIST_VERSION", cargo_dist_version)
        .replace("VERSION", version)
}

fn write_receipt(
    version: &str,
    cargo_dist_version: &str,
    prefix: &Utf8PathBuf,
) -> std::io::Result<()> {
    let contents = install_receipt(version, cargo_dist_version, prefix);
    let receipt_name = prefix.join("axolotlsay-receipt.json");
    LocalAsset::write_new(&contents, receipt_name).unwrap();

    Ok(())
}

#[test]
fn bails_out_with_default_name() {
    let mut command = Cmd::new(BIN, "execute axoupdater");
    command.check(false);
    let result = command.output().unwrap();
    assert!(!result.status.success());

    let stderr_string = String::from_utf8(result.stderr).unwrap();
    assert!(stderr_string.contains("App name calculated as `axoupdater'"));
}

// Performs an in-place upgrade from an old version to a newer one.
// The process runs like so:
// * Simulate an install of axolotlsay into a temporary directory
// * Write an install receipt to that path
// * Copy this repo's copy of axoupdater into the temporary directory in place of the one that axolotlsay once came with
// * Run axoupdater
// * Confirm that the new binary exists and is a newer version than the one we had before
//
// NOTE: axolotlsay 0.2.115 is a good base version to use because it contains a
//       several noteworthy bugfixes in its installer.
#[test]
fn test_upgrade() -> std::io::Result<()> {
    let tempdir = TempDir::new()?;
    let bindir_path = &tempdir.path().join("bin");
    let bindir = Utf8Path::from_path(bindir_path).unwrap();
    std::fs::create_dir_all(bindir)?;

    let base_version = "0.2.115";

    let url = axolotlsay_tarball_path(base_version);
    let compressed_path =
        Utf8PathBuf::from_path_buf(tempdir.path().join("axolotlsay.tar.gz")).unwrap();

    let client = axoasset::AxoClient::with_reqwest(axoasset::reqwest::Client::new());
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(client.load_and_write_to_file(&url, &compressed_path))
        .unwrap();

    // Write the receipt for the updater to use
    write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;

    LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

    // Now install our copy of the updater instead of the one axolotlsay came with
    let updater_path = bindir.join(format!("axolotlsay-update{EXE_SUFFIX}"));
    std::fs::copy(BIN, &updater_path)?;

    let mut updater = Cmd::new(&updater_path, "run updater");
    updater.env("AXOUPDATER_CONFIG_PATH", bindir);
    // If we're not running in CI, try to avoid ruining the user's PATH.
    if std::env::var("CI").is_err() {
        updater.env("INSTALLER_NO_MODIFY_PATH", "1");
    }
    // We'll do that manually
    updater.check(false);
    let res = updater.output().unwrap();
    let output_stdout = String::from_utf8(res.stdout).unwrap();
    let output_stderr = String::from_utf8(res.stderr).unwrap();

    // Now let's check the version we just updated to
    let new_axolotlsay_path = &bindir.join(format!("axolotlsay{EXE_SUFFIX}"));
    assert!(
        new_axolotlsay_path.exists(),
        "update result was\nstdout\n{}\nstderr\n{}",
        output_stdout,
        output_stderr
    );
    let mut new_axolotlsay = Cmd::new(new_axolotlsay_path, "version test");
    new_axolotlsay.arg("--version");
    let output = new_axolotlsay.output().unwrap();
    let stderr_string = String::from_utf8(output.stdout).unwrap();
    assert!(stderr_string.starts_with("axolotlsay "));
    assert_ne!(stderr_string, format!("axolotlsay {}\n", base_version));

    Ok(())
}

#[test]
fn test_upgrade_allow_prerelease() -> std::io::Result<()> {
    let tempdir = TempDir::new()?;
    let bindir_path = &tempdir.path().join("bin");
    let bindir = Utf8Path::from_path(bindir_path).unwrap();
    std::fs::create_dir_all(bindir)?;

    let base_version = "0.2.115";

    let url = axolotlsay_tarball_path(base_version);
    let compressed_path =
        Utf8PathBuf::from_path_buf(tempdir.path().join("axolotlsay.tar.gz")).unwrap();

    let client = axoasset::AxoClient::with_reqwest(axoasset::reqwest::Client::new());
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(client.load_and_write_to_file(&url, &compressed_path))
        .unwrap();

    // Write the receipt for the updater to use
    write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;

    LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

    // Now install our copy of the updater instead of the one axolotlsay came with
    let updater_path = bindir.join(format!("axolotlsay-update{EXE_SUFFIX}"));
    std::fs::copy(BIN, &updater_path)?;

    let mut updater = Cmd::new(&updater_path, "run updater");
    // If we're not running in CI, try to avoid ruining the user's PATH.
    if std::env::var("CI").is_err() {
        updater.env("INSTALLER_NO_MODIFY_PATH", "1");
    }
    updater.env("AXOUPDATER_CONFIG_PATH", bindir);
    updater.arg("--prerelease");
    // We'll do that manually
    updater.check(false);
    let res = updater.output().unwrap();
    let output_stdout = String::from_utf8(res.stdout).unwrap();
    let output_stderr = String::from_utf8(res.stderr).unwrap();

    // Now let's check the version we just updated to
    let new_axolotlsay_path = &bindir.join(format!("axolotlsay{EXE_SUFFIX}"));
    assert!(
        new_axolotlsay_path.exists(),
        "update result was\nstdout\n{}\nstderr\n{}",
        output_stdout,
        output_stderr
    );
    let mut new_axolotlsay = Cmd::new(new_axolotlsay_path, "version test");
    new_axolotlsay.arg("--version");
    let output = new_axolotlsay.output().unwrap();
    let stderr_string = String::from_utf8(output.stdout).unwrap();
    assert!(stderr_string.starts_with("axolotlsay "));
    assert_ne!(stderr_string, format!("axolotlsay {}\n", base_version));

    Ok(())
}

// A similar test to the one above, but it upgrades to a specific version
// instead of whatever's latest.
#[test]
fn test_upgrade_to_specific_version() -> std::io::Result<()> {
    let tempdir = TempDir::new()?;
    let bindir_path = &tempdir.path().join("bin");
    let bindir = Utf8Path::from_path(bindir_path).unwrap();
    std::fs::create_dir_all(bindir)?;

    let base_version = "0.2.115";
    let target_version = "0.2.116";

    let url = axolotlsay_tarball_path(base_version);
    let compressed_path =
        Utf8PathBuf::from_path_buf(tempdir.path().join("axolotlsay.tar.gz")).unwrap();

    let client = axoasset::AxoClient::with_reqwest(axoasset::reqwest::Client::new());
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(client.load_and_write_to_file(&url, &compressed_path))
        .unwrap();

    // Write the receipt for the updater to use
    write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;

    LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

    // Now install our copy of the updater instead of the one axolotlsay came with
    let updater_path = bindir.join(format!("axolotlsay-update{EXE_SUFFIX}"));
    std::fs::copy(BIN, &updater_path)?;

    let mut updater = Cmd::new(&updater_path, "run updater");
    updater.arg("--version").arg(target_version);
    updater.env("AXOUPDATER_CONFIG_PATH", bindir);
    // If we're not running in CI, try to avoid ruining the user's PATH.
    if std::env::var("CI").is_err() {
        updater.env("INSTALLER_NO_MODIFY_PATH", "1");
    }
    // We'll do that manually
    updater.check(false);
    let _res = updater.output().unwrap();

    // Now let's check the version we just updated to
    let new_axolotlsay_path = &bindir.join(format!("axolotlsay{EXE_SUFFIX}"));
    assert!(new_axolotlsay_path.exists());
    let mut new_axolotlsay = Cmd::new(new_axolotlsay_path, "version test");
    new_axolotlsay.arg("--version");
    let output = new_axolotlsay.output().unwrap();
    let stderr_string = String::from_utf8(output.stdout).unwrap();
    assert_eq!(stderr_string, format!("axolotlsay {}\n", target_version));

    Ok(())
}

// A similar test to the one above, but it actually downgrades to an older
// version on request instead of upgrading.
#[test]
fn test_downgrade_to_specific_version() -> std::io::Result<()> {
    let tempdir = TempDir::new()?;
    let bindir_path = &tempdir.path().join("bin");
    let bindir = Utf8Path::from_path(bindir_path).unwrap();
    std::fs::create_dir_all(bindir)?;

    let base_version = "0.2.116";
    let target_version = "0.2.115";

    let url = axolotlsay_tarball_path(base_version);
    let compressed_path =
        Utf8PathBuf::from_path_buf(tempdir.path().join("axolotlsay.tar.gz")).unwrap();

    let client = axoasset::AxoClient::with_reqwest(axoasset::reqwest::Client::new());
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(client.load_and_write_to_file(&url, &compressed_path))
        .unwrap();

    // Write the receipt for the updater to use
    write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;

    LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

    // Now install our copy of the updater instead of the one axolotlsay came with
    let updater_path = bindir.join(format!("axolotlsay-update{EXE_SUFFIX}"));
    std::fs::copy(BIN, &updater_path)?;

    let mut updater = Cmd::new(&updater_path, "run updater");
    updater.arg("--version").arg(target_version);
    updater.env("AXOUPDATER_CONFIG_PATH", bindir);
    // If we're not running in CI, try to avoid ruining the user's PATH.
    if std::env::var("CI").is_err() {
        updater.env("INSTALLER_NO_MODIFY_PATH", "1");
    }
    // We'll do that manually
    updater.check(false);
    let _res = updater.output().unwrap();

    // Now let's check the version we just updated to
    let new_axolotlsay_path = &bindir.join(format!("axolotlsay{EXE_SUFFIX}"));
    assert!(new_axolotlsay_path.exists());
    let mut new_axolotlsay = Cmd::new(new_axolotlsay_path, "version test");
    new_axolotlsay.arg("--version");
    let output = new_axolotlsay.output().unwrap();
    let stderr_string = String::from_utf8(output.stdout).unwrap();
    assert_eq!(stderr_string, format!("axolotlsay {}\n", target_version));

    Ok(())
}

// A similar test to the one above, but it upgrades to a significantly older
// version that's outside the GitHub API's 30 versions.
// This version isn't available for every target, so we only run it for
// certain target triples.
#[test]
fn test_downgrade_to_specific_old_version() -> std::io::Result<()> {
    // Only available for x86_64 Darwin and x86_64 Linux
    match std::env::consts::OS {
        "linux" | "macos" => {
            if std::env::consts::ARCH != "x86_64" {
                return Ok(());
            }
        }
        "windows" => return Ok(()),
        _ => return Ok(()),
    }

    let tempdir = TempDir::new()?;
    let bindir_path = &tempdir.path().join("bin");
    let bindir = Utf8Path::from_path(bindir_path).unwrap();
    std::fs::create_dir_all(bindir)?;

    let base_version = "0.2.116";
    let target_version = "0.2.50";

    let url = axolotlsay_tarball_path(base_version);
    let compressed_path =
        Utf8PathBuf::from_path_buf(tempdir.path().join("axolotlsay.tar.gz")).unwrap();

    let client = axoasset::AxoClient::with_reqwest(axoasset::reqwest::Client::new());
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(client.load_and_write_to_file(&url, &compressed_path))
        .unwrap();

    // Write the receipt for the updater to use
    write_receipt(base_version, "0.11.1", &bindir.to_path_buf())?;

    LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

    // Now install our copy of the updater instead of the one axolotlsay came with
    let updater_path = bindir.join(format!("axolotlsay-update{EXE_SUFFIX}"));
    std::fs::copy(BIN, &updater_path)?;

    let mut updater = Cmd::new(&updater_path, "run updater");
    updater.arg("--version").arg(target_version);
    updater.env("AXOUPDATER_CONFIG_PATH", bindir);
    // If we're not running in CI, try to avoid ruining the user's PATH.
    if std::env::var("CI").is_err() {
        updater.env("INSTALLER_NO_MODIFY_PATH", "1");
    }
    // This installer is so old it doesn't respect the install path, so we
    // have to set CARGO_HOME to force it.
    updater.env("CARGO_HOME", tempdir.path());
    // We'll do that manually
    updater.check(false);
    let _res = updater.output().unwrap();

    // Now let's check the version we just updated to
    let new_axolotlsay_path = &bindir.join(format!("axolotlsay{EXE_SUFFIX}"));
    assert!(new_axolotlsay_path.exists());
    let mut new_axolotlsay = Cmd::new(new_axolotlsay_path, "version test");
    new_axolotlsay.arg("--version");
    let output = new_axolotlsay.output().unwrap();
    let stderr_string = String::from_utf8(output.stdout).unwrap();
    assert_eq!(stderr_string, format!("axolotlsay {}\n", target_version));

    Ok(())
}

// Similar to `test_upgrade` but tests releases created after the
// cargo-dist receipt prefix bug was fixed; see:
// https://github.com/axodotdev/cargo-dist/pull/1037
#[test]
fn test_upgrade_from_prefix_with_no_bin() -> std::io::Result<()> {
    let tempdir = TempDir::new()?;
    let prefix = Utf8PathBuf::from_path_buf(tempdir.path().to_path_buf()).unwrap();
    let bindir_path = &tempdir.path().join("bin");
    let bindir = Utf8Path::from_path(bindir_path).unwrap();
    std::fs::create_dir_all(bindir)?;

    // The first cargodisttest release with the "/bin" bug fixed
    let base_version = "0.2.133";

    let url = axolotlsay_tarball_path(base_version);
    let compressed_path =
        Utf8PathBuf::from_path_buf(tempdir.path().join("axolotlsay.tar.gz")).unwrap();

    let client = axoasset::AxoClient::with_reqwest(axoasset::reqwest::Client::new());
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(client.load_and_write_to_file(&url, &compressed_path))
        .unwrap();

    // Write the receipt for the updater to use
    // 0.15.0 is the first cargo-dist that published fixed installers for the
    // /bin bug mentioned above
    write_receipt(base_version, "0.15.0", &prefix)?;

    LocalAsset::untar_gz_all(&compressed_path, bindir).unwrap();

    // Now install our copy of the updater instead of the one axolotlsay came with
    let updater_path = bindir.join(format!("axolotlsay-update{EXE_SUFFIX}"));
    std::fs::copy(BIN, &updater_path)?;

    let mut updater = Cmd::new(&updater_path, "run updater");
    updater.env("AXOUPDATER_CONFIG_PATH", prefix);
    // If we're not running in CI, try to avoid ruining the user's PATH.
    if std::env::var("CI").is_err() {
        updater.env("INSTALLER_NO_MODIFY_PATH", "1");
    }
    // We'll do that manually
    updater.check(false);
    let res = updater.output().unwrap();

    let output_stdout = String::from_utf8(res.stdout).unwrap();
    let output_stderr = String::from_utf8(res.stderr).unwrap();

    // Now let's check the version we just updated to
    let new_axolotlsay_path = &bindir.join(format!("axolotlsay{EXE_SUFFIX}"));
    assert!(
        new_axolotlsay_path.exists(),
        "update result was\nstdout\n{}\nstderr\n{}",
        output_stdout,
        output_stderr
    );
    let mut new_axolotlsay = Cmd::new(new_axolotlsay_path, "version test");
    new_axolotlsay.arg("--version");
    let output = new_axolotlsay.output().unwrap();
    let stderr_string = String::from_utf8(output.stdout).unwrap();
    assert!(stderr_string.starts_with("axolotlsay "));
    assert_ne!(stderr_string, format!("axolotlsay {}\n", base_version));

    Ok(())
}