60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestConfigFilePathUsesXDGConfigDir(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
|
|
got, err := configFilePath()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "docspell-cli", "config.json")
|
|
if got != want {
|
|
t.Fatalf("expected %q, got %q", want, got)
|
|
}
|
|
}
|
|
|
|
func TestArchiveDestinationIndexesMultipleAttachmentsAndPreservesDistinctFiles(t *testing.T) {
|
|
directory := t.TempDir()
|
|
sourcePath := filepath.Join(t.TempDir(), "source.pdf")
|
|
if err := os.WriteFile(sourcePath, []byte("source"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, alreadyArchived, err := archiveDestination(directory, "Document", "pdf", 2, sourcePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if alreadyArchived || got != filepath.Join(directory, "Document.1.pdf") {
|
|
t.Fatalf("expected first indexed destination, got %q (already archived: %t)", got, alreadyArchived)
|
|
}
|
|
|
|
if err := os.WriteFile(got, []byte("different"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, alreadyArchived, err = archiveDestination(directory, "Document", "pdf", 2, sourcePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if alreadyArchived || got != filepath.Join(directory, "Document.2.pdf") {
|
|
t.Fatalf("expected second indexed destination, got %q (already archived: %t)", got, alreadyArchived)
|
|
}
|
|
|
|
if err := os.WriteFile(got, []byte("source"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, alreadyArchived, err = archiveDestination(directory, "Document", "pdf", 2, sourcePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !alreadyArchived || got != filepath.Join(directory, "Document.2.pdf") {
|
|
t.Fatalf("expected matching indexed destination, got %q (already archived: %t)", got, alreadyArchived)
|
|
}
|
|
}
|