49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package icon
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestInstall: the icon lands in the directory, is left alone when current,
|
||
|
|
// and is repaired when the file on disk differs.
|
||
|
|
func TestInstall(t *testing.T) {
|
||
|
|
if len(PNG) < 8 || !bytes.HasPrefix(PNG, []byte("\x89PNG")) {
|
||
|
|
t.Fatalf("embedded icon is not a PNG (%d bytes)", len(PNG))
|
||
|
|
}
|
||
|
|
dir := filepath.Join(t.TempDir(), "talk")
|
||
|
|
path, err := Install(dir)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if path != filepath.Join(dir, FileName) {
|
||
|
|
t.Errorf("path = %q", path)
|
||
|
|
}
|
||
|
|
got, err := os.ReadFile(path)
|
||
|
|
if err != nil || !bytes.Equal(got, PNG) {
|
||
|
|
t.Fatalf("installed file differs (err %v)", err)
|
||
|
|
}
|
||
|
|
first, _ := os.Stat(path)
|
||
|
|
if _, err := Install(dir); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
second, _ := os.Stat(path)
|
||
|
|
if !second.ModTime().Equal(first.ModTime()) {
|
||
|
|
t.Error("unchanged icon was rewritten")
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(path, []byte("stale"), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, err := Install(dir); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got, _ = os.ReadFile(path)
|
||
|
|
if !bytes.Equal(got, PNG) {
|
||
|
|
t.Error("stale icon was not replaced")
|
||
|
|
}
|
||
|
|
if _, err := Install(""); err == nil {
|
||
|
|
t.Error("Install(\"\") succeeded")
|
||
|
|
}
|
||
|
|
}
|