package main import ( "errors" "os" "path/filepath" "reflect" "strconv" "strings" "testing" ) const nobody = 4194400 func TestTheDeclaredWallpaperIsReadFromTheModulesFehbg(t *testing.T) { raw, err := os.ReadFile(filepath.Join("..", "..", "files", "fehbg")) if err != nil { t.Fatal(err) } w := parseFehbg(string(raw), "/home/op") if w == nil || w.Mode != "fill" || !reflect.DeepEqual(w.Images, []string{"/home/op/.local/share/feh/wallpapers/default.jpg"}) { t.Fatalf("%+v", w) } // The form feh writes itself, single-quoted, two monitors. w = parseFehbg("#!/bin/sh\nfeh --no-fehbg --bg-center '/a b/one.png' '/two.png' \n", "/home/op") if w.Mode != "center" || !reflect.DeepEqual(w.Images, []string{"/a b/one.png", "/two.png"}) { t.Fatalf("%+v", w) } if parseFehbg("#!/bin/sh\n", "/h") != nil { t.Fatal("a file without feh declares a wallpaper") } } func TestSetPutsTheImagesUpForThisSessionAndCurrentSaysSo(t *testing.T) { root := fakeMachine(t) fakeProcess(t, nobody, "i3", "DISPLAY=:1") if err := os.MkdirAll(filepath.Join(runUserDir, strconv.Itoa(os.Getuid())), 0o700); err != nil { t.Fatal(err) } home := filepath.Join(root, "home") for _, f := range []string{"Pictures/a.jpg", "Pictures/b.jpg"} { if err := os.MkdirAll(filepath.Dir(filepath.Join(home, f)), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(home, f), []byte("jpg"), 0o644); err != nil { t.Fatal(err) } } src, _ := os.ReadFile(filepath.Join("..", "..", "files", "fehbg")) if err := os.WriteFile(filepath.Join(home, ".fehbg"), src, 0o755); err != nil { t.Fatal(err) } bin := fakeBinaries(t, map[string]string{"feh": `echo "$* DISPLAY=$DISPLAY" > "$LOG"`}) t.Setenv("LOG", filepath.Join(bin, "log")) got, err := Set([]string{"~/Pictures/a.jpg", "Pictures/b.jpg"}, "scale") if err != nil || !strings.Contains(got.Note, "next login") { t.Fatalf("%+v, %v", got, err) } asked, _ := os.ReadFile(filepath.Join(bin, "log")) want := "--no-fehbg --bg-scale " + filepath.Join(home, "Pictures/a.jpg") + " " + filepath.Join(home, "Pictures/b.jpg") + " DISPLAY=:1\n" if string(asked) != want { t.Fatalf("feh was asked %q, want %q", asked, want) } cur, err := Current() if err != nil || cur.Declared == nil || cur.Session == nil || cur.Session.Mode != "scale" || len(cur.Session.Images) != 2 || cur.Declared.Images[0] != filepath.Join(home, ".local/share/feh/wallpapers/default.jpg") { t.Fatalf("%+v, %v", cur, err) } } func TestSetRefusesWhatFehCannotShow(t *testing.T) { fakeMachine(t) if _, err := Set([]string{"/nowhere.jpg"}, ""); err == nil { t.Fatal("a missing image was accepted") } if _, err := Set([]string{"/"}, ""); err == nil { t.Fatal("a directory was accepted") } if _, err := Set([]string{"/etc/hostname"}, "stretch"); err == nil { t.Fatal("an unknown mode was accepted") } if _, err := Set(nil, ""); err == nil { t.Fatal("no image was accepted") } f := filepath.Join(t.TempDir(), "x.jpg") if err := os.WriteFile(f, nil, 0o644); err != nil { t.Fatal(err) } if _, err := Set([]string{f}, ""); !errors.Is(err, ErrNoSession) { t.Fatalf("without a session: %v", err) } }