Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,50 @@ fn suggestion(cache: &Cache, args: &[std::ffi::OsString], interactive: bool) ->
.latest
.as_ref()
.filter(|latest| newer(latest))
.map(|latest| format!("proofline {latest} is available; run 'proofline update'"))
.map(|latest| notice(latest, cfg!(target_os = "windows")))
}

fn notice(latest: &str, windows: bool) -> String {
if windows {
format!("proofline {latest} is available; 'proofline update' shows how to install it")
} else {
format!("proofline {latest} is available; run 'proofline update'")
}
}

/// The README's verified manual update: self-update cannot replace a Windows
/// binary, and Windows has no Unix installer directory to check.
/// `latest` is the cached version the notice announced; its pinned downloads
/// exist before GitHub marks that release as the latest one.
fn windows_steps(executable: Option<&Path>, latest: Option<&str>) -> String {
const ARCHIVE: &str = "proofline-x86_64-pc-windows-msvc.tar.gz";
let download = latest.map_or_else(
|| {
// No newer version is known, so this may already be the newest one.
format!(
"{ARCHIVE} and SHA256SUMS from https://github.com/prooflinedev/cli/releases/latest \
(stop if that release is not newer than this proofline {})",
env!("CARGO_PKG_VERSION")
)
},
|version| {
let release = format!("{RELEASES}/download/{version}");
format!("{release}/{ARCHIVE} and {release}/SHA256SUMS")
},
);
let executable = executable.map_or_else(
|| "the proofline.exe you ran (find it with '(Get-Command proofline).Source')".to_owned(),
|path| path.display().to_string(),
);
format!(
"'proofline update' cannot replace proofline.exe on Windows; update it by hand:\n \
1. Close every running Proofline process.\n \
2. Download {download}\n \
3. Run 'Get-FileHash -Algorithm SHA256 {ARCHIVE}' and check that the hash matches the archive's line in SHA256SUMS.\n \
4. Create an empty folder with 'mkdir proofline-new' (choose another name if it already exists).\n \
5. Extract into it with 'tar -xzf {ARCHIVE} -C proofline-new'.\n \
6. Only if that succeeded, replace {executable} with proofline-new\\proofline.exe."
)
}

fn prepare_check(
Expand Down Expand Up @@ -422,6 +465,16 @@ fn replace(archive: &[u8], destination: &Path) -> anyhow::Result<()> {

/// Updates an official installer location after verifying the pinned archive.
pub(crate) async fn install() -> anyhow::Result<()> {
if cfg!(target_os = "windows") {
let latest = cache_path()
.and_then(|path| read_cache(&path).ok())
.and_then(|cache| cache.latest)
.filter(|latest| newer(latest));
bail!(windows_steps(
std::env::current_exe().ok().as_deref(),
latest.as_deref()
));
}
let (destination, target) =
tokio::task::spawn_blocking(|| Ok::<_, anyhow::Error>((destination()?, target()?)))
.await??;
Expand Down
56 changes: 54 additions & 2 deletions src/update/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,58 @@ fn notices_are_suppressed_for_machine_and_hook_commands() {
assert!(suggestion(&cache, &["status".into()], true).is_some());
}

#[test]
fn windows_notices_point_to_the_manual_steps() {
assert_eq!(
notice("9.0.0", false),
"proofline 9.0.0 is available; run 'proofline update'"
);
let windows = notice("9.0.0", true);
assert!(
windows.starts_with("proofline 9.0.0 is available;"),
"{windows}"
);
assert!(
windows.contains("'proofline update' shows how"),
"{windows}"
);
// `suggestion` itself must pick the wording of the platform it runs on.
let cache = Cache {
checked: now(),
latest: Some("9.0.0".to_owned()),
};
assert_eq!(
suggestion(&cache, &["status".into()], true).unwrap(),
notice("9.0.0", cfg!(target_os = "windows"))
);
let steps = windows_steps(Some(Path::new(r"C:\tools\proofline.exe")), Some("9.0.0"));
for expected in [
"Close every running Proofline process",
"https://proofline.sh/cli/download/9.0.0/proofline-x86_64-pc-windows-msvc.tar.gz",
"https://proofline.sh/cli/download/9.0.0/SHA256SUMS",
"Get-FileHash -Algorithm SHA256 proofline-x86_64-pc-windows-msvc.tar.gz",
"mkdir proofline-new",
"tar -xzf proofline-x86_64-pc-windows-msvc.tar.gz -C proofline-new",
r"replace C:\tools\proofline.exe with proofline-new\proofline.exe",
] {
assert!(
steps.contains(expected),
"{expected:?} missing from {steps}"
);
}
let unknown = windows_steps(None, None);
for expected in [
"from https://github.com/prooflinedev/cli/releases/latest",
"(Get-Command proofline).Source",
concat!("not newer than this proofline ", env!("CARGO_PKG_VERSION")),
] {
assert!(
unknown.contains(expected),
"{expected:?} missing from {unknown}"
);
}
}

// This same test executable supplies a real child fixture. All endpoint and
// fixture-mode injection lives in cfg(test), never in the customer binary.
#[tokio::test]
Expand Down Expand Up @@ -294,7 +346,7 @@ async fn detached_discovery_is_serialized_and_notices_arrive_on_the_next_invocat
assert!(
!String::from_utf8(output.stdout)
.unwrap()
.contains("run 'proofline update'")
.contains(" is available;")
);
}
tokio::time::timeout(Duration::from_secs(5), async {
Expand Down Expand Up @@ -322,7 +374,7 @@ async fn detached_discovery_is_serialized_and_notices_arrive_on_the_next_invocat
assert!(
String::from_utf8(output.stdout)
.unwrap()
.contains("run 'proofline update'")
.contains(&notice("9.0.0", cfg!(target_os = "windows")))
);
assert_eq!(received.load(Ordering::SeqCst), 1);
server.abort();
Expand Down
81 changes: 81 additions & 0 deletions tests/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,84 @@ fn cached_update_preserves_schema_help_and_source_installation() {
b"keep this installed CLI"
);
}

// Windows binaries are installed by hand (README "Windows installation"), so
// `update` must explain those steps instead of failing on Unix installer paths.
#[cfg(windows)]
#[test]
fn windows_update_explains_the_manual_steps() {
let directory = tempfile::tempdir().unwrap();
let installed = directory.path().join("bin").join("proofline.exe");
std::fs::create_dir_all(installed.parent().unwrap()).unwrap();
std::fs::copy(env!("CARGO_BIN_EXE_proofline"), &installed).unwrap();
let before = std::fs::read(&installed).unwrap();
let cache = directory.path().join("proofline").join("update.json");
std::fs::create_dir_all(cache.parent().unwrap()).unwrap();
let checked = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
// Without a cached newer version the steps name the GitHub page; with
// one they pin the downloads to the version the notice announced.
for (home, cached, download) in [
(
None,
false,
"https://github.com/prooflinedev/cli/releases/latest",
),
(
Some(directory.path()),
false,
"https://github.com/prooflinedev/cli/releases/latest",
),
(
None,
true,
"https://proofline.sh/cli/download/9.0.0/proofline-x86_64-pc-windows-msvc.tar.gz",
),
] {
if cached {
std::fs::write(
&cache,
format!("{{\"checked\":{checked},\"latest\":\"9.0.0\"}}"),
)
.unwrap();
}
let mut command = Command::new(&installed);
command
.arg("update")
.env("PROOFLINE_NO_UPDATE_CHECK", "1")
.env("LOCALAPPDATA", directory.path())
.env_remove("PROOFLINE_INSTALL_DIR")
.env_remove("HOME");
if let Some(home) = home {
command.env("HOME", home);
}
let update = command.output().unwrap();
assert!(!update.status.success());
assert!(update.stdout.is_empty());
let stderr = String::from_utf8(update.stderr).unwrap();
for expected in [
download,
"proofline-x86_64-pc-windows-msvc.tar.gz",
"SHA256SUMS",
"Get-FileHash -Algorithm SHA256",
"-C proofline-new",
&installed.display().to_string(),
] {
assert!(
stderr.contains(expected),
"{expected:?} missing from {stderr}"
);
}
assert!(
!stderr.contains("set HOME or PROOFLINE_INSTALL_DIR"),
"{stderr}"
);
assert!(
!stderr.contains("outside the installer directory"),
"{stderr}"
);
}
assert_eq!(std::fs::read(&installed).unwrap(), before);
}