feat: a running lazysecrets says when a newer one was installed over it
Every minute it looks whether its binary was replaced, from anywhere (install.sh, go install, another lazysecrets): a stat, and the new binary's --version when it changed. It shows → vX (restart), as after U, so a long-running one (lazysecrets.nvim's) doesn't run an old version unnoticed.
This commit is contained in:
7 files changed
+164
-1
No files matched your search
@@ -0,0 +1,40 @@
|
||||
package update
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBinaryReplaced(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
exe := filepath.Join(dir, "lazysecrets")
|
||||
write := func(version string) {
|
||||
t.Helper()
|
||||
tmp := filepath.Join(dir, ".new")
|
||||
if err := os.WriteFile(tmp, []byte("#!/bin/sh\necho lazysecrets "+version+"\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Rename(tmp, exe); err != nil { // as Install and install.sh do
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
write("v0.3.0")
|
||||
info, _ := os.Stat(exe)
|
||||
b := &Binary{path: exe, info: info}
|
||||
ctx := context.Background()
|
||||
|
||||
if v, ok := b.Replaced(ctx); ok {
|
||||
t.Errorf("the same file: replaced by %q", v)
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
write("v0.3.1")
|
||||
if v, ok := b.Replaced(ctx); !ok || v != "v0.3.1" {
|
||||
t.Errorf("installed over: %q %t", v, ok)
|
||||
}
|
||||
if v, ok := b.Replaced(ctx); ok {
|
||||
t.Errorf("said again: %q", v)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user